Skip to main content

TTS WebSocket

Real-time Text-to-Speech synthesis via WebSocket streaming with full-duplex bidirectional communication.

Endpoint

Authentication

Query parameter authentication: Example:

Protocol Overview

Connection Sequence

1. Connect

2. Receive Authentication Message

3. Receive Connection Established

Fields:
string
Service name: "tts"
integer
Your user ID
number
Available credits
string
Workspace name

Client → Server Messages

1. create_context

Must be the first message. Initializes the TTS session with voice and audio settings.
Parameters: Supported encoding + sample rate combinations: Not all combinations are valid. The table below shows which pairs are supported. Unsupported combinations silently fall back to LINEAR16 at 16000 Hz.
Note: MULAW/ULAW only works at 8000 Hz. Using other sample rates with MULAW falls back to LINEAR16 @ 16kHz. Similarly, OGG_OPUS only works at 24000 Hz.
Limits:

2. send_text

Append text to the internal buffer. Text is accumulated until a flush_context or close_context is received.
Fields: You can send multiple send_text messages to build up text incrementally (e.g., from an LLM token stream):
Text chunking behavior: Long text is automatically split into sentence-based chunks for reliable synthesis. The model works best with 3–30 second utterances. The server handles:
  • Sentence boundary detection for natural chunk splits
  • Newline characters (\n) are treated as hard paragraph boundaries
  • Mixed-language text (e.g., English + Hindi) is chunked per paragraph to prevent early EOS

3. flush_context

Triggers synthesis of all accumulated text. The server responds with audio_chunk messages followed by flush_completed (only on success — if synthesis fails, an error message is sent instead with no flush_completed).

4. close_context

Flushes any remaining text, sends final audio, and closes the WebSocket connection.

Server → Client Messages

context_created

Confirms the session was initialized successfully.

audio_chunk

Contains a chunk of synthesized audio. Multiple chunks are sent per flush. Each chunk is streamed as soon as it’s decoded for minimum latency.
Fields:
string
Session identifier
string
Base64-encoded audio bytes
The audio encoding and chunk format depend on audio_config:

flush_completed

Signals that all audio for the flushed text has been sent. Only sent on successful synthesis — if synthesis fails, an error is sent instead.

context_closed

Confirms the session is closed. The WebSocket connection closes after this message.

error

Sent if synthesis fails or a protocol violation occurs.
Common errors:

Complete Example

    Real-time Playback (Browser)

    For low-latency playback as chunks arrive (instead of waiting for all chunks), use the Web Audio API with scheduled AudioBufferSourceNode:

    LLM Integration Pattern

    When streaming tokens from an LLM into TTS:

    Audio Format Notes

    For telephony integration (Twilio, etc.), use MULAW at 8000 Hz:
    For web playback with low bandwidth, use OGG_OPUS at 24000 Hz:
    Important: OGG_OPUS chunks are individually wrapped OGG files. To merge for download, decode each chunk independently (e.g., via AudioContext.decodeAudioData()) and concatenate the PCM output. Do not concatenate raw OGG bytes.

    Supported Languages

    The TTS model supports synthesis in multiple Indic languages and English. The language is auto-detected from the input text — no explicit language parameter is needed. Mixed-language text is supported. Use newlines (\n) to separate paragraphs in different languages for best results.

    Default Voice

    The default voice ID is:
    To get more voices, use the Voices API.

    Context Management

    Reuse Context

    Keep a context open for multiple syntheses:

    Multiple Contexts

    You can create multiple contexts in one connection:

    Pricing

    • Rate: $0.00002 per character
    • Minimum: $0.01 per context
    • Billing: Per character synthesized

    Error Codes

    Testing

    Then send: