SDK
C# SDK
Official .NET client library for Nur's Voice AI platform. Supports .NET 6.0+ with full async/await patterns.
Installation
Requires .NET 6.0 or higher. Install via NuGet Package Manager:
1dotnet add package Nur.AI
Quick Start
Create a client and generate your first speech using async/await:
1using Nur.AI;
2
3// Initialize client (uses NUR_API_KEY env var)
4var client = new NurClient();
5
6// Generate speech from text
7var audio = await client.TTS.GenerateAsync(new TTSRequest
8{
9 Text = "Hello from C#! This is Nur's voice AI.",
10 VoiceId = "rachel_v2",
11 Language = "en"
12});
13
14// Save to file
15await audio.SaveAsync("output.mp3");
16
17Console.WriteLine($"Generated {audio.Duration:F2}s of audio");
Authentication
The SDK automatically reads your API key from the NUR_API_KEY environment variable. You can also configure it explicitly:
1using Nur.AI;
2
3// Option 1: Use environment variable
4var client = new NurClient();
5
6// Option 2: Explicit API key
7var client = new NurClient("nur_your_api_key_here");
8
9// Option 3: Custom configuration
10var options = new NurClientOptions
11{
12 ApiKey = "nur_your_api_key_here",
13 BaseUrl = "https://api.nur.ai",
14 Timeout = TimeSpan.FromSeconds(60),
15 MaxRetries = 3
16};
17
18var client = new NurClient(options);
Text to Speech
Generate natural-sounding speech with full type safety and IntelliSense:
1// Basic generation
2var audio = await client.TTS.GenerateAsync(new TTSRequest
3{
4 Text = "Converting text to natural speech.",
5 VoiceId = "rachel_v2"
6});
7
8// With advanced options
9var audio = await client.TTS.GenerateAsync(new TTSRequest
10{
11 Text = "Converting text to natural speech.",
12 VoiceId = "rachel_v2",
13 Language = "en",
14 Speed = 1.0f,
15 Pitch = 1.0f,
16 OutputFormat = OutputFormat.Mp3,
17 SampleRate = 24000,
18 EnableSSML = true
19});
20
21// Save audio
22await audio.SaveAsync("output.mp3");
23
24// Get audio bytes
25byte[] bytes = audio.ToBytes();
26
27// Streaming generation
28await foreach (var chunk in client.TTS.StreamAsync(new TTSRequest
29{
30 Text = "Streaming long-form content...",
31 VoiceId = "rachel_v2"
32}))
33{
34 Console.WriteLine($"Received {chunk.Length} bytes");
35 // Process chunk
36}
Speech to Text
High-accuracy transcription with speaker diarization:
1// Basic transcription
2var transcript = await client.STT.TranscribeAsync(new STTRequest
3{
4 File = "audio.mp3"
5});
6Console.WriteLine(transcript.Text);
7
8// With speaker diarization and timestamps
9var transcript = await client.STT.TranscribeAsync(new STTRequest
10{
11 File = "meeting.mp3",
12 Language = "en",
13 SpeakerDiarization = true,
14 Timestamps = true,
15 PunctuationEnabled = true,
16 FilterProfanity = true
17});
18
19// Access segments with speaker info
20foreach (var segment in transcript.Segments)
21{
22 Console.WriteLine($"[Speaker {segment.Speaker}] {segment.Text}");
23 Console.WriteLine($" Time: {segment.Start:F2}s - {segment.End:F2}s");
24}
25
26// Real-time streaming transcription
27var stream = await client.STT.StreamTranscribeAsync(new StreamSTTRequest
28{
29 Language = "en",
30 Timestamps = true
31});
32
33// Send audio chunks in background task
34_ = Task.Run(async () =>
35{
36 using var fileStream = File.OpenRead("audio.mp3");
37 var buffer = new byte[4096];
38 int bytesRead;
39
40 while ((bytesRead = await fileStream.ReadAsync(buffer)) > 0)
41 {
42 await stream.SendAsync(buffer.AsMemory(0, bytesRead));
43 }
44
45 await stream.CompleteAsync();
46});
47
48// Receive transcripts
49await foreach (var result in stream.ReceiveAsync())
50{
51 Console.WriteLine($"Transcription: {result.Text} (final: {result.IsFinal})");
52}
Voice Agents
Build real-time conversational AI with bidirectional streaming:
1// Create a voice agent session
2var session = await client.VoiceAgent.CreateSessionAsync(new VoiceAgentConfig
3{
4 VoiceId = "rachel_v2",
5 Language = "en",
6 SystemPrompt = "You are a helpful assistant.",
7 Temperature = 0.7f,
8 MaxTokens = 150
9});
10
11// Send audio from microphone in background task
12_ = Task.Run(async () =>
13{
14 await foreach (var audioChunk in microphoneStream)
15 {
16 await session.SendAudioAsync(audioChunk);
17 }
18});
19
20// Receive agent responses
21await foreach (var response in session.ReceiveAsync())
22{
23 switch (response.Type)
24 {
25 case ResponseType.Transcript:
26 Console.WriteLine($"User: {response.Transcript.Text}");
27 break;
28
29 case ResponseType.AgentText:
30 Console.WriteLine($"Agent: {response.AgentText}");
31 break;
32
33 case ResponseType.Audio:
34 // Play audio chunk
35 PlayAudio(response.Audio);
36 break;
37 }
38}
Configuration Options
All available client configuration options:
| Property | Type | Default | Description |
|---|---|---|---|
| ApiKey | string | NUR_API_KEY | Your API key |
| BaseUrl | string | https://api.nur.ai | API base URL |
| Timeout | TimeSpan | 30s | Request timeout |
| MaxRetries | int | 3 | Maximum retry attempts |
| RetryDelay | TimeSpan | 1s | Delay between retries |
| HttpClient | HttpClient | Default | Custom HTTP client |
Error Handling
The SDK provides typed exceptions for different failure scenarios:
1using Nur.AI.Exceptions;
2
3try
4{
5 var audio = await client.TTS.GenerateAsync(new TTSRequest
6 {
7 Text = "Hello world",
8 VoiceId = "rachel_v2"
9 });
10}
11catch (RateLimitException ex)
12{
13 Console.WriteLine($"Rate limited. Retry after {ex.RetryAfter} seconds");
14 await Task.Delay(TimeSpan.FromSeconds(ex.RetryAfter));
15 // Retry request...
16}
17catch (AuthenticationException ex)
18{
19 Console.WriteLine("Authentication failed: check your API key");
20}
21catch (NurApiException ex)
22{
23 Console.WriteLine($"API error [{ex.StatusCode}]: {ex.Message}");
24}
25catch (HttpRequestException ex)
26{
27 Console.WriteLine($"Network error: {ex.Message}");
28}
29
30// Using cancellation tokens
31using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
32
33try
34{
35 var audio = await client.TTS.GenerateAsync(
36 new TTSRequest { Text = "Long text..." },
37 cts.Token
38 );
39}
40catch (OperationCanceledException)
41{
42 Console.WriteLine("Request timed out or was cancelled");
43}