Skip to main content
For interactive, realtime use cases the Voice API exposes a WebSocket endpoint that bridges your connection to the synthesis engine. From a server, send your API key in the X-API-Key header, as on the REST endpoints. From a browser, authenticate with a short-lived access token (fin_tok_…) that your server mints with Create an access token, so your API key never reaches the browser.
Building on LiveKit Agents? The LiveKit plugin speaks this protocol for you, including cancelling the reply when the user interrupts.
Need the lowest latency per sentence? A direct realtime session speaks this same protocol straight to the synthesis GPU in ap-south or us-east, without this endpoint in between.

Authentication

1

Mint a token on your server

Call POST /tokens with your API key (scope tts). Optionally pass voice_ids to restrict which voices the session may use.
2

Open the socket with the token

Pass the token as the second subprotocol: new WebSocket(url, ["finesse.v1", token]). Browsers can’t set headers on a WebSocket handshake, so the subprotocol list carries it; the server selects and echoes finesse.v1.
If more than one is sent, the server uses the first it finds in this order: subprotocol token, query token, X-API-Key header, query API key. A token is single use: it is consumed by the handshake, and the session then lasts as long as the socket. A handshake refused before the upgrade (a 429 or a plan check) keeps the token. Open a new socket with a new token. A token grants tts only and inherits its key’s workspace; it stops working if that key is revoked or expires.
Don’t put your API key in a WebSocket URL. URLs end up in browser history, proxy logs and devtools, and a key there can be copied and reused. ?api_key= keeps working for now, but migrate to tokens.
A valid token (or key) upgrades the connection (101 Switching Protocols). An unknown, expired or already-used token fails the handshake with 401 (token_invalid); an expired key with 401 (key_expired); a key without the tts scope with 403 (insufficient_scope). If the token was minted with voice_ids, every task_start for another voice is answered with a non-fatal voice_not_allowed error frame; the rest of the session continues. The handshake can also fail with 402/403 (plan check, see Billing), 400 (the request isn’t a WebSocket upgrade), or 429: over your plan’s rate or concurrent-synthesis limit (rate_limited / concurrency_limited, with Retry-After), or 503 upstream_busy with Retry-After when the service is at capacity — retry after the given seconds. An open session holds one of your workspace’s concurrent stream slots until it closes. When every slot is in use, or the handshake is over your plan’s requests per second, it is refused before the upgrade with 429, a Retry-After header and the standard JSON error body (code is concurrency_limited or rate_limited). Handshake responses carry X-Request-Id, X-Billing-Remaining, RateLimit-Policy and RateLimit.

How the session is bridged

The service connects to the synthesis engine only after upgrading you; if the engine can’t be reached you receive a fatal upstream_unavailable error frame and the socket closes with 1013 — reconnect with backoff. On the JSON control frames you send, the service replaces voice_id with your enrolled engine voice for that id — so you always reference your own Flam voice ids (the 24-char ids from List voices), never the engine’s. Only JSON text frames of the four types below are forwarded; anything else (binary frames, invalid JSON, an unknown type) is dropped and answered with a non-fatal invalid_message error frame. Frames from the engine are forwarded to you verbatim.

Session model

One connection is one session; a session can run up to 4 concurrent contexts (independent synthesis streams), each keyed by a client-chosen context_id. You start a context, optionally keep appending text to it (ideal for LLM output arriving in chunks), and finalize it when the turn is done. Audio for each context streams back as it’s synthesized.
  • You send — JSON text frames. The type field dispatches the message (see below); other frames are refused with invalid_message.
  • You receive — JSON text frames (status/control) interleaved with binary frames (raw audio).
  • Limits — text frames over 1 MiB close the socket (1009); a session idle for 600 s is closed by the server (session_ended, reason idle); each open session holds one of the workspace’s concurrent stream slots.

Messages you send

task_start — begin a context

task_continue — append text to an open context

Send as many as you like; set finalize: true on the last one to flush and finish the context.

task_cancel — abort a context

session_end — graceful close

The server replies with session_ended and closes the socket.

Messages you receive

text frame
{context_id, sample_rate, audio_format} — the effective rate and format for the context. Use these (not what you requested) to configure playback.
text frame + binary frame
{context_id, sentence_index, chunk_index, audio_duration_s, byte_length} — immediately followed by one binary frame of raw PCM (or opus) bytes. Pair them in order: each audio_chunk announces the binary frame that follows it.
text frame
{context_id, sentence_index, text, audio_duration_s} — one sentence fully synthesized.
text frame
{context_id, num_sentences, total_audio_duration_s, profile, trace_id} — the context is complete. profile carries timing detail (ttfb_ms, generate_ms, overall_rtf, …).
text frame
{context_id, sentences_completed, sentences_dropped, trace_id} — reply to task_cancel.
text frame
{context_id, sentences_completed, code, trace_id} — terminal failure for one context.
text frame
{context_id?, code, message, fatal} — validation or billing errors. When fatal is true the session is closing; otherwise only the offending message was rejected. Billing errors are fatal: insufficient_credits, or stream_billing_error when the billing check itself failed (retry on a new connection).
text frame
{reason} — idle or client_request. The socket closes after this frame.

Audio formats

Binary frames carry raw PCM (no container) in the context’s output_format: Audio is mono at the effective sample_rate from task_started (native 24000 Hz). Wrap or play it client-side; nothing on this path returns WAV.

Phone calls (Twilio)

Twilio Media Streams play 8 kHz µ-law. Start each context with "output_format": "mulaw" and "sample_rate": 8000, then forward every binary frame to Twilio as a media message with the bytes base64-encoded. There is no WAV header to strip and nothing to resample.

Billing

WebSocket synthesis is metered exactly like /speak and SSE streaming: you’re billed per character of text on every task_start / task_continue frame, against your plan’s monthly character allowance. On prepaid credits, a session reserves credits in blocks of up to 1,000 characters ahead of use, so each sentence doesn’t wait on a billing check. The unused part is returned when the session ends, so your balance can dip by up to that much while a session is open.
  • The handshake checks your plan before upgrading: streaming not included in your plan → 403; character quota exhausted → 402 (when billing enforcement is active). The handshake response carries X-Billing-Remaining with your remaining characters.
  • If your workspace runs out of credits mid-session (strict enforcement), the text frame that exceeded the balance is not forwarded — you receive a fatal error frame and the session closes with code 1008 (reason insufficient_credits):
If the billing check itself fails, the same fatal frame arrives with code stream_billing_error instead, and the session closes with 1011 — reconnect and retry. When the synthesis engine ends the session normally you get 1000. If it is at capacity the socket closes with 1013 (reason upstream_busy), and if it fails, with 1011 (reason upstream_error); see the close codes below. If the connection to the engine drops without a close, you get a fatal error frame with code upstream_closed and the socket closes with code 1011. Reconnect and retry. Usage appears in your dashboard analytics and on the monthly invoice like any other synthesis.

Close codes

Any other close code the synthesis engine sends is passed through with its reason.

End-to-end example

The first connection after a long idle period may be slow while the synthesis engine warms up. For latency-sensitive apps, open the socket ahead of the first turn and keep it alive.
Need a one-way, fully self-described stream (e.g. for server-side playback)? Stream speech (SSE) uses a fixed start / chunk / done / error event shape. Use the WebSocket when you need a bidirectional, turn-based session.

Prompt

Copy this into your coding agent (Claude Code, Cursor, Codex…) to implement this endpoint.