# Chat REST API: send messages to the chatbot

> Get a visitor token, send a message, stream the answer and save leads through the chatbot's REST routes, with request and response examples.

Source: https://www.wpbot.pro/help/dev-chat-rest-api/  
Section: Developers › REST API and MCP  
Updated: 2026-11-26

The chatbot's public routes live under `/wp-json/wpbot-chatbot/v1/`. They answer with the same engine as the chat window: Simple Text Responses, Dialogflow, site search, the knowledge base, AI and the fallback, with your answer settings, Business Context and spending limits. Use them to build your own chat interface or to let a kiosk, app or another site ask your chatbot.

## 1. Get a visitor token

```
curl -X POST https://example.com/wp-json/wpbot-chatbot/v1/token
```

```
{ "token": "v1.1795000000.3f2a…", "expires": 1795086400 }
```

The token is valid for 24 hours and is not tied to a page, so it also works for pages served from a full-page cache. Fetch it when the chat opens, not when the page is built. Send it with every other request in the `X-WPBot-Token` header (or as a `token` field).

## 2. Send a message

```
curl -X POST https://example.com/wp-json/wpbot-chatbot/v1/message \
  -H "Content-Type: application/json" \
  -H "X-WPBot-Token: v1.1795000000.3f2a…" \
  -d '{"text":"Do you ship to Canada?","session_id":"kiosk-0042","page_url":"https://example.com/shipping/","language":"en_US"}'
```

| Field | Required | What it is |  |
| --- | --- | --- | --- |
| `text` | yes | The visitor's message |  |
| `session_id` | recommended | Your id for the conversation (6–128 characters: letters, digits, `_ - . :`). Messages with the same id share history and form one session in **ChatBot → Sessions** |  |
| `name` | no | The visitor's name, if known |  |
| `language` | no | The locale, for example `en_US` |  |
| `page_url`, `page_title` | no | The page the visitor is on; used when **Use the current page** is on |  |
| `history` | no | Earlier messages as \`\[ { "role": "user" \\ | "assistant", "content": "…" } \]\` |
| `extra` | no | `strid` (a chosen Simple Text Response), `intent`, `ai_action` |  |
| `stream` | no | `true` for server-sent events |  |

The answer:

```
{
  "source": "ai",
  "text": "Yes, we ship to Canada. Delivery takes 5–7 business days.",
  "html": "",
  "data": { },
  "meta": { "provider": "openai", "model": "…" }
}
```

`source` tells you which step answered: `str` (Simple Text Responses), `search` (site search, with result cards in `html` and `data`), `ai`, `dialogflow` or `fallback`. `text` can contain Markdown; `html` is already safe HTML.

## 3. Stream the answer

With `"stream": true` the route answers with server-sent events: a series of `{"type":"text","text":"…"}` pieces while the AI writes, then one `{"type":"done","reply":{…}}` with the full reply in the format above. Providers without streaming send the answer as one piece.

## 4. Save visitor details and leads

| Route | Fields | Answer |
| --- | --- | --- |
| `POST /session/{session_id}` | `name`, `email`, `phone`, `source_url` | `{ "status": "success" }` |
| `POST /lead` | `name`, `email` and/or `phone`, `url`, `language` | The same message the chat shows after a sign-up |

A lead saved here fires `qcld_wpbot_chat/lead_captured`, so it reaches AI Marketing and your own hooks like any chat lead. The routes `/feedback` and `/report` serve the chat window's like, dislike and report buttons.

## Limits and errors

| Status | Code | Meaning |
| --- | --- | --- |
| 403 | `qcld_wpbot_chat_token` | The token is missing or expired: fetch a new one |
| 400 | `qcld_wpbot_chat_empty` | No text was sent |
| 429 | `qcld_wpbot_chat_rate_limited` | Too many requests from this IP address: wait and try again |

Every route is rate limited per IP address, and AI answers follow the chatbot's AI limits (by default 60 AI answers a day for visitors who are not logged in) and your [Limit chatbot AI answers per user role](/help/chatbot-rate-limits/). Requests sent from one server all share that server's IP address, so for a server-side integration raise the limits with the `qcld_wpbot_chat/rate_limit` filter, or pass the real visitor's address through `qcld_wpbot_chat/visitor_ip` when your proxy is trusted. When an AI spending limit is reached, the chatbot still answers from saved answers, FAQ and site search.

Tip

Visitors' messages and answers are stored like chat window conversations, so they appear in **ChatBot → Sessions** (while chat sessions are saved) and unanswered questions in **ChatBot → Train**. Tell your users in your privacy policy.

## Availability

In every edition, including WPBot Free.
