detection message as soon as the Telnyx text-to-speech watermark reads on a channel. Use it for audio that is still coming in, such as a live call. For a recorded file, use the HTTP endpoint instead.
Endpoint
Authentication
Send your API key in theAuthorization header of the WebSocket upgrade request:
telnyx-auth-ws marker, then base64url.bearer.telnyx. followed by the key in base64url without padding. Send either the header or the subprotocols, not both.
Do not ship your API key to browsers you do not control. For public web apps, proxy the stream through your server.
401 for missing or invalid credentials, 403 for an account level that does not permit watermark detection, and 422 (37005) for an organization whose data locality this endpoint does not serve.
Query parameters
Set all options as query parameters on the upgrade request. You cannot change them during the session.
A missing or unsupported value opens the connection, sends an
invalid_params error frame, and closes it with code 1008.
Stream the two legs of a call as two channels rather than a mono mix: each channel is read on its own, and the other party’s voice masks the watermark in a mix.
Session flow
- Open the WebSocket with the query parameters.
- Send audio as binary frames: raw samples,
channelsinterleaved, with no header. Each frame must contain a whole number of samples for all channels and must not exceed 65,536 bytes. 20 ms frames are a good default; at 8 kHzmulaw, 20 ms of one channel is 160 bytes. - Read messages while you send. When the watermark reads on a channel, the server sends a
detectionmessage for it. - When you have no more audio, send the text frame
{"type": "end_of_stream"}. - The server sends a
summarymessage, then closes with code1000.
Messages
Detection
Sent once for each channel whose watermark reads, as soon as it reads. Channels without a watermark send none.detected_at_millis is the position in the channel’s audio at which the watermark read. Each channel is first read after 4 seconds of its audio, then about every 2 seconds, and less often after 30 seconds. Clean text-to-speech audio can read at the first read; through phone networks, expect 8 to 20 seconds of synthesized speech.
After a detection, that channel is no longer read. You can end the stream as soon as you have the answer you need.
Summary
Sent once afterend_of_stream, or at a session limit. data is the same object that the HTTP endpoint returns, with audio.format set to the stream’s format.
end_of_stream, you get no summary.
Error
code is a stable identifier; message is for humans, do not parse it. Some errors end the session and some only drop a frame. See WebSocket errors.
Session limits
A session is limited to 590 seconds, and to 60 minutes of audio. At either limit, the server sends thesummary for the audio received so far, then a max_session_duration error frame, and closes with 1008. To keep checking a longer stream, open a new session.
Examples
Both examples read a 16-bit PCM WAV file, stream it in 20 ms frames in real time as a live source would, and print each detection and the summary. Because they stream in real time, a file longer than about 9.5 minutes reaches the session limit; remove the 20 ms pause to send faster than real time. Save the Node.js example asdetect-stream.mjs. To convert a file with ffmpeg:
1011 or 1013, or a close without a summary, the failure is temporary: reconnect with exponential backoff and send the audio again. See Close codes.
The WebSocket API reference lists every message schema.