Basic example showing how to create an authenticated Soniox client. The
SONIOX_API_KEY environment variable holds the API key issued by the
Soniox Console.
1
usingvarclient=newSonioxClient(apiKey);
List models
Fetches the list of Soniox speech-to-text models available to your workspace,
including supported languages and transcription mode (async / real-time).
Submits a Soniox async transcription job for a public audio URL and polls
until it completes. Uses the current default async model.
1 2 3 4 5 6 7 8 91011121314151617
usingvarclient=newSonioxClient(apiKey);varcreated=awaitclient.Transcriptions.CreateTranscriptionAsync(model:SonioxClient.DefaultAsyncModel,audioUrl:"https://soniox.com/media/examples/coffee_shop.mp3");// Poll until the job reaches a terminal state.while(created.StatusisTranscriptionStatus.QueuedorTranscriptionStatus.Processing){awaitTask.Delay(1000);created=awaitclient.Transcriptions.GetTranscriptionAsync(created.Id);}vartranscript=awaitclient.Transcriptions.GetTranscriptionTranscriptAsync(created.Id);// Clean up to keep the workspace tidy.awaitclient.Transcriptions.DeleteTranscriptionAsync(created.Id);
Voice cloning with Text-to-Speech
Creates a Soniox voice clone from a short reference clip, waits until it is
ready for the current TTS model, then uses the cloned voice ID in a REST
Text-to-Speech request.
Set SONIOX_VOICE_CLONE_AUDIO_PATH to a clear speech sample you have the
rights and consent to clone. Soniox accepts reference clips up to 20 seconds.
Set SONIOX_RUN_VOICE_CLONING_EXAMPLE=1 before running this paid example.
if(!IsEnvironmentFlagEnabled(RunVoiceCloningExampleFlag)&&!IsEnvironmentFlagEnabled(RunPaidTestsFlag)){thrownewAssertInconclusiveException($"Set {RunVoiceCloningExampleFlag}=1 to run this paid voice-cloning example.");}varaudioPath=Environment.GetEnvironmentVariable("SONIOX_VOICE_CLONE_AUDIO_PATH")is{Length:>0}path?path:thrownewAssertInconclusiveException("SONIOX_VOICE_CLONE_AUDIO_PATH environment variable is not found.");usingvarclient=newSonioxClient(apiKey);awaitusingvarreferenceAudio=System.IO.File.OpenRead(audioPath);varvoice=awaitclient.Voices.CreateVoiceAsync(name:$"sdk-example-{Guid.NewGuid():N}",file:referenceAudio,filename:System.IO.Path.GetFileName(audioPath));try{voice=awaitWaitForVoiceReadyAsync(client:client,voiceId:voice.Id,model:SonioxClient.DefaultTtsModel);varaudio=awaitclient.GenerateSpeechAsync(text:"Hello from a cloned Soniox voice.",voice:voice.Id.ToString(),language:"en",audioFormat:"wav",sampleRate:24000);}finally{awaitclient.Voices.DeleteVoiceAsync(voice.Id);}
Realtime Text-to-Speech
Streams text to the Soniox realtime Text-to-Speech WebSocket API. The default
test path serializes generated messages without making a network call. Set
SONIOX_RUN_REALTIME_TTS_EXAMPLE=1 to run the paid live example.
varstreamId=$"sdk-example-{Guid.NewGuid():N}";varconfig=newTtsRealtime.TtsConfig{ApiKey=GetOptionalEnvironmentVariable("SONIOX_API_KEY")??"test-key",StreamId=streamId,Model=SonioxClient.DefaultTtsModel,Language=SonioxClient.DefaultTtsLanguage,Voice="Adrian",AudioFormat=SonioxClient.DefaultTtsAudioFormat,SampleRate=24000,ReturnTimestamps=true,Speed=1.1,};vartextChunks=new[]{newTtsRealtime.TtsText{StreamId=streamId,Text="Hello from realtime ",TextEnd=false,},newTtsRealtime.TtsText{StreamId=streamId,Text="Text-to-Speech.",TextEnd=true,},};varkeepAlive=newTtsRealtime.TtsKeepAlive{KeepAlive=true};varcancel=newTtsRealtime.TtsCancel{StreamId=streamId,Cancel=true};if(!IsEnvironmentFlagEnabled(RunRealtimeTtsExampleFlag)){varconfigJson=JsonSerializer.Serialize(config,typeof(TtsRealtime.TtsConfig),TtsRealtime.TtsRealtimeSourceGenerationContext.Default);varfirstTextJson=JsonSerializer.Serialize(textChunks[0],typeof(TtsRealtime.TtsText),TtsRealtime.TtsRealtimeSourceGenerationContext.Default);varkeepAliveJson=JsonSerializer.Serialize(keepAlive,typeof(TtsRealtime.TtsKeepAlive),TtsRealtime.TtsRealtimeSourceGenerationContext.Default);varcancelJson=JsonSerializer.Serialize(cancel,typeof(TtsRealtime.TtsCancel),TtsRealtime.TtsRealtimeSourceGenerationContext.Default);return;}usingvarcancellationTokenSource=newCancellationTokenSource(TimeSpan.FromSeconds(45));awaitusingvarclient=newTtsRealtime.SonioxTtsRealtimeClient();awaitclient.ConnectAsync(keepAliveInterval:TimeSpan.FromSeconds(15),connectTimeout:TimeSpan.FromSeconds(10),cancellationToken:cancellationTokenSource.Token);config.ApiKey=GetRequiredEnvironmentVariable("SONIOX_API_KEY");awaitclient.SendTtsConfigAsync(config,cancellationTokenSource.Token);awaitclient.SendTtsTextAsync(textChunks[0],cancellationTokenSource.Token);awaitclient.SendTtsKeepAliveAsync(keepAlive,cancellationTokenSource.Token);awaitclient.SendTtsTextAsync(textChunks[1],cancellationTokenSource.Token);varresult=awaitCollectRealtimeTtsResultAsync(client:client,streamId:streamId,cancellationToken:cancellationTokenSource.Token);
MEAI ISpeechToTextClient
SonioxClient implements Microsoft.Extensions.AI.ISpeechToTextClient, so the
same call site works with Soniox, Deepgram, Gladia, or any other MEAI STT
provider.
Non-streaming calls upload the audio to /v1/files, create a transcription
job on /v1/transcriptions, and poll until the job completes. Streaming
calls open a WebSocket to wss://stt-rt.soniox.com/transcribe-websocket.
12345678
usingvarclient=newSonioxClient(apiKey);// SonioxClient implements Meai.ISpeechToTextClient directly.Meai.ISpeechToTextClientspeechClient=client;// Metadata is exposed via ISpeechToTextClient.GetService.varmetadata=speechClient.GetService(typeof(Meai.SpeechToTextClientMetadata))asMeai.SpeechToTextClientMetadata;
MEAI AIFunction tools
Using Soniox endpoints as AIFunction tools with any Microsoft.Extensions.AI
IChatClient.
1 2 3 4 5 6 7 8 910111213
usingvarclient=newSonioxClient(apiKey);// Create AIFunction tools from the Soniox client.vartranscribeTool=client.AsTranscribeTool();vargetTool=client.AsGetTranscriptionTool();varlistModelsTool=client.AsListModelsTool();varlistLanguagesTool=client.AsListLanguagesTool();vartempKeyTool=client.AsCreateTemporaryApiKeyTool();// Verify all tools are created with the expected names.// These tools can be passed to any IChatClient for function calling.vartools=new[]{transcribeTool,getTool,listModelsTool,listLanguagesTool,tempKeyTool};