Skip to content
SDK

Go SDK

Official Go client library for Nur's Voice AI platform. Build production-ready voice applications with idiomatic Go.

Installation

Requires Go 1.21 or higher. Install using go get:

1go get github.com/nur-ai/nur-go

Quick Start

Create a client and generate your first speech in seconds:

1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 nur "github.com/nur-ai/nur-go"
9)
10
11func main() {
12 // Initialize client (uses NUR_API_KEY env var)
13 client := nur.NewClient()
14
15 // Generate speech from text
16 audio, err := client.TTS.Generate(context.Background(), &nur.TTSRequest{
17 Text: "Hello from Go! This is Nur's voice AI.",
18 VoiceID: "rachel_v2",
19 Language: "en",
20 })
21 if err != nil {
22 log.Fatal(err)
23 }
24
25 // Save to file
26 if err := audio.Save("output.mp3"); err != nil {
27 log.Fatal(err)
28 }
29
30 fmt.Printf("Generated %.2fs of audio\n", audio.Duration)
31}

Authentication

The SDK automatically reads your API key from the NUR_API_KEY environment variable. You can also pass it explicitly:

1package main
2
3import nur "github.com/nur-ai/nur-go"
4
5func main() {
6 // Option 1: Use environment variable
7 client := nur.NewClient()
8
9 // Option 2: Explicit API key
10 client := nur.NewClient(nur.WithAPIKey("nur_your_api_key_here"))
11
12 // Option 3: Custom configuration
13 client := nur.NewClient(
14 nur.WithAPIKey("nur_your_api_key_here"),
15 nur.WithBaseURL("https://api.nur.ai"),
16 nur.WithTimeout(60 * time.Second),
17 nur.WithMaxRetries(3),
18 )
19}

Text to Speech

Generate natural-sounding speech from text with customizable voices and settings:

1// Basic generation
2audio, err := client.TTS.Generate(ctx, &nur.TTSRequest{
3 Text: "Converting text to natural speech.",
4 VoiceID: "rachel_v2",
5})
6
7// With advanced options
8audio, err := client.TTS.Generate(ctx, &nur.TTSRequest{
9 Text: "Converting text to natural speech.",
10 VoiceID: "rachel_v2",
11 Language: "en",
12 Speed: 1.0,
13 Pitch: 1.0,
14 OutputFormat: "mp3",
15 SampleRate: 24000,
16 EnableSSML: true,
17})
18
19// Save audio
20err = audio.Save("output.mp3")
21
22// Get audio bytes
23bytes := audio.Bytes()
24
25// Streaming generation
26stream, err := client.TTS.Stream(ctx, &nur.TTSRequest{
27 Text: "Streaming long-form content...",
28 VoiceID: "rachel_v2",
29})
30if err != nil {
31 log.Fatal(err)
32}
33defer stream.Close()
34
35for {
36 chunk, err := stream.Next()
37 if err == io.EOF {
38 break
39 }
40 if err != nil {
41 log.Fatal(err)
42 }
43 // Process chunk
44 fmt.Printf("Received %d bytes\n", len(chunk))
45}

Speech to Text

Transcribe audio files with high accuracy and optional speaker diarization:

1// Basic transcription
2transcript, err := client.STT.Transcribe(ctx, &nur.STTRequest{
3 File: "audio.mp3",
4})
5fmt.Println(transcript.Text)
6
7// With speaker diarization and timestamps
8transcript, err := client.STT.Transcribe(ctx, &nur.STTRequest{
9 File: "meeting.mp3",
10 Language: "en",
11 SpeakerDiarization: true,
12 Timestamps: true,
13 PunctuationEnabled: true,
14 FilterProfanity: true,
15})
16
17// Access segments with speaker info
18for _, segment := range transcript.Segments {
19 fmt.Printf("[Speaker %s] %s\n", segment.Speaker, segment.Text)
20 fmt.Printf(" Time: %.2fs - %.2fs\n", segment.Start, segment.End)
21}
22
23// Real-time streaming transcription
24stream, err := client.STT.StreamTranscribe(ctx, &nur.StreamSTTRequest{
25 Language: "en",
26 Timestamps: true,
27})
28if err != nil {
29 log.Fatal(err)
30}
31defer stream.Close()
32
33// Send audio chunks
34go func() {
35 for audioChunk := range audioSource {
36 if err := stream.Send(audioChunk); err != nil {
37 log.Fatal(err)
38 }
39 }
40 stream.CloseSend()
41}()
42
43// Receive transcripts
44for {
45 result, err := stream.Recv()
46 if err == io.EOF {
47 break
48 }
49 if err != nil {
50 log.Fatal(err)
51 }
52 fmt.Printf("Transcription: %s (final: %v)\n", result.Text, result.IsFinal)
53}

Voice Agents

Build real-time conversational AI agents with bidirectional streaming:

1// Create a voice agent session
2session, err := client.VoiceAgent.CreateSession(ctx, &nur.VoiceAgentConfig{
3 VoiceID: "rachel_v2",
4 Language: "en",
5 SystemPrompt: "You are a helpful assistant.",
6 Temperature: 0.7,
7 MaxTokens: 150,
8})
9if err != nil {
10 log.Fatal(err)
11}
12defer session.Close()
13
14// Handle bidirectional streaming
15errChan := make(chan error)
16
17// Send audio from microphone
18go func() {
19 for audioChunk := range microphoneStream {
20 if err := session.SendAudio(audioChunk); err != nil {
21 errChan <- err
22 return
23 }
24 }
25}()
26
27// Receive agent responses
28go func() {
29 for {
30 response, err := session.Receive()
31 if err == io.EOF {
32 break
33 }
34 if err != nil {
35 errChan <- err
36 return
37 }
38
39 // Handle different response types
40 switch response.Type {
41 case nur.ResponseTypeTranscript:
42 fmt.Printf("User: %s\n", response.Transcript.Text)
43 case nur.ResponseTypeAgentText:
44 fmt.Printf("Agent: %s\n", response.AgentText)
45 case nur.ResponseTypeAudio:
46 // Play audio chunk
47 playAudio(response.Audio)
48 }
49 }
50}()
51
52// Wait for completion or error
53select {
54case err := <-errChan:
55 log.Fatal(err)
56case <-ctx.Done():
57 fmt.Println("Session ended")
58}

Configuration Options

All available client configuration options:

OptionTypeDefaultDescription
WithAPIKeystringNUR_API_KEYYour API key
WithBaseURLstringhttps://api.nur.aiAPI base URL
WithTimeouttime.Duration30sRequest timeout
WithMaxRetriesint3Maximum retry attempts
WithRetryDelaytime.Duration1sDelay between retries
WithHTTPClient*http.Clienthttp.DefaultClientCustom HTTP client

Error Handling

The SDK provides typed errors for different failure scenarios:

1import (
2 "errors"
3 nur "github.com/nur-ai/nur-go"
4)
5
6audio, err := client.TTS.Generate(ctx, &nur.TTSRequest{
7 Text: "Hello world",
8 VoiceID: "rachel_v2",
9})
10
11if err != nil {
12 var apiErr *nur.APIError
13 var rateLimitErr *nur.RateLimitError
14 var authErr *nur.AuthenticationError
15
16 switch {
17 case errors.As(err, &rateLimitErr):
18 fmt.Printf("Rate limited. Retry after %d seconds\n", rateLimitErr.RetryAfter)
19 time.Sleep(time.Duration(rateLimitErr.RetryAfter) * time.Second)
20 // Retry request...
21
22 case errors.As(err, &authErr):
23 log.Fatal("Authentication failed: check your API key")
24
25 case errors.As(err, &apiErr):
26 fmt.Printf("API error [%d]: %s\n", apiErr.StatusCode, apiErr.Message)
27
28 default:
29 log.Fatal("Unexpected error:", err)
30 }
31}
32
33// Context timeout handling
34ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
35defer cancel()
36
37audio, err := client.TTS.Generate(ctx, &nur.TTSRequest{
38 Text: "Long text...",
39})
40if errors.Is(err, context.DeadlineExceeded) {
41 fmt.Println("Request timed out")
42}

Need Help?