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

Overview

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

Add the following to your Cargo.toml:
[dependencies]
camb_api = { git = "https://github.com/Camb-ai/cambai-rust-sdk" }
tokio = { version = "1.0", features = ["full"] }
futures = "0.3"

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 uses tokio for async and returns a stream of bytes:
use camb_api::prelude::*;
use std::io::Write;
use futures::stream::TryStreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the client
    let client = APIClient::new(ClientConfig {
        api_key: Some(std::env::var("CAMB_API_KEY")?),
        ..ClientConfig::default()
    })?;

    // Stream TTS audio
    let mut stream = client.text_to_speech.tts(&CreateStreamTtsRequestPayload {
        text: "Hello! Welcome to Camb.ai text-to-speech.".to_string(),
        language: CreateStreamTtsRequestPayloadLanguage::EnUs,
        voice_id: 147320,
        speech_model: Some(CreateStreamTtsRequestPayloadSpeechModel::MarsFlash),
        output_configuration: Some(StreamTtsOutputConfiguration {
            format: Some(OutputFormat::Wav),
            ..Default::default()
        }),
        ..Default::default()
    }, None).await?;

    // Save to file
    let mut file = std::fs::File::create("output.wav")?;
    while let Some(chunk) = stream.try_next().await? {
        file.write_all(&chunk)?;
    }

    println!("Success! Audio saved to output.wav");
    Ok(())
}

Using the Helper Function

You can easily wrap the stream processor into a helper function:
use futures::stream::TryStreamExt;
use std::io::Write;

async fn save_stream_to_file<S, E>(mut stream: S, path: &str) -> Result<(), Box<dyn std::error::Error>>
where
    S: futures::Stream<Item = Result<Vec<u8>, E>> + Unpin,
    E: std::error::Error + Send + Sync + 'static,
{
    let mut file = std::fs::File::create(path)?;
    while let Some(chunk) = stream.try_next().await? {
        file.write_all(&chunk)?;
    }
    Ok(())
}

// Usage:
let stream = client.text_to_speech.tts(&payload, None).await?;
save_stream_to_file(stream, "output.wav").await?;

Choosing a Model

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

MARS Flash

speech_model: Some(CreateStreamTtsRequestPayloadSpeechModel::MarsFlash)
Best for: Real-time voice agents, low-latency applications
Sample rate: 22.05kHz

MARS Pro

speech_model: Some(CreateStreamTtsRequestPayloadSpeechModel::MarsPro)
Best for: Audio production, high-quality content
Sample rate: 48kHz

MARS Instruct

speech_model: Some(CreateStreamTtsRequestPayloadSpeechModel::MarsInstruct),
user_instructions: Some("Speak in a warm, friendly tone".to_string()),
Best for: Fine-grained control over tone and style
Sample rate: 22.05kHz

Listing Available Voices

Discover available voices for your application:
let voices = client.voice_cloning.list_voices(&ListVoicesListVoicesGetRequest::default(), None).await?;

for voice in voices {
    println!("ID: {}, Name: {}, Gender: {:?}", voice.id, voice.voice_name, 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:
match client.text_to_speech.tts(&payload, None).await {
    Ok(stream) => { /* Process stream */ },
    Err(e) => println!("Error generating speech: {}", e),
}

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
use camb_api::provider::BasetenProvider;

let tts_provider = BasetenProvider::new(
    "YOUR_BASETEN_API_KEY".to_string(),
    Some("YOUR_BASETEN_URL".to_string()),
);

Next Steps

Resources