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

Overview

The Camb.ai PHP 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 Camb.ai repository and package to your composer.json:
{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/Camb-ai/cambai-php-sdk.git"
    }
  ],
  "require": {
    "camb-ai/cambai-php-sdk": "*@dev"
  }
}
Then run:
composer install

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:
<?php
require_once __DIR__ . '/vendor/autoload.php';

use Camb\Ai\CambAIClient;
use Camb\Ai\Model\CreateStreamTTSRequestPayload;
use Camb\Ai\Model\Languages;

$client = new CambAIClient(getenv('CAMB_API_KEY'));

$payload = new CreateStreamTTSRequestPayload();
$payload->setText("Hello! Welcome to Camb.ai text-to-speech.");
$payload->setVoiceId(147320);
$payload->setLanguage(Languages::EN_US);
$payload->setSpeechModel("mars-flash");

// Stream the audio
$audioStream = $client->tts()->ttsTtsStreamPost($payload);

// Save to file
file_put_contents("output.wav", $audioStream);
echo "Audio saved to output.wav\n";

Using the Helper Function

You can easily wrap the file saving into a helper function:
function saveStreamToFile($stream, $filename) {
    return file_put_contents($filename, $stream);
}

// Usage:
$stream = $client->tts()->ttsTtsStreamPost($payload);
saveStreamToFile($stream, "output.wav");

Choosing a Model

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

MARS Flash

$payload->setSpeechModel("mars-flash");
$stream = $client->tts()->ttsTtsStreamPost($payload);
Best for: Real-time voice agents, low-latency applications
Sample rate: 22.05kHz

MARS Pro

$payload->setSpeechModel("mars-pro");
$stream = $client->tts()->ttsTtsStreamPost($payload);
Best for: Audio production, high-quality content
Sample rate: 48kHz

MARS Instruct

$payload->setSpeechModel("mars-instruct");
$payload->setUserInstructions("Speak in a warm, friendly tone");
$stream = $client->tts()->ttsTtsStreamPost($payload);
Best for: Fine-grained control over tone and style
Sample rate: 22.05kHz

Listing Available Voices

Discover available voices for your application:
$voices = $client->voiceCloning()->listVoicesListVoicesGet();

foreach ($voices as $voice) {
    echo "ID: " . $voice->getId() . ", Name: " . $voice->getVoiceName() . "\n";
}

Language Support

Camb.ai supports 140+ languages. Specify the language using the Languages constants: Languages supported by each model mentioned at MARS Models.
use Camb\Ai\Model\Languages;

// English (US)
$payload->setLanguage(Languages::EN_US);

// Spanish
$payload->setLanguage(Languages::ES_ES);

// French
$payload->setLanguage(Languages::FR_FR);

Error Handling

Handle common errors gracefully:
try {
    $stream = $client->tts()->ttsTtsStreamPost($payload);
} catch (Exception $e) {
    echo "Error generating speech: " . $e->getMessage() . "\n";
}

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
require_once 'examples/BasetenTtsProvider.php';

$basetenProvider = new BasetenTtsProvider("YOUR_BASETEN_API_KEY", "YOUR_BASETEN_URL");
$client->setTtsProvider($basetenProvider);

Next Steps

Resources