Skip to main content

Overview

The Telnyx Text-to-Speech (TTS) WebSocket API provides real-time audio synthesis from text input. This streaming endpoint allows you to send text and receive synthesized audio incrementally, enabling low-latency voice generation for real-time applications.

WebSocket endpoint

Connection URL

Query parameters

Authentication

Include your Telnyx API key as an Authorization header in the connection request:

Example connection

Connection flow

The TTS WebSocket follows this lifecycle:
  1. Connect - Establish WebSocket connection with authentication.
  2. Initialize - Send initialization frame with space character.
  3. Send Text - Send one or more text frames to synthesize.
  4. Receive Audio - Receive audio frames with base64-encoded mp3 data.
  5. Stop - Send empty text frame to signal completion.
  6. Close - Connection closes after processing completes.

Flow diagram

Frame types

Outbound frames (Client → Server)

All outbound frames are JSON text messages with the following structure:

1. Initialization frame

Purpose: Initialize the TTS session Format:
Example:
Notes:
  • Must be sent first after connection.
  • Contains a single space character.
  • Required to begin the session.

2. Text frame

Purpose: Send text content to be synthesized into speech Format:
Example:
Multiple Text Frames:
Notes:
  • Can send multiple text frames in one session.
  • Each frame is processed and synthesized separately.
  • Audio is returned incrementally for each text frame.

3. Stop frame

Purpose: Signal completion of text input and end the session Format:
Example:
Notes:
  • Contains an empty string.
  • Signals the server to finish processing.
  • Should be sent after all text frames.

Inbound frames (Server → Client)

The server sends JSON text messages containing synthesized audio data.

Audio frame

Purpose: Deliver synthesized audio data Format:
Processing Audio:
Audio Specifications: Notes:
  • Multiple audio frames may be received for a single text input.
  • Each audio chunk is a complete mp3 file with headers.
  • Chunks should be concatenated in the order received.
  • Use append mode when saving to file to preserve all audio.

Complete example

Here’s a complete example showing all frame types in sequence:
Expected Output:

Configuration summary

Required configuration

Message sequence

Demo project

A complete Python implementation is available on GitHub.

Video demo

Watch this demonstration to see the Telnyx Text-to-Speech in action:

Troubleshooting

Additional resources