Skip to Content
Text to speech

Connect a live speech session

Permissioninference-tts:createAdd-onInferenceAuthentication
GET
/inference/audio/speech/realtime

Opens a WebSocket for streaming speech. The model connection opens once, at connect, so every utterance after that skips connection setup.

Connecting

From a backend, send an Inference API key in the Authorization Bearer header, with the model, voice (an approved UUID), and optionally max_characters (1–100,000; default 10,000) and max_audio_seconds (1–600; default 600) query parameters.

From a browser, mint a one-use token with POST /inference/realtime-tokens and connect with the token query parameter alone. Never combine token with Authorization, and never put a long-lived key in a URL. A token is consumed on the first connection attempt.

Speaking

Send {"type":"speak","text":…} for each utterance, 1–8,000 characters. Utterances may be pipelined and are spoken in order. Audio comes back as binary PCM16 mono frames at the sample_rate given in the ready event, with {"type":"done","segment":n,"audio_ms":…} after each one.

The other messages

{"type":"cancel"} drops every utterance not yet fully spoken and answers {"type":"cancelled","segments":[…]}. {"type":"keepalive"} holds a quiet session open. {"type":"finish"} ends the session once queued speech is done, answered by {"type":"finished"}.

Limits and close codes

A session lasts at most 9 minutes. Each speak is charged by its characters when accepted, against the session's max_characters budget; a message that would pass the budget is refused with character_limit_reached and costs nothing. max_audio_seconds only caps the speech produced. With nothing queued and nothing received for 60 seconds, the session gets an idle_timeout error event and closes with 1008.

Close codes are 1000 after finished; 1008 for idle_timeout, audio_limit_reached, character_limit_reached or invalid_message; 1011 when the model fails.

Text to speech does not accept language. Credentials, options, permissions and available minutes are all checked before the upgrade, and each direct connection creates a new session.

See live speech.

Authorization

headerAuthorizationBearer <token>

Use an Inference API key. Access and keys.

Query Parameters

token?string

One-use browser token. Omit when authenticating with an API key.

Length43 <= length <= 43
model?string

Published TTS model alias. Required with API-key authentication; omit when using a token.

voice?string

Approved voice UUID. Required with API-key authentication; omit when using a token.

Formatuuid
max_characters?integer

Character budget reserved for the session with API-key authentication. Omit when using a token.

Range1 <= value <= 100000
Default10000
max_audio_seconds?integer

Cap on the speech the session may produce, with API-key authentication. A safety limit; it does not change what is charged. Omit when using a token.

Range1 <= value <= 600
Default600

Response Body

WebSocket upgraded. Client sends JSON speak, cancel, keepalive and finish messages. Server sends binary PCM16 audio and JSON events: ready, done, cancelled, finished, error.

Example Request
curl -X GET 'https://api.kejue.co/v1/inference/audio/speech/realtime'
Example Response
Empty