Connect
?client_id=kl_ci_...&client_secret=kl_cs_... instead.
If the handshake is refused you get a plain HTTP error instead of a connection: 401 for missing or wrong credentials, 403 for a disallowed origin or another knowledge base’s key, 426 if the request is not a WebSocket upgrade.
Ask a question
Send achat frame:
| Field | Required | Description |
|---|---|---|
type | Yes | Always "chat" |
query | Yes | The question. Max 4,000 characters |
thread_id | No | Continue a conversation |
captcha_token | When CAPTCHA is on | A reCAPTCHA v3 token, for public keys. See CAPTCHA |
group_ids | No | Answer only from these source groups |
user | No | { "id": "...", "email": "..." }, for analytics. id is used when both are sent |
widget_id | No | A saved widget’s id. The answer uses that widget’s voice. The question must also pass that widget’s Enabled switch and Allowed origins, checked against the page that opened the connection |
Receive the answer
token frames stream the text. One done frame ends the answer:
done frame:
citationslists the sources the answer cited, each withindex,title,urlandsnippet. The field is left out when the answer cited nothing.message_idis what you rate.message_idandthread_idare both absent when the workspace uses Zero data retention.answeris only sent when the[n]markers were renumbered. When it is there, show it instead of the text you built fromtokenframes. See Renumbered answers.
Errors
A failed question gets oneerror frame instead of done:
code | Meaning |
|---|---|
bad_request | Invalid JSON, a type other than chat, empty query, a question over 4,000 characters, a thread_id from another knowledge base, or a widget_id that is not valid or names no widget of this knowledge base |
captcha_required | Public key, CAPTCHA is on, and no captcha_token was sent |
captcha_failed | The token did not verify |
rate_limited | The key’s per-minute limit was reached |
forbidden | A group_ids entry is not a source group of this knowledge base, or the widget named by widget_id is turned off or does not allow the page’s origin. message says which |
unauthorized | The key was revoked. The connection closes |
| anything else | The answer failed. Same codes as chat errors |
retryable says whether sending the same question again can work. After an error frame the connection stays open for the next question, except where noted.
Connection rules
- Questions are answered one at a time, in the order you send them.
- The server pings every 30 seconds. It closes the connection after 60 seconds with no frame or pong from you. Browsers answer pings for you.
- A single frame can be up to 64 KB.
- The key is checked again on every question, so a revoked key stops working mid-connection.