This is the real-time transport for an API chat. If you only need request and response, Create chat completion sends a message and returns the agent’s reply in one call. Use the WebSocket when you want the agent’s reply to stream in and your UI to update as it does.
Overview
The chat WebSocket runs a live chat session: your application submits the user’s messages and receives the agent’s replies, tool calls, and node transitions as JSON events, in order. Open one connection per participant in a chat you already created. Use it when a single request and response isn’t enough:- Live chat UI — show the agent’s reply as it’s generated and update the thread the moment each message is stored, instead of waiting for one completion call to return.
- Human handoff — when the agent escalates a chat to a person, that human connects as
transfer_target, reads what the user sends, and replies on the same socket. - Multiple viewers — up to 5 connections can follow one chat at once, so a user’s browser and a supervisor can both watch.
transfer_target and takes over.
Endpoint
WebSocket endpoint:
wss://api.retellai.com/connect-to-chat?chat_id={chat_id}&role={role}Query parameters
Id of the chat to connect to, from Create chat. The chat must belong to your workspace, be ongoing, and be an API chat — SMS chats can’t be connected to.
Who you’re connecting as:
user for the person chatting with the agent, or transfer_target for the human a chat was escalated to.Available options: user, transfer_targetAuthentication
Authenticate the handshake the same way you would a chat REST request: a Retell API key on your server, or a public key from an allowed domain in the browser. A restricted API key needs the Call → Edit permission. A public key can connect asuser only — it’s visible to every visitor, so it can’t vouch for the human a chat was escalated to. Connect as transfer_target with an API key from your backend.
Connection limits
- The chat must be ongoing. Connecting to an ended chat, or an SMS chat, is rejected.
- Up to 5 connections per chat (as of November 2026). A sixth is rejected.
- Up to 10,000 concurrent chats per workspace have an open connection at once (as of November 2026). Each connection holds its place with a renewing lease, so a chat’s place frees itself shortly after its last connection drops.
Protocol
All messages are text frames whose data is a stringified JSON object. Check thetype field to tell events apart.
Event flow
- Retell sends one
init_socketevent. It carrieslast_message_timestamp, the created time of the newest message already stored. The socket forwards only messages created after this, so list the earlier ones with List chat messages. - From then on, each change arrives as a message event: a
new_messagefor a user, agent, transfer target, or tool call, or anode_transitionfor a conversation flow move. - To send a message, your application sends a
submit_messageframe. You may send it once you’ve receivedinit_socket; frames sent earlier are ignored. - When the chat is handed to a human, Retell sends a
chat_escalatedevent. When the chat ends, it sends achat_endedevent and closes the connection with code1000.
event_timestamp, the message’s created time. Forward only events newer than the last one you handled: a message can be re-sent when a handler can’t confirm its earlier broadcast reached you.
Submitting messages
Send asubmit_message frame to add the user’s (or transfer target’s) message. The agent replies unless the chat has been escalated to a human.
A key you choose to identify this message, 1 to 256 characters. The message is stored once per key, so resending the same key doesn’t store it twice. A message counts as delivered when it comes back as a
new_message with this key.idempotency_key — the key keeps it from being stored twice.
Listing earlier messages
The socket doesn’t replay a chat’s history. To show messages created before you connected, callPOST /list-chat-messages/{chat_id} up to init_socket’s last_message_timestamp, then append the events the socket sends from there. Each listed message carries the idempotency_key it was submitted with, the same key the socket’s new_message events use, so a message you both list and receive is easy to match.
Close codes
Events
Init socket event
Theinit_socket event, sent once, first.
Created time of the newest stored message, in milliseconds since the Unix epoch, or
0 if the chat has none. The socket sends only messages created after this.New message event
Anew_message event: a user, agent, transfer target, or tool-call message was stored. The fields depend on role.
Available options:
user, agent, transfer_target, tool_call_invocation, tool_call_resultuser, agent, or transfer_target message:
The key the message was submitted with, so the sender can recognize its own message. Absent on messages the agent generated.
tool_call_invocation message:
Arguments passed to the tool. A stored JSON object is parsed; anything else is the stored string.
tool_call_result message:
What the tool returned. A stored JSON object is parsed; anything else is the stored string.
Node transition event
Anode_transition event: a conversation flow agent moved to another node.
Chat escalated event
Achat_escalated event: the chat was handed to a human.
The kind of destination only. Where the handoff goes stays between you and Retell, since the socket can reach an end user’s browser.
Chat ended event
Achat_ended event, the last one before Retell closes the connection with code 1000.
Sample events
FAQ
Does the socket send the chat's earlier messages?
Does the socket send the chat's earlier messages?
No. It sends
init_socket and then only messages created after last_message_timestamp. List the earlier ones with POST /list-chat-messages/{chat_id} up to that timestamp.How do I know my submitted message was stored?
How do I know my submitted message was stored?
It comes back as a
new_message event carrying the idempotency_key you submitted it with. Until then, treat it as pending.A submission failed and the socket closed. What now?
A submission failed and the socket closed. What now?
Reconnect and submit again from the failed message on, under the same
idempotency_key. The key keeps the message from being stored twice.Can I use this for SMS chats?
Can I use this for SMS chats?
No. Only API chats can be connected to. SMS chats run over Twilio — see Enable SMS.

