Text to Speech (WebSocket)
Many streaming synthesis requests on one connection: the lowest-latency way to use Bakbak TTS.
wss://hub.getraya.app/v1/text-to-speech/ws carries many streaming synthesis
requests on one connection. Each request behaves exactly like
POST /v1/text-to-speech/stream:
the same fields, validation, billing and limits. The audio comes back as JSON
messages carrying the same payloads as the stream’s events.
Why use it: an HTTP request pays the connection set-up and the API gateway’s checks every time. A WebSocket pays them once, when it opens. On our measurements that took about 10 ms off time to first audio per request, before counting the network round trips a new HTTP connection costs from far away.
The official Python and JavaScript SDKs and the LiveKit plugin use this endpoint for you, with reconnects, ordering and cancellation handled.
Connect
- URL:
wss://hub.getraya.app/v1/text-to-speech/ws - Authentication: the
X-API-Keyheader. Browsers cannot set headers on a WebSocket, so the key is also accepted as a query parameter:?X-API-Key=<key>. Prefer the header everywhere else.
A wrong key is refused before the upgrade with HTTP 401.
Send requests
Send each request as one JSON text message: the body of the streaming
endpoint, plus an optional context_id (up to 128 characters of
A-Z a-z 0-9 . _ -).
Ordering. Requests with the same context_id run one after another, in
the order you sent them, so a reply sent sentence by sentence plays in order.
Requests in different contexts run at the same time and their messages
interleave; tell them apart by context_id. Requests without a context_id
share one implicit context.
A request queued behind its context holds no capacity and is not billed until it starts.
Receive audio
Every message has a type and a done flag. A request produces zero or more
chunk messages, then exactly one done or error:
data is base64 audio in the request’s output_format (32-bit float PCM at
24 kHz without one). request_id identifies the request to support.
Cancel
stops the context’s running request and drops the ones queued behind it, for
example when the caller interrupts. You get exactly one
{"type":"cancelled","done":true,"context_id":"turn-7"}, and nothing sent before
the cancel arrives after it: discard the context’s audio until you see it. A
request sent after the cancel on the same context runs normally.
A request cancelled after its first audio is billed like a delivered request; one cancelled before any audio is not.
Errors
status_code is the HTTP status the same failure gets on the streaming
endpoint, with that endpoint’s error fields. An error
ends that request only: the connection stays open, other contexts carry on,
and later requests on the same context still run. A refusal keeps its place in
its context’s order.
An error with neither context_id nor request_id is about a message that
was not a request, such as a binary frame.
Limits
The server pings every 30 seconds. Browsers answer automatically; other clients must keep reading the socket so their library answers too.
Close codes
While a connection is closing for a restart, a new request is refused with a
503 error asking you to reconnect and send it again. However a connection
ends, a request without its done or error did not complete: resend it on a
new connection, unless you already received some of its audio.

