Skip to main content
Get started with the Camb.ai Go SDK in minutes

Overview

The Camb.ai Go SDK provides a simple interface to integrate high-quality text-to-speech into your applications. This quickstart will have you generating speech in under 5 minutes.

Installation

go get github.com/camb-ai/cambai-go-sdk

Authentication

Get your API key from CAMB.AI Studio and set it as an environment variable:
export CAMB_API_KEY=your_api_key_here
Or use a .env file or load it directly in your code.

Quick Start

Streaming Text-to-Speech

Generate and stream speech in real-time:
package main

import (
	"context"
	"io"
	"os"

	"github.com/camb-ai/cambai-go-sdk"
	"github.com/camb-ai/cambai-go-sdk/client"
	"github.com/camb-ai/cambai-go-sdk/option"
)

func main() {
	// Initialize the client
	c := client.NewClient(
		option.WithAPIKey(os.Getenv("CAMB_API_KEY")),
	)

	// Stream TTS audio
	stream, err := c.TextToSpeech.Tts(
		context.Background(),
		&cambai.CreateStreamTtsRequestPayload{
			Text:     "Hello! Welcome to Camb.ai text-to-speech.",
			Language: cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
			VoiceID:  147320,
			SpeechModel: cambai.CreateStreamTtsRequestPayloadSpeechModelMars8Flash.Ptr(),
			OutputConfiguration: &cambai.StreamTtsOutputConfiguration{
				Format: cambai.OutputFormatWav.Ptr(),
			},
		},
	)
	if err != nil {
		panic(err)
	}

	// Save to file
	outFile, _ := os.Create("output.wav")
	defer outFile.Close()
	io.Copy(outFile, stream)
}

Using the Helper Function

Since the SDK returns an io.Reader, you can use this helper to save streams easily:
func SaveStreamToFile(stream io.Reader, filename string) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = io.Copy(f, stream)
	return err
}

// Usage:
stream, _ := client.TextToSpeech.Tts(...)
SaveStreamToFile(stream, "output.wav")

Choosing a Model

Camb.ai offers three MARS models optimized for different use cases:

MARS Flash

stream, _ := c.TextToSpeech.Tts(ctx, &cambai.CreateStreamTtsRequestPayload{
    Text:        "Fast and efficient speech synthesis.",
    Language:    cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
    VoiceID:     147320,
    SpeechModel: cambai.CreateStreamTtsRequestPayloadSpeechModelMars8Flash.Ptr(),
})
Best for: Real-time voice agents, low-latency applications
Sample rate: 22.05kHz

MARS Pro

stream, _ := c.TextToSpeech.Tts(ctx, &cambai.CreateStreamTtsRequestPayload{
    Text:        "High-fidelity audio production.",
    Language:    cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
    VoiceID:     147320,
    SpeechModel: cambai.CreateStreamTtsRequestPayloadSpeechModelMars8.Ptr(),
})
Best for: Audio production, high-quality content
Sample rate: 48kHz

MARS Instruct

stream, _ := c.TextToSpeech.Tts(ctx, &cambai.CreateStreamTtsRequestPayload{
    Text:             "Great to meet you!",
    Language:         cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
    VoiceID:          147320,
    SpeechModel:      cambai.CreateStreamTtsRequestPayloadSpeechModelMars8Instruct.Ptr(),
    UserInstructions: cambai.String("Speak in a warm, friendly tone"),
})
Best for: Fine-grained control over tone and style
Sample rate: 22.05kHz

Listing Available Voices

Discover available voices for your application:
voices, _ := c.VoiceCloning.ListVoices(
    context.Background(), 
    &cambai.ListVoicesListVoicesGetRequest{},
)

for _, voice := range voices {
    fmt.Printf("ID: %d, Name: %s, Gender: %v\n", voice.ID, voice.VoiceName, voice.Gender)
}

Language Support

Camb.ai supports 140+ languages. Specify the language using the provided constants: Languages supported by each model mentioned at MARS Models.
// English (US)
c.TextToSpeech.Tts(ctx, &cambai.CreateStreamTtsRequestPayload{
    Language: cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
    // ...
})

// Spanish
c.TextToSpeech.Tts(ctx, &cambai.CreateStreamTtsRequestPayload{
    Language: cambai.CreateStreamTtsRequestPayloadLanguageEsEs,
    // ...
})

Error Handling

Handle common errors gracefully:
stream, err := c.TextToSpeech.Tts(ctx, request)
if err != nil {
    fmt.Printf("Error generating speech: %v\n", err)
    return
}

Using Custom Provider

For more details check this guide Custom Cloud Providers

Baseten Deployment

Initialize the client with your API key and the specific model URL. Baseten Provider Example
var ttsProvider provider.TtsProvider = &BasetenProvider{
	APIKey: "your_api_key_here",
	URL:    "your_model_url_here",
}

req := &cambai.CreateStreamTtsRequestPayload{
	Text:     "Hello from Go Custom Provider with Context options!",
	Language: cambai.CreateStreamTtsRequestPayloadLanguageEnUs,
}

stream, err := ttsProvider.TextToSpeech.Tts(ctx, req)
if err != nil {
	fmt.Printf("Error generating speech: %v\n", err)
	return
}

Next Steps

Resources