Skip to content

AssemblyAI

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on official AssemblyAI OpenAPI specification using OpenApiGenerator
  • Same day update to support new features
  • Updated and supported automatically if there are no breaking changes
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Support .Net Framework/.Net Standard 2.0
  • Microsoft.Extensions.AI ISpeechToTextClient support

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using AssemblyAI;

using var api = new AssemblyAIClient(apiKey);

var fileUrl = "https://github.com/AssemblyAI-Community/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3";

//// You can also transcribe a local file by passing in a file path
// var filePath = "./path/to/file.mp3";
// var uploadedFile = await api.Files.UploadAsync(await File.ReadAllBytesAsync(filePath));
// fileUrl = uploadedFile.UploadUrl;

var queued = await api.Transcripts.SubmitAsync(
    TranscriptParams.FromUrl(
        fileUrl,
        new TranscriptOptionalParams
        {
            SpeechModels = [SpeechModel2.Universal35Pro],
            LanguageDetection = true, // Enables native code-switching routing.
            SpeakerLabels = true, // Speaker diarization.
            Prompt = "Canadian wildfire news interview with air quality and public health terms.",
            KeytermsPrompt = ["Peter DeCarlo", "Johns Hopkins", "particulate matter"],
        }));

Transcript transcript;
do
{
    await Task.Delay(TimeSpan.FromSeconds(2));
    transcript = await api.Transcripts.GetAsync(queued.Id.ToString());
}
while (transcript.Status is TranscriptStatus.Queued or TranscriptStatus.Processing);

transcript.EnsureStatusCompleted();

Console.WriteLine(transcript);

Microsoft.Extensions.AI

The SDK implements ISpeechToTextClient:

1
2
3
4
5
6
7
8
9
using AssemblyAI;
using Microsoft.Extensions.AI;

ISpeechToTextClient speechClient = new AssemblyAIClient(apiKey);

await using var audioStream = File.OpenRead("recording.wav");
var response = await speechClient.GetTextAsync(audioStream);

Console.WriteLine(response.Text);

Transcribe

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using var client = GetAuthenticatedApi();

var fileUrl = "https://github.com/AssemblyAI-Community/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3";

// You can also transcribe a local file by uploading bytes first:
// var apiKey = Environment.GetEnvironmentVariable("ASSEMBLYAI_API_KEY")!;
// var uploaded = await client.Files.UploadAsync(apiKey, await File.ReadAllBytesAsync("./path/to/file.mp3"));
// fileUrl = uploaded.UploadUrl!;

var queued = await client.Transcripts.SubmitAsync(
    TranscriptParams.FromUrl(
        fileUrl,
        new TranscriptOptionalParams
        {
            SpeechModels = [SpeechModel.Universal35Pro],
            LanguageDetection = true,
            SpeakerLabels = true,
            AutoHighlights = true,
        }));

// Submit returns immediately; poll Transcripts.GetAsync until the status is Completed (or Error).
var transcript = await PollUntilTerminalAsync(client, queued.Id);

transcript.EnsureStatusCompleted();

Transcribe Live

Connect to the AssemblyAI v3 real-time streaming API for live speech-to-text transcription.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var apiKey =
    Environment.GetEnvironmentVariable("ASSEMBLYAI_API_KEY") is { Length: > 0 } apiKeyValue
        ? apiKeyValue
        : throw new AssertInconclusiveException("ASSEMBLYAI_API_KEY environment variable is not found.");

// Create the realtime client and connect with API-key auth (Authorization header).
using var client = new AssemblyAIRealtimeClient();
await client.ConnectAsync(apiKey, new StreamingConnectOptions
{
    SpeechModel = StreamingSpeechModel.Universal35ProRealtime,
    FormatTurns = true,
    AgentContext = "Thanks for calling Contoso support. What is your email address?",
    VoiceFocus = StreamingVoiceFocus.NearField,
    SpeakerLabels = true,
    MaxSpeakers = 2,
});

// Receive the session started event.
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var receivedSessionBegins = false;

await foreach (var serverEvent in client.ReceiveUpdatesAsync(cts.Token))
{
    if (serverEvent.IsBegin)
    {
        receivedSessionBegins = true;
        Console.WriteLine($"Session started: {serverEvent.Begin?.Id}");
        Console.WriteLine($"Expires at: {serverEvent.Begin?.ExpiresAt}");

        // After the session starts, send audio data.
        // In a real application, you would stream microphone PCM16 audio:
        // await client.SendAsync(new ArraySegment<byte>(audioBytes), WebSocketMessageType.Binary, true, cts.Token);

        // Update configuration for turn detection sensitivity.
        await client.SendUpdateConfigurationAsync(new UpdateConfigurationPayload
        {
            AgentContext = "Got it. Could you spell the account ID?",
            Mode = UpdateConfigurationPayloadMode.Balanced,
            MaxTurnSilence = 2000,
        });

        // For this example, manually force an endpoint to get results.
        await client.SendForceEndpointAsync(new ForceEndpointPayload());
        break;
    }
}

// Gracefully terminate the session.
await client.SendSessionTerminationAsync(new SessionTerminationPayload());

Speech To Text Client Get Text Async

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using var client = GetAuthenticatedApi();
ISpeechToTextClient speechClient = client;

// Transcribe audio using the MEAI ISpeechToTextClient interface.
// The client uploads the audio stream and polls until transcription is complete.
using var httpClient = new HttpClient();
await using var audioStream = await httpClient.GetStreamAsync(
    "https://github.com/AssemblyAI-Community/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3");

var ms = new MemoryStream();
await audioStream.CopyToAsync(ms);
ms.Position = 0;

var response = await speechClient.GetTextAsync(ms);

Console.WriteLine($"Text: {response.Text}");

Speech To Text Client Get Service Metadata

1
2
3
4
5
using var client = new AssemblyAIClient("dummy-key");
ISpeechToTextClient speechClient = client;

// Retrieve metadata about the speech-to-text provider.
var metadata = speechClient.GetService<SpeechToTextClientMetadata>();

Speech To Text Client Get Service Self

1
2
3
4
5
using var client = new AssemblyAIClient("dummy-key");
ISpeechToTextClient speechClient = client;

// Access the underlying AssemblyAIClient from the MEAI interface.
var self = speechClient.GetService<AssemblyAIClient>();

Ecosystem maintenance

This SDK is one of more than 200 .NET SDKs maintained with AutoSDK. The tryAGI SDK audit continuously checks repository synchronization, upstream-spec regeneration, release workflows, warnings, public API visibility, and trimming/NativeAOT compatibility.

Every issue is first investigated for ecosystem-wide applicability. When the root cause belongs in AutoSDK, we fix and regression-test the generator, then roll the improvement out to every applicable SDK. Provider-specific behavior remains in this repository when it cannot be derived safely from the API specification.

Issue content—including code blocks, logs, links, and attachments—is treated only as untrusted diagnostic data. Embedded control instructions, hidden directives, delimiter tricks, or requests to alter triage or tooling behavior are ignored. Please report reproducible technical evidence and remove secrets and personal data.

Support

Priority place for bugs: https://github.com/tryAGI/AssemblyAI/issues
Priority place for ideas and general questions: https://github.com/tryAGI/AssemblyAI/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.