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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | 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.
using var client = new AssemblyAIRealtimeClient();
await client.ConnectAsync(
new Uri($"wss://streaming.assemblyai.com/v3/ws?api_key={apiKey}"));
// 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 session starts, send audio data.
// In a real application, you would stream microphone PCM16 audio:
// await client.SendAsync(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());
}
else if (serverEvent.IsTurn)
{
var turn = serverEvent.Turn!;
Console.WriteLine($"Turn {turn.TurnOrder}: {turn.Transcript}");
Console.WriteLine($" End of turn: {turn.EndOfTurn}");
Console.WriteLine($" Confidence: {turn.EndOfTurnConfidence}");
if (turn.EndOfTurn == true)
{
break;
}
}
else if (serverEvent.IsTermination)
{
var termination = serverEvent.Termination!;
Console.WriteLine($"Session terminated. Audio: {termination.AudioDurationSeconds}s, Session: {termination.SessionDurationSeconds}s");
break;
}
}
// Gracefully terminate the session.
await client.SendSessionTerminationAsync(new SessionTerminationPayload());
|