Skip to main content
Two WebSocket endpoints carry interactive I/O:
The CLI’s exec and execs attach commands implement this protocol, resuming included. Use this page to build a client of your own.

Connect

Send the API key in the handshake’s Authorization: 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 byte offset, then streams it live.

Client Messages

Server Messages

Resume

Reconnect at any time, from anywhere, with offset set to:
  • after an output message: its offset plus the decoded length of its data;
  • after a gap: its offset plus its length;
  • after a truncated: its oldest_offset.
Reconnecting this way also covers the edge proxy’s connection limit of about 600 seconds. Each exec keeps at least its last 64 MiB of output, within the sandbox’s total for exec output (1/16 of its memory, at most 256 MiB), oldest output deleted first. 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 the websockets package, version 14 or later) prints an exec’s output and resumes after a dropped connection:
A refused handshake raises 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.

Close Codes