Skip to main content
Stream raw audio over a WebSocket and receive a 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 the Authorization header of the WebSocket upgrade request:
Browsers cannot set headers on a WebSocket. Send the key as two subprotocols instead: the 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.
The upgrade request fails before the WebSocket opens with HTTP 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

  1. Open the WebSocket with the query parameters.
  2. Send audio as binary frames: raw samples, channels interleaved, 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 kHz mulaw, 20 ms of one channel is 160 bytes.
  3. Read messages while you send. When the watermark reads on a channel, the server sends a detection message for it.
  4. When you have no more audio, send the text frame {"type": "end_of_stream"}.
  5. The server sends a summary message, then closes with code 1000.
You can send audio faster than real time, for example to check a file over the WebSocket.

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 after end_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.
If you close the connection without 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 the summary 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 as detect-stream.mjs. To convert a file with ffmpeg:
Output for a two-channel call recording with Telnyx text-to-speech on the second channel:
On close 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.