Connect
Send the API key in the handshake’sAuthorization: Bearer header. Browsers can’t set that header on a WebSocket, so connect from a server-side client.
The handshake is an HTTP request. A refusal is an HTTP response with the standard Telnyx error body, never an opened-then-closed socket:
Every message after the upgrade is a JSON text frame with a
type field. Binary data is base64-encoded.
Exec Attach
The server replays the exec’s combined output (stdout and stderr in the order written) from byteoffset, then streams it live.
Client Messages
Server Messages
Resume
Reconnect at any time, from anywhere, withoffset set to:
- after an
outputmessage: itsoffsetplus the decoded length of itsdata; - after a
gap: itsoffsetplus itslength; - after a
truncated: itsoldest_offset.
offset=0 replays everything still retained.
Disconnecting never sends EOF and never stops the process. Use stdin_eof, POST …/execs/{exec_id}/stdin/close, or POST …/kill.
Example: Follow an Exec’s Output
The following Python client (using thewebsockets package, version 14 or later) prints an exec’s output and resumes after a dropped connection:
websockets.InvalidStatus, which this client doesn’t retry.
The client stops at the first exit message. An exec that ends failed with reason agent_unavailable or cancelled can still start afterwards (see Statuses), so before treating one of those as final, read the exec again with GET /v2/ai/sandboxes/{sandbox_id}/execs/{exec_id}.
Example: Write to an Exec’s Stdin
Start the exec with a command that reads stdin, then send input and EOF:Main Process Stdin
wss://api.telnyx.com/v2/ai/sandboxes/{sandbox_id}/attach writes to the stdin of the sandbox’s main process. It accepts the same client messages as the exec attach: stdin (at most 64 KiB decoded each) and stdin_eof.
The server sends:
Reconnect at any time. Disconnecting never sends EOF, so the process keeps waiting for input.
POST /v2/ai/sandboxes/{sandbox_id}/stdin/close closes the main process’s stdin without a WebSocket.