> ## Documentation Index
> Fetch the complete documentation index at: https://docs.camb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Browser SDK Quickstart

Get started with the Camb.ai Browser SDK in minutes

## Overview

The Camb.ai Browser SDK is designed for client-side applications, providing a simple, type-safe interface to integrate high-quality text-to-speech directly into your web apps.

## Installation

```bash theme={null}
npm install @camb-ai/browser-sdk
```

## Authentication

> **\[!CAUTION] Security Warning**: Hardcoding your API key in client-side code (HTML/JS) is **not secure**. Anyone visiting your site can inspect the source code and steal your key, which can lead to unauthorized usage and charges.

### Recommended: Backend Proxy

For production applications, it is **highly recommended** to route requests through a secure backend proxy:

1. Store your Camb.ai API key securely on your server (e.g., in environment variables).
2. Set up an endpoint on your server that forwards requests to Camb.ai and adds the `x-api-key` header.
3. Implement authentication on your server to control who can use the TTS services.

Initialize the client to point to your proxy URL:

```typescript theme={null}
import { CambClient } from "@camb-ai/browser-sdk";

const client = new CambClient({
  baseUrl: "https://your-backend.com/api/camb-ai-proxy",
  apiKey: "PROXY_MANAGED", // Current SDK requirement - use a placeholder
});
```

### Development Only (Direct Client)

Use the API key directly **only for local development** or prototyping:

```typescript theme={null}
import { CambClient } from "@camb-ai/browser-sdk";

const client = new CambClient({
  apiKey: "your_api_key_here",
});
```

## Quick Start

### Streaming Text-to-Speech

Generate and play speech directly in the browser: (Assumes `client` is initialized as shown in the [Authentication](#authentication) section).

```typescript theme={null}
import { Languages } from "@camb-ai/browser-sdk";

async function playSpeech() {
  const response = await client.textToSpeech.tts({
    text: "Hello! Welcome to Camb.ai text-to-speech.",
    language: Languages.EN_US,
    voice_id: 147320,
    speech_model: "mars-8.1-flash-beta",
  });

  // Response is a ReadableStream or Blob depending on fetch configuration
  const blob = await response.blob();
  const url = URL.createObjectURL(blob);
  const audio = new Audio(url);
  audio.play();
}
```

### Using the Helper Function

The SDK's `tts` method returns binary data that can be easily converted to a URL for playback:

```typescript theme={null}
async function streamAndPlay(payload) {
    const stream = await client.textToSpeech.tts(payload);
    const audioUrl = URL.createObjectURL(await stream.blob());
    new Audio(audioUrl).play();
}
```

## Choosing a Model

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

```typescript MARS Flash theme={null}
speech_model: "mars-flash"
// Best for: Real-time voice agents, low-latency applications
// Sample rate: 22.05kHz
```

```typescript MARS Pro theme={null}
speech_model: "mars-pro"
// Best for: Audio production, high-quality content
// Sample rate: 48kHz
```

```typescript MARS Instruct theme={null}
const payload = {
  text: "[warm, friendly] Great to meet you!",
  language: Languages.EN_US,
  voice_id: 147320,
  speech_model: "mars-8-instruct",
  user_instructions: "Speak in a warm, friendly tone",
  output_configuration: { format: "wav" },
};
// Best for: Fine-grained control over tone and style
// Sample rate: 22.05kHz
```

## TTS Options

`client.textToSpeech.tts(...)` accepts a payload with the core request fields plus optional controls for model behavior and output format.

| Option | Description |
| - | - |
| `text` | Text to synthesize. For instruct models, you can include inline emotion or pacing tags in the text. |
| `language` | Locale such as `Languages.EN_US`. |
| `voice_id` | Voice profile ID from `listVoices()`. |
| `speech_model` | Model to use, such as `mars-8-instruct`, `mars-pro`, or `mars-flash`. |
| `user_instructions` | Adds style, tone, pronunciation, or delivery guidance for the request. Available only with an instruct speech model. |
| `output_configuration` | Output settings such as `{ format: "wav" }`. |
| `voice_settings` | Voice behavior controls such as speaking rate, reference enhancement, or accent preservation. |
| `inference_options` | Advanced generation controls for supported models. |
| `enhance_named_entities_pronunciation` | Improves pronunciation for names and other named entities when supported. |

## Listing Available Voices

Discover available voices for your application:

```typescript theme={null}
const voices = await client.voiceCloning.listVoices();

voices.forEach(voice => {
    console.log(`ID: ${voice.id}, Name: ${voice.voiceName}`);
});
```

## Language Support

Camb.ai supports up to 312 languages depending on the MARS model — see [Language Support](https://docs.camb.ai/language-support.md) for the full per-model locale list. Specify the language using the `Languages` enum exports:

```typescript theme={null}
import { Languages } from "@camb-ai/browser-sdk";

// English (US)
language: Languages.EN_US

// Spanish
language: Languages.ES_ES

// French
language: Languages.FR_FR
```

## Using Custom Provider

For more details check this guide [Custom Cloud Providers](https://docs.camb.ai/custom-cloud-providers.md)

### Baseten Deployment

Initialize the client with provider configuration. [Baseten Provider Example](https://github.com/Camb-ai/cambai-browser-sdk/blob/master/examples/baseten_provider.ts)

```typescript theme={null}
const client = new CambClient({
    ttsProvider: "baseten",
    providerParams: {
        apiKey: "YOUR_BASETEN_API_KEY",
        marsProUrl: "YOUR_BASETEN_URL"
    }
});
```

***

## Next Steps

> [card] [Voice Agents](https://docs.camb.ai/integrations/pipecat.md)
>
> Build real-time voice agents with Pipecat

> [card] [LiveKit Integration](https://docs.camb.ai/integrations/livekit.md)
>
> Create voice agents with LiveKit

> [card] [API Reference](https://docs.camb.ai/api-reference/endpoint/create-tts-stream.md)
>
> Explore the full TTS API

> [card] [Voice Library](https://docs.camb.ai/api-reference/endpoint/list-voices.md)
>
> Browse available voices

## Resources

* [GitHub: camb-ai/cambai-browser-sdk](https://github.com/Camb-ai/cambai-browser-sdk)
* [SDK Examples](https://github.com/Camb-ai/cambai-browser-sdk/tree/master/examples)
* [API Reference](https://docs.camb.ai/api-reference/endpoint/create-tts-stream.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.