Skip to main content
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.
For example, a support site opens a chat socket from the browser when the visitor starts chatting. The agent’s reply streams into the thread, and when it reaches an escalation node, the site routes the conversation to a live agent who joins as transfer_target and takes over.

Endpoint

WebSocket endpoint: wss://api.retellai.com/connect-to-chat?chat_id={chat_id}&role={role}

Query parameters

string
required
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.
enum<string>
required
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_target

Authentication

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 as user 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 the type field to tell events apart.

Event flow

  1. Retell sends one init_socket event. It carries last_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.
  2. From then on, each change arrives as a message event: a new_message for a user, agent, transfer target, or tool call, or a node_transition for a conversation flow move.
  3. To send a message, your application sends a submit_message frame. You may send it once you’ve received init_socket; frames sent earlier are ignored.
  4. When the chat is handed to a human, Retell sends a chat_escalated event. When the chat ends, it sends a chat_ended event and closes the connection with code 1000.
Message events carry an 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 a submit_message frame to add the user’s (or transfer target’s) message. The agent replies unless the chat has been escalated to a human.
enum<string>
required
Available options: submit_message
string
required
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.
string
required
The message text, 1 to 50,000 characters.
A message you submit while the agent is still replying interrupts the reply, so one reply answers both messages. Keep at most 10 submissions in flight on a socket at a time. If a submission fails, Retell closes the socket. Reconnect and submit the message again from the failed one on, under the same 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, call POST /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

The init_socket event, sent once, first.
enum<string>
required
Available options: init_socket
integer
required
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.
boolean
required
Whether the chat has already been handed to a human.

New message event

A new_message event: a user, agent, transfer target, or tool-call message was stored. The fields depend on role.
enum<string>
required
Available options: new_message
enum<string>
required
Available options: user, agent, transfer_target, tool_call_invocation, tool_call_result
integer
required
The message’s created time, in milliseconds since the Unix epoch.
For a user, agent, or transfer_target message:
string
required
Id of the stored message.
string
required
The message text.
string
The key the message was submitted with, so the sender can recognize its own message. Absent on messages the agent generated.
For a tool_call_invocation message:
string
Id of the tool call, to match with its result.
string
Name of the tool.
string
Kind of tool, such as custom, code, or mcp.
object | string
Arguments passed to the tool. A stored JSON object is parsed; anything else is the stored string.
boolean
Whether the call moved the agent to another state.
For a tool_call_result message:
string
required
Id of the invocation this result belongs to.
object | string
What the tool returned. A stored JSON object is parsed; anything else is the stored string.
boolean
Whether the tool call succeeded.

Node transition event

A node_transition event: a conversation flow agent moved to another node.
enum<string>
required
Available options: node_transition
integer
required
When the move happened, in milliseconds since the Unix epoch.
string
required
Id of the node the agent left.
string
required
Name of the node the agent left.
string
required
Id of the node the agent entered.
string
required
Name of the node the agent entered.
enum<string>
required
How the node was reached: normal, global, global_go_back, or interrupt_go_back.

Chat escalated event

A chat_escalated event: the chat was handed to a human.
enum<string>
required
Available options: chat_escalated
integer
required
When the chat was escalated, in milliseconds since the Unix epoch.
object
required
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

A chat_ended event, the last one before Retell closes the connection with code 1000.
enum<string>
required
Available options: chat_ended
integer
required
When the chat ended, in milliseconds since the Unix epoch.
string
Why the chat ended, when recorded.

Sample events

FAQ

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.
It comes back as a new_message event carrying the idempotency_key you submitted it with. Until then, treat it as pending.
Reconnect and submit again from the failed message on, under the same idempotency_key. The key keeps the message from being stored twice.
No. Only API chats can be connected to. SMS chats run over Twilio — see Enable SMS.