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

Overview

The Camb.ai C# 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

Install the package via NuGet:
dotnet add package CambApi

Authentication

Get your API key from CAMB.AI Studio and set it as an environment variable:
export CAMB_API_KEY=your_api_key_here

Quick Start

Streaming Text-to-Speech

Generate and stream speech in real-time. The SDK returns a Stream for the audio data:
using CambApi;

// Initialize the client
var client = new CambApiClient(Environment.GetEnvironmentVariable("CAMB_API_KEY"));

// Stream TTS audio
var responseStream = await client.TextToSpeech.TtsAsync(new CreateStreamTtsRequestPayload
{
    Text = "Hello! Welcome to Camb.ai text-to-speech.",
    Language = CreateStreamTtsRequestPayloadLanguage.EnUs,
    VoiceId = 147320,
    SpeechModel = CreateStreamTtsRequestPayloadSpeechModel.MarsFlash,
    OutputConfiguration = new StreamTtsOutputConfiguration
    {
        Format = OutputFormat.Wav
    }
});

// Save to file
using var fileStream = File.Create("output.wav");
await responseStream.CopyToAsync(fileStream);

Console.WriteLine("Success! Audio saved to output.wav");

Using the Helper Function

You can easily wrap the stream saving into a helper method:
public static async Task SaveStreamToFileAsync(Stream stream, string filename)
{
    using var fileStream = File.Create(filename);
    await stream.CopyToAsync(fileStream);
}

// Usage:
var stream = await client.TextToSpeech.TtsAsync(payload);
await SaveStreamToFileAsync(stream, "output.wav");

Choosing a Model

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

MARS Flash

SpeechModel = CreateStreamTtsRequestPayloadSpeechModel.MarsFlash
Best for: Real-time voice agents, low-latency applications
Sample rate: 22.05kHz

MARS Pro

SpeechModel = CreateStreamTtsRequestPayloadSpeechModel.MarsPro
Best for: Audio production, high-quality content
Sample rate: 48kHz

MARS Instruct

SpeechModel = CreateStreamTtsRequestPayloadSpeechModel.MarsInstruct,
UserInstructions = "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:
var voices = await client.VoiceCloning.ListVoicesAsync();

foreach (var voice in voices)
{
    Console.WriteLine($"ID: {voice.Id}, Name: {voice.VoiceName}, Gender: {voice.Gender}");
}

Language Support

Camb.ai supports 140+ languages. Specify the language using the CreateStreamTtsRequestPayloadLanguage enum: Languages supported by each model mentioned at MARS Models.
// English (US)
Language = CreateStreamTtsRequestPayloadLanguage.EnUs

// Spanish
Language = CreateStreamTtsRequestPayloadLanguage.EsEs

// French
Language = CreateStreamTtsRequestPayloadLanguage.FrFr

Error Handling

Handle common errors gracefully:
try 
{
    var stream = await client.TextToSpeech.TtsAsync(payload);
}
catch (Exception ex)
{
    Console.WriteLine($"Error generating speech: {ex.Message}");
}

Using Custom Provider

For more details check this guide Custom Cloud Providers

Baseten Deployment

Initialize the client with your custom provider implementation. Baseten Provider Example
using CambApi.Providers;

var ttsProvider = new BasetenProvider(
    apiKey: "YOUR_BASETEN_API_KEY",
    url: "YOUR_BASETEN_URL"
);

Next Steps

Resources