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

Overview

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

Gradle

Add the dependency to your build.gradle:
dependencies {
    implementation 'ai.camb:cambai-java-sdk:1.5.9'
}

Maven

Add the dependency to your pom.xml:
<dependency>
    <groupId>ai.camb</groupId>
    <artifactId>cambai-java-sdk</artifactId>
    <version>1.5.9</version>
</dependency>

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 an InputStream for the audio data:
import CambApiClient;
import resources.texttospeech.requests.CreateStreamTtsRequestPayload;
import resources.texttospeech.types.CreateStreamTtsRequestPayloadLanguage;
import resources.texttospeech.types.CreateStreamTtsRequestPayloadSpeechModel;
import types.OutputFormat;
import types.StreamTtsOutputConfiguration;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        // Initialize the client
        CambApiClient client = CambApiClient.builder()
            .apiKey(System.getenv("CAMB_API_KEY"))
            .build();

        // Stream TTS audio
        InputStream audioStream = client.textToSpeech().tts(CreateStreamTtsRequestPayload.builder()
            .text("Hello! Welcome to Camb.ai text-to-speech.")
            .language(CreateStreamTtsRequestPayloadLanguage.EN_US)
            .voiceId(147320)
            .speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARSFLASH)
            .outputConfiguration(StreamTtsOutputConfiguration.builder().format(OutputFormat.WAV).build())
            .build());

        // Save to file
        File outputFile = new File("output.wav");
        try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = audioStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }

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

Using the Helper Function

You can easily wrap the stream saving into a helper method:
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public static void saveStreamToFile(InputStream stream, String filename) throws IOException {
    Files.copy(stream, Path.of(filename), StandardCopyOption.REPLACE_EXISTING);
}

// Usage:
InputStream stream = client.textToSpeech().tts(payload);
saveStreamToFile(stream, "output.wav");

Choosing a Model

Camb.ai offers three MARS models optimized for different use cases:
.speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARSFLASH)
// Best for: Real-time voice agents, low-latency applications
// Sample rate: 22.05kHz

Listing Available Voices

Discover available voices for your application:
var voices = client.voiceCloning().listVoices();

for (var voice : voices) {
    System.out.println("ID: " + voice.getId() + ", Name: " + voice.getVoiceName());
}

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.EN_US)

// Spanish
.language(CreateStreamTtsRequestPayloadLanguage.ES_ES)

// French
.language(CreateStreamTtsRequestPayloadLanguage.FR_FR)

Error Handling

Handle common errors gracefully:
try {
    InputStream stream = client.textToSpeech().tts(payload);
} catch (Exception e) {
    System.err.println("Error generating speech: " + e.getMessage());
}

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
// Initialize the Baseten Mars8-Flash custom hosting provider.
// referenceAudio can be a public URL or base64-encoded audio file.
ITtsProvider basetenProvider = new BasetenProvider(
    "YOUR_BASETEN_API_KEY",
    "YOUR_BASETEN_URL",
    "YOUR_REFERENCE_AUDIO",  // public URL or base64-encoded audio file
    "en-us"                  // reference audio language
);

// Build the TTS request
CreateStreamTtsRequestPayload request = CreateStreamTtsRequestPayload.builder()
    .text("Hello from Java via Baseten Mars8-Flash!")
    .language(CreateStreamTtsRequestPayloadLanguage.EN_US)
    .voiceId(1) // Required by the SDK's staged builder; ignored by the Baseten provider
    .build();

InputStream audioStream = basetenProvider.tts(request, null);

Next Steps

https://mintcdn.com/cambai/2LvnefIkletroPxv/images/pipecat-orange.svg?fit=max&auto=format&n=2LvnefIkletroPxv&q=85&s=40cf8e001b8cadc8a4c3c557dea603d5

Voice Agents

Build real-time voice agents with Pipecat
https://mintcdn.com/cambai/2LvnefIkletroPxv/images/livekit-orange.svg?fit=max&auto=format&n=2LvnefIkletroPxv&q=85&s=c750fcee9b1de69e3c1d0d6ec7eb6b3f

LiveKit Integration

Create voice agents with LiveKit

API Reference

Explore the full TTS API

Voice Library

Browse available voices

Resources