Skip to content

Transcribe Live

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

This example assumes using AssemblyAI; is in scope and apiKey contains your AssemblyAI API key.

 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
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
{
    FormatTurns = true,
});

// 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
        {
            EndOfTurnConfidenceThreshold = 0.5,
            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());