Skip to main content
For the lowest latency and most interactive applications, use the WebSocket API. It lets you stream text input and receive audio chunks in real time over a single persistent connection, which is ideal for chatbots, live assistants, and voice agents. For one-shot requests where you only need a stream of audio bytes back, see Streaming instead.
The WebSocket API is currently only available via raw WebSocket connections. SDK support coming soon.

Establish a connection

Connect to: wss://api.together.ai/v1/audio/speech/websocket

Authentication

  • Include your API key as a query parameter: ?api_key=<your_api_key>.
  • Or use the Authorization header when establishing the WebSocket connection.

Client-to-server messages

Append text to buffer

Appends text to the input buffer. Text is buffered until sentence completion or maximum length is reached.

Commit buffer

Forces processing of all buffered text. Use this at the end of your input stream.

Clear buffer

Clears all buffered text without processing (except text already being processed by the model).

Update session parameters

Updates TTS session settings like voice in real time. If no context_id is specified, all contexts are updated.

Server-to-client messages

Session created

Text received acknowledgment

Audio delta (streaming chunks)

Audio complete

Word timestamps

Sent when alignment=word is set. Contains word-level timing information for the generated audio.

TTS error

WebSocket example

WebSocket parameters

When establishing a WebSocket connection, you can configure:
You can pass these query parameters either in the WebSocket URL (e.g., wss://api.together.ai/v1/audio/speech/websocket?model=hexgrad/Kokoro-82M&voice=af_alloy&sample_rate=24000&alignment=word) or dynamically via the tts_session.updated event after the connection is established.

Multi-context support

You can manage multiple independent TTS streams over a single WebSocket connection using context_id. This is useful for applications handling multiple simultaneous conversations or characters.
  • Add context_id to any client message to route it to a specific context.
  • Messages without context_id use the "default" context.
  • Each context maintains its own text buffer and voice settings.
  • Cancel a specific context with the context.cancel message type.
  • Send tts_session.updated without a context_id to update all contexts at once.
  • Maximum 100 contexts per connection.
Sending text to a specific context:
Cancelling a context:
The server confirms cancellation with a context.cancelled message:

See also