Website widget API
The website chat and voice widget.
The same operations are in the interactive API reference, and the whole contract is in the OpenAPI document.
GET/v1/widget/config
What the widget needs before a chat opens
Returns what the widget needs to draw its launcher and to know whether a human check is required, before any chat opens: the launcher's colour, side, corner radius and the appearance document, and the Cloudflare Turnstile site key.
The key is optional. With a valid publishable key, the launcher fields are the organization's. Without one, only turnstile_site_key is returned and the launcher fields are absent, so the widget cannot look like the business until a key is supplied. A key that is present but wrong is refused with 401 before reaching this operation.
turnstile_site_key is set only when the server will actually check a challenge. When it is present, send the challenge's token as captcha_token when you open a session, or the session is refused.
It reads only, costs nothing and is safe to repeat. Unlike opening a session, it does not check the page's origin.
Accepts: publishable key. Open in the API reference
POST/v1/widget/session
Open or resume a visitor session
Opens a chat for a visitor and returns a session token for every later widget call, together with the widget's look.
Credential. A publishable key (af_pk_…), sent from a site the key lists: the Origin header must match, or 403. A secret key may open a session on a visitor's behalf and skips both the origin check and the human check. A restricted key is refused. Test and live publishable keys both work; a test key reaches the sandbox.
Who the visitor is. Without user_id and user_hash, the visitor is anonymous. Pass visitor_id from an earlier response to carry on as the same anonymous visitor in the same chat; the response returns the visitor_id to keep. To link a signed-in customer to their contact, pass your own user_id together with user_hash, the hex HMAC-SHA256 of the user_id under the organization's identity secret, computed on your server; the secret never goes into the page. A wrong hash is refused with 403. A user_id sent without a hash is ignored and the visitor stays anonymous: an unsigned id is never trusted.
Human check. A browser session (publishable key) must pass the Turnstile challenge, test keys included, by sending captcha_token; otherwise 403 "The human check failed".
Refusals. 403 when chat is switched off for the business. 429 when one address opens more than ten sessions a minute.
Repeating it is safe. The same visitor or customer gets the same contact and conversation back, with a fresh token. No Idempotency-Key applies. The session lasts two hours; open a new one when the stream reports expired or a call answers 401.
Accepts: publishable key. Open in the API reference
GET/v1/widget/messages
The conversation so far
Returns the visitor's conversation in order: what the visitor wrote, what the assistant answered and what a person on the team replied. Use it when the widget opens, to restore the chat, then follow new messages with the stream.
Credential. The session token from POST /v1/widget/session, as a bearer token. It shows only that session's conversation. Expired or missing, 401.
after is a message's seq; only messages after it are returned, up to 100 at a time, oldest first. To catch up, call again with the last seq you have until fewer than 100 come back. Read only and safe to repeat. This is a polling feed, not a paged list: it takes after (a seq), not limit and cursor.
Accepts: widget session. Open in the API reference
| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
after | query | integernull | No | Only messages after this `seq`. |
POST/v1/widget/messages
Send a visitor message
Records something the visitor wrote and returns it as stored. The assistant answers shortly afterwards, and the reply arrives through the event stream or the next GET; this call does not wait for it. Messages sent in quick succession are answered once, together.
Credential. The session token from POST /v1/widget/session.
The text is trimmed and must be 1 to 2000 characters, otherwise 400. At most 20 messages a minute per session (429). 403 when chat has been switched off for the business since the session opened. 409 when the chat has reached its message limit; open a new session or ask the visitor to call. If staff closed the conversation after the session opened, the message goes into a fresh conversation rather than a closed one.
Retrying. There is no Idempotency-Key. Send client_id, the page's own id for the message, and a retried send of the same client_id is stored once. Without it, a retry after a network error may duplicate the message.
Replies from the assistant count towards the organization's daily chat-reply allowance; when it is used up the visitor's message is still stored but the assistant stays silent until it renews.
Accepts: widget session. Open in the API reference
GET/v1/widget/stream
New messages as server-sent events
Streams new messages in the visitor's conversation as server-sent events: event: message, with the message as JSON in data and its seq as the event id, until the session expires. When it does, one event: expired is sent and the stream ends; open a new session.
Credential. The session token. A browser EventSource cannot send headers, so the token may be passed as ?token= instead. Do not log or share URLs that carry it. A missing or expired token answers 401 before the stream starts.
Pass ?after=<seq> to resume from the last message you saw; without it the stream starts from the beginning of the conversation. Messages are pushed as they arrive, and the server also checks every 15 seconds so a lost notification delays a message at most that long. Reconnect when the connection drops; resume with after.
The stream carries no cost beyond the conversation itself. Only one per visitor is useful: opening many ties up connections for no gain.
Accepts: widget session. Open in the API reference
| Parameter | In | Type | Required | Description |
|---|---|---|---|---|
after | query | integernull | No | Only messages after this `seq`. |
token | query | string | No | The session token, for clients that cannot set an `Authorization` header. |
POST/v1/widget/typing
Tell the assistant the visitor is typing
The visitor is typing: hold the pending reply a little longer, so the message they are writing is answered together with the ones before it. Send it while the visitor types, at most every few seconds.
Credential. The session token. Each call pushes the wait ahead by about three seconds, but never past 15 seconds after the first unanswered message, so a visitor who types forever still gets an answer. It has no effect when no reply is waiting.
Always answers 204 with no body once the session is valid. Beyond 60 calls a minute per conversation the call is ignored and still answers 204 rather than 429, so a chatty page is never shown an error for it. Safe to repeat.
Accepts: widget session. Open in the API reference
POST/v1/widget/handoff
The visitor asks for a person
The visitor asks for a person: the assistant stops answering this chat and the organization's owners and admins are notified, in the console, that a visitor needs them. A conversation.needs_person event is also sent to webhooks, with the reason visitor_asked.
Credential. The session token. Team members then reply from the contact, which takes the chat over.
Repeating it is safe: asking twice stops the assistant once and notifies each admin once, because the notification has a stable key per conversation and admin. The assistant stays off for this conversation until a person turns it back on from the console. It works even when the widget's handoff button is switched off in settings, since that only hides the button.
Answers 204 with no body.
Accepts: widget session. Open in the API reference
POST/v1/widget/feedback
The visitor marks an answer as wrong
A thumbs-down on an answer in this chat. It is kept as a quality signal that the business's gap list and the assistant's calibration read; it does not change what the assistant knows and calls no model.
Credential. The session token. Leave out turn to rate the chat's last answer. Answers 404 when the assistant has not answered yet in this chat.
Accepts: widget session. Open in the API reference
POST/v1/widget/verify/start
Send the visitor a code or a signed link to prove who they are
Sends a one-time code (or a signed link) to the channel stored for the visitor's contact, never to a number typed in the chat. The answer is 202 whether or not the contact has a channel, so it cannot be used to find out which numbers are customers. Completing it raises the contact from channel-asserted to verified for a while.
Credential. The session token. The visitor passes the human check first: send the Turnstile token as captcha_token when the site shows the challenge. Sends are capped per number, per address, per workspace and per month; the caps that depend on the caller are answered 429.
Accepts: widget session. Open in the API reference
POST/v1/widget/verify/confirm
Check the code or link the visitor was sent
Checks the typed code, or the token of the signed link the visitor opened. Too many wrong codes lock the send (locked is true): start a new verification.
Credential. The session token.
Accepts: widget session. Open in the API reference
POST/v1/widget/voice
Start a voice call with the assistant in the browser
Puts the visitor in a voice call with the assistant, over the browser's microphone. It makes a call room, sends the assistant into it, and returns a short-lived microphone-only token and the media server's URL for the page to connect with. A call.started event is sent to webhooks.
Credential. The session token.
This costs money. Website calls are real AI minutes, charged to the organization like any call, whatever the mode of the key that opened the session. The business must have voice switched on in the widget settings and be verified or paying for the website-voice capability; otherwise 403, in plain words that do not reveal billing to the visitor.
Limited to two calls a minute per conversation (429). 409 when voice is not available right now, for example the call server is not running; try again later. Each successful call makes a new room, so a retry after a timeout may start a second call: do not retry blindly, and no Idempotency-Key applies.
Accepts: widget session. Open in the API reference
GET/v1/widget/settings
The website widget's settings
Returns how the website widget looks and behaves for this organization: its name and greeting, language, colour, side and corner radius, whether voice and the "ask for a person" button are on, the assistant it uses, the sites it may run on, and the full appearance document.
This GET can write. The first time it is called it creates the widget's settings and its identity secret, so a new organization sees defaults instead of nothing. Later calls change nothing.
domains are the allowed origins of the newest publishable key that is not revoked. The publishable key itself is never returned here: only a hash is kept, and the key is shown once, when it is made under Apps. The identity secret used to sign user_hash is not returned here either.
Who may call it: a person with settings or developer-management rights, or a secret key. Restricted and publishable keys are refused. This is the staff view; the visitor's widget reads /v1/widget/config.
Scope widget:read. Accepts: API key or personal access token. Open in the API reference
PUT/v1/widget/settings
Save the website widget's settings
Saves the widget's settings and returns them as stored. Changes show on the site for the next visitor to open a chat; sessions already open keep what they were given.
It replaces, not merges, the core fields. Always send name, greeting, language, color, position, radius, voice_enabled and handoff_enabled; a boolean left out is saved as false. Only appearance is partial: it is left unchanged when absent, and when present it is checked and replaces the stored document, in which every field has a default.
Rules, each refused with 400 and nothing saved: name 1 to 60 characters; greeting up to 300; color a hex colour like #0A7D5A (stored in upper case); language en or ne; position right or left; every domains entry a real website address. radius is cut to between 0 and 32 rather than refused. domains are tidied to origins and de-duplicated.
Domains apply to every publishable key. Setting domains rewrites the allowed origins of all publishable keys of the organization that are not revoked, test and live alike, and a [] locks every widget out until origins are set again. This is the one place that edits a key's origins.
voice_enabled: true only allows voice; whether visitors can actually call also depends on the organization's plan or verification.
Saving the same body again leaves the same result, so it is safe to repeat.
Who may call it: a person with settings or developer-management rights, or a secret key. Restricted and publishable keys are refused.
Scope widget:write. Accepts: API key or personal access token. Open in the API reference