Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating Deepgram Speech-to-Text API with ASP.NET Core: A Comprehensive Guide

Integrating Deepgram Speech-to-Text API with ASP.NET Core: A Comprehensive Guide

Date- May 07,2026 232
deepgram speech to text

Overview

The Deepgram Speech-to-Text API is a powerful tool that converts spoken language into written text using advanced machine learning algorithms. This technology exists to address the growing demand for speech recognition in various applications, from transcription services to voice command interfaces. By automating the transcription process, it significantly reduces the time and effort required to convert audio content into text, thus allowing developers to focus on enhancing user experiences.

Real-world use cases for Deepgram's API are diverse. Businesses can implement it for customer service applications to transcribe calls for quality assurance. Educational institutions can use it to transcribe lectures, making content accessible to all students. Additionally, media companies can leverage speech recognition to create searchable archives of audio content, making it easier to retrieve information. The implications of integrating such technology into applications are profound, leading to improved efficiency and accessibility.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version of the ASP.NET Core SDK installed for developing and running the application.
  • Deepgram Account: Sign up for a Deepgram account to obtain your API key, necessary for authenticating requests.
  • Basic Knowledge of C#: Familiarity with C# programming language and ASP.NET Core framework will help in understanding the code examples.
  • Audio Files for Testing: Prepare audio files in formats supported by Deepgram (e.g., WAV, MP3) for testing the integration.
  • HTTP Client Library: Understanding of how to make HTTP requests in ASP.NET Core, preferably using HttpClient.

Setting Up the ASP.NET Core Project

To start using the Deepgram Speech-to-Text API, you first need to set up an ASP.NET Core project. This involves creating a new project using the .NET CLI or Visual Studio. The choice of template can vary depending on your requirements, but a web application template is commonly used for this purpose.

To create a new project using the .NET CLI, execute the following command:

dotnet new webapp -n DeepgramIntegration

This command creates a new ASP.NET Core web application named DeepgramIntegration. Navigate to the project folder using:

cd DeepgramIntegration

Once inside the project directory, you can open it in your preferred IDE, such as Visual Studio or Visual Studio Code, to start adding functionalities.

Adding Required NuGet Packages

Before we can interact with the Deepgram API, we need to add the necessary NuGet packages. The primary package required is System.Net.Http.Json, which allows for easy JSON serialization and deserialization.

dotnet add package System.Net.Http.Json

This command installs the package, making it easier to handle HTTP requests and responses in a JSON format. After adding the package, ensure your project file (csproj) reflects this dependency.

Configuring Deepgram API Credentials

To securely interact with the Deepgram API, you need to store your API key. The recommended approach is to use the appsettings.json file to manage configuration settings.

Open the appsettings.json file and add your Deepgram API key as follows:

{
"Deepgram": {
"ApiKey": "YOUR_DEEPGRAM_API_KEY"
}
}

Replace YOUR_DEEPGRAM_API_KEY with the actual API key obtained from your Deepgram account. This approach centralizes your configuration and allows for easy access throughout your application.

Implementing the Speech-to-Text Functionality

With the project set up and the API key configured, the next step is to implement the functionality that sends audio data to Deepgram and retrieves the transcribed text. This involves creating a service that handles the API requests.

Begin by creating a new folder named Services in your project structure. Within this folder, create a file named DeepgramService.cs and add the following code:

using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using System.IO;

public class DeepgramService
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;

public DeepgramService(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
_apiKey = configuration["Deepgram:ApiKey"];
}

public async Task TranscribeAudioAsync(string audioFilePath)
{
using var audioStream = File.OpenRead(audioFilePath);
using var content = new StreamContent(audioStream);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/wav");
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", _apiKey);
var response = await _httpClient.PostAsync("https://api.deepgram.com/v1/listen", content);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync();
return result.Channel.Alternatives[0].Transcript;
}
}

public class DeepgramResponse
{
public Channel Channel { get; set; }
}

public class Channel
{
public Alternative[] Alternatives { get; set; }
}

public class Alternative
{
public string Transcript { get; set; }
}

This DeepgramService class is responsible for making the HTTP request to the Deepgram API. Here's a breakdown of the code:

  • The constructor takes an HttpClient instance and an IConfiguration instance. The HttpClient is used to send requests, and IConfiguration retrieves the API key from the configuration file.
  • The TranscribeAudioAsync method opens the audio file specified by audioFilePath and sends it to the Deepgram API.
  • It sets the content type to audio/wav assuming the audio file is in WAV format. Adjust this if using a different format.
  • The API key is added to the request headers for authorization.
  • Upon receiving a response, it checks if the response was successful using EnsureSuccessStatusCode. If not, an exception is thrown.
  • The response is deserialized into the DeepgramResponse class, and the transcript is extracted and returned.

Using the DeepgramService

To utilize the DeepgramService in your application, you need to register it in the Startup.cs file. Modify the ConfigureServices method as follows:

public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton();
services.AddControllersWithViews();
}

This code registers the DeepgramService as a singleton, allowing it to be injected into controllers or other services where needed.

Creating the Controller for Transcription

Next, create a controller that handles requests and responses related to audio transcription. In the Controllers folder, create a new file named TranscriptionController.cs and add the following code:

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

public class TranscriptionController : Controller
{
private readonly DeepgramService _deepgramService;

public TranscriptionController(DeepgramService deepgramService)
{
_deepgramService = deepgramService;
}

[HttpPost("/transcribe")]
public async Task Transcribe([FromForm] string audioFilePath)
{
var transcript = await _deepgramService.TranscribeAudioAsync(audioFilePath);
return Ok(transcript);
}
}

This controller handles POST requests to the /transcribe endpoint. Here’s a breakdown of what each part does:

  • The constructor receives an instance of DeepgramService through dependency injection.
  • The Transcribe method is an action method that takes an audio file path as input from the form data.
  • It calls the TranscribeAudioAsync method of the DeepgramService to get the transcript and returns it as an HTTP response.

Creating the View for Uploading Audio

To allow users to upload audio files, you need a simple view. Create a new folder named Views/Transcription and add a file named Index.cshtml with the following content:

@model string

Audio Transcription

@if (Model != null)
{

Transcript:


@Model


}

This view provides a file input for users to upload audio files and displays the transcription result. The key components are:

  • The form uses the asp-action attribute to specify the action method to be called upon submission.
  • The enctype="multipart/form-data" attribute is necessary for file uploads.
  • Upon successful transcription, the result is displayed below the form.

Edge Cases & Gotchas

While integrating the Deepgram API, developers may encounter several edge cases and pitfalls. Understanding these can save time and improve the robustness of your application.

Handling Different Audio Formats

Deepgram supports various audio formats; however, it is essential to match the content type in your requests with the actual audio format being sent. For example, if you are sending a file in MP3 format, ensure the content type is set to audio/mpeg. Failure to do so may result in unexpected errors or incorrect transcriptions.

content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/mpeg");

Managing Long Audio Files

For long audio files, consider splitting the audio into smaller segments. The Deepgram API has limitations on the audio length for real-time processing. If a file exceeds these limits, it may not process correctly. Implementing logic to handle audio segmentation can be crucial for effective transcription.

Performance & Best Practices

Optimizing the performance of your integration with the Deepgram API can significantly enhance user experience. Here are some best practices to consider:

Asynchronous Processing

Utilizing asynchronous processing is vital for performance, especially when dealing with potentially long-running API calls. Ensure that all HTTP requests to the Deepgram API are asynchronous to prevent blocking the main thread, which could lead to unresponsive applications.

Implementing Caching

If your application frequently processes the same audio files, consider implementing caching mechanisms. Caching transcripts can reduce the number of API calls, leading to lower costs and improved response times. Use in-memory caching or distributed caching solutions based on your application's needs.

Real-World Scenario: Building a Transcription Web App

Let’s put everything together into a mini-project that allows users to upload audio files and receive transcriptions. In this scenario, we will create a simple web application using the previously discussed components.

Complete Code Implementation

Below is the complete implementation of the ASP.NET Core application:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
using System.Net.Http.Json;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton();
services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Transcription}/{action=Index}/{id?}");
});
}
}

public class DeepgramService
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;

public DeepgramService(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
_apiKey = configuration["Deepgram:ApiKey"];
}

public async Task TranscribeAudioAsync(string audioFilePath)
{
using var audioStream = File.OpenRead(audioFilePath);
using var content = new StreamContent(audioStream);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/wav");
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", _apiKey);
var response = await _httpClient.PostAsync("https://api.deepgram.com/v1/listen", content);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync();
return result.Channel.Alternatives[0].Transcript;
}
}

public class DeepgramResponse
{
public Channel Channel { get; set; }
}

public class Channel
{
public Alternative[] Alternatives { get; set; }
}

public class Alternative
{
public string Transcript { get; set; }
}

public class TranscriptionController : Controller
{
private readonly DeepgramService _deepgramService;

public TranscriptionController(DeepgramService deepgramService)
{
_deepgramService = deepgramService;
}

[HttpPost("/transcribe")]
public async Task Transcribe([FromForm] string audioFilePath)
{
var transcript = await _deepgramService.TranscribeAudioAsync(audioFilePath);
return Ok(transcript);
}
}

Views/Transcription/Index.cshtml


Audio Transcription

@if (Model != null)
{

Transcript:


@Model


}

With this complete implementation, you can run the application and access the transcription feature through the browser. Users can upload audio files, and the application will return the transcribed text.

Conclusion

  • Understanding the Deepgram Speech-to-Text API enables developers to integrate advanced speech recognition capabilities into their applications.
  • Setting up a robust ASP.NET Core application involves proper project structure, dependency injection, and configuration management.
  • Implementing best practices, such as asynchronous processing and caching, can enhance the performance and user experience of your application.
  • Handling edge cases, such as audio format compatibility and file size limitations, is essential for a smooth user experience.
  • The provided mini-project serves as a solid foundation for further exploration and development of voice-enabled applications.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating OpenAI DALL-E Image Generation in ASP.NET Core Applications
May 07, 2026
Resend Email API Integration in ASP.NET Core - Modern Transactional Email
Apr 26, 2026
CWE-78: Preventing OS Command Injection in ASP.NET Core Applications
May 30, 2026
Integrating Discord Bots with ASP.NET Core Using Discord.NET Library
May 25, 2026
Previous in ASP.NET Core
Integrating OpenAI DALL-E Image Generation in ASP.NET Core Applic…
Next in ASP.NET Core
Elasticsearch Integration in ASP.NET Core - Full-Text Search with…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 328 views
  • 2
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,928 views
  • 3
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 240 views
  • 4
    Error-An error occurred while processing your request in .… 11,953 views
  • 5
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 817 views
  • 6
    Send Email With HTML Template And PDF Using ASP.Net C# 17,171 views
  • 7
    Integrating Google reCAPTCHA Validation in ASP.NET MVC 6,457 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26677 views
  • Exception Handling Asp.Net Core 21714 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21168 views
  • How to implement Paypal in Asp.Net Core 20126 views
  • Task Scheduler in Asp.Net core 18195 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor