AssemblyAI has an official .NET SDK available,
and we use this SDK primarily to improve the overall experience
of our SDK/try to reach/exceed the level of the official one and
extend it to all our generated SDKs for other platforms.
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 91011121314151617181920212223
usingAssemblyAI;usingvarapi=newAssemblyAIClient(apiKey);varfileUrl="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;Transcripttranscript=awaitapi.Transcripts.SubmitAsync(newTranscriptParams{AudioUrl=fileUrl,SpeechModels=[],LanguageDetection=true,SpeakerLabels=true,// Identify speakers in your audiosAutoHighlights=true,// Identifying highlights in your audio});transcript.EnsureStatusCompleted();Console.WriteLine(transcript);
usingvarclient=GetAuthenticatedApi();varfileUrl="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 client.Files.UploadAsync(await File.ReadAllBytesAsync(filePath));// fileUrl = uploadedFile.UploadUrl;Transcripttranscript=awaitclient.Transcripts.SubmitAsync(newTranscriptParams{AudioUrl=fileUrl,SpeechModels=[],LanguageDetection=true,SpeakerLabels=true,// Identify speakers in your audiosAutoHighlights=true,// Identifying highlights in your audio});transcript.EnsureStatusCompleted();Console.WriteLine(transcript);
Transcribe Live
Connect to the AssemblyAI v3 real-time streaming API for live speech-to-text transcription.
varapiKey=Environment.GetEnvironmentVariable("ASSEMBLYAI_API_KEY")is{Length:>0}apiKeyValue?apiKeyValue:thrownewAssertInconclusiveException("ASSEMBLYAI_API_KEY environment variable is not found.");// Create the realtime client and connect.usingvarclient=newAssemblyAIRealtimeClient();awaitclient.ConnectAsync(newUri($"wss://streaming.assemblyai.com/v3/ws?api_key={apiKey}"));// Receive the session started event.usingvarcts=newCancellationTokenSource(TimeSpan.FromSeconds(30));varreceivedSessionBegins=false;awaitforeach(varserverEventinclient.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.awaitclient.SendUpdateConfigurationAsync(newUpdateConfigurationPayload{EndOfTurnConfidenceThreshold=0.5,MaxTurnSilence=2000,});// For this example, manually force an endpoint to get results.awaitclient.SendForceEndpointAsync(newForceEndpointPayload());}elseif(serverEvent.IsTurn){varturn=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;}}elseif(serverEvent.IsTermination){vartermination=serverEvent.Termination!;Console.WriteLine($"Session terminated. Audio: {termination.AudioDurationSeconds}s, Session: {termination.SessionDurationSeconds}s");break;}}// Gracefully terminate the session.awaitclient.SendSessionTerminationAsync(newSessionTerminationPayload());
Speech To Text Client Get Text Async
1 2 3 4 5 6 7 8 910111213141516
usingvarclient=GetAuthenticatedApi();ISpeechToTextClientspeechClient=client;// Transcribe audio using the MEAI ISpeechToTextClient interface.// The client uploads the audio stream and polls until transcription is complete.usingvarhttpClient=newHttpClient();awaitusingvaraudioStream=awaithttpClient.GetStreamAsync("https://github.com/AssemblyAI-Community/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3");varms=newMemoryStream();awaitaudioStream.CopyToAsync(ms);ms.Position=0;varresponse=awaitspeechClient.GetTextAsync(ms);Console.WriteLine($"Text: {response.Text}");
Speech To Text Client Get Service Metadata
12345
usingvarclient=newAssemblyAIClient("dummy-key");ISpeechToTextClientspeechClient=client;// Retrieve metadata about the speech-to-text provider.varmetadata=speechClient.GetService<SpeechToTextClientMetadata>();
Speech To Text Client Get Service Self
12345
usingvarclient=newAssemblyAIClient("dummy-key");ISpeechToTextClientspeechClient=client;// Access the underlying AssemblyAIClient from the MEAI interface.varself=speechClient.GetService<AssemblyAIClient>();