Chatbot API: how it works and what you can build with it
What a chatbot API does, the request and response you can expect, how streaming and user identity work, and the builds that make sense when you use an API instead of a widget.
Read articleA chatbot API lets your own app, portal or backend talk to your support agent. See when it beats a widget, and the request, streaming and error handling you need.
By the Askocto team · · 5 min read
You use a chatbot API when you want your support agent inside something you build yourself: a customer portal, a mobile screen, an internal tool or a backend job. Your code sends the customer's message to the agent, receives the reply and shows it in your own interface. This article covers when the API is the right choice, then walks through a first request, conversation continuity, streaming and error handling, with examples against the Askocto v2 API.
In short: Keep the API key on your server, send each customer message to the agent, store the returned conversation ID and stream the reply when the user is waiting on screen.
Many teams do not need the API. A widget gives you a working chat in minutes, with no front-end code. Choose the API when one of these is true:
If none apply, a widget is the faster route, and Askocto gives you both options from the same agent. Read how to embed an AI chat widget on any website and skip the code. The wider overview of APIs for chat is in chatbot API basics.
Create a key under Settings > API keys in your workspace. It is shown once, and production keys start with oc_live_, while test keys start with oc_test_. A key acts on the whole workspace, so treat it like a password.
Never put the key in browser code. Your front end should call your own server, and your server should call the API with the key.
The chat endpoint is POST /v2/agents/{agentId}/chat. Authenticate with a bearer token and send the conversation so far.
curl https://api.askocto.co/v2/agents/agt_123/chat \
-H "Authorization: Bearer $OCTO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"messages": [{ "role": "user", "content": "Where is my order?" }],
"userId": "usr_42",
"stream": false
}'
The same call in JavaScript on Node 20 or later:
const res = await fetch("https://api.askocto.co/v2/agents/agt_123/chat", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OCTO_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Where is my order?" }],
userId: "usr_42",
stream: false,
}),
})
const data = await res.json()
console.log(data.message.content)
A response contains the conversationId, the assistant message with its content and the sources used, and a usage block. Show the sources as links if you want customers to read further.
Save the conversationId from the first response, then send it with every later message so the agent keeps the context.
{
"conversationId": "conv_8f2a",
"messages": [{ "role": "user", "content": "It was order 1042" }],
"userId": "usr_42"
}
Use a stable userId for each customer. It ties conversations together and lets you find or delete a customer's history later. For logged-in users, pass your own user ID rather than inventing a new one for each visit.
A full answer can take a few seconds. If a person is watching, set "stream": true and show text as it arrives. The response is a stream of server-sent events: message.delta for new text, message.done when the message is complete, tool.call when the agent runs an action and error if something fails. The stream ends with [DONE].
event: message.delta
data: {"messageId":"msg_01","delta":"Your order shipped "}
event: message.done
data: {"messageId":"msg_01"}
data: [DONE]
Read the stream in chunks, split on blank lines and append each delta to the screen. The streaming reference lists every event and field.
Errors come back in one shape, with a code, a message and a request ID.
{ "error": { "code": "rate_limited", "message": "Too many requests.", "requestId": "req_77" } }
| Code | Status | What to do |
|---|---|---|
invalid_request |
400 | Fix the body and do not retry unchanged |
unauthorized |
401 | Check the key and the header |
forbidden |
403 | The key lacks access to this resource |
not_found |
404 | Check the agent or conversation ID |
conflict |
409 | Reload state, then try again |
rate_limited |
429 | Wait for the Retry-After header, then retry |
server_error |
500 | Retry with backoff, then show a fallback |
Log the requestId with every failure. It is the first thing support needs to trace a problem. Always give the customer a fallback line, such as "I could not answer that just now. Would you like to talk to the team?"
The request itself is small. Most of the work sits around it. Decide where the conversation ID is stored, for example next to the customer record in your database. Decide what your interface shows while the agent is working, such as a typing indicator, and what it shows when a tool.call event arrives. Decide how a customer reaches a person from your screen, because the agent will sometimes hand over. A handover event tells you the conversation moved to your team, and you can mirror it in your interface.
Test with a key from the test environment first. Send ten real past questions, check the sources attached to each answer and look at what happens when you send an empty message, a very long message and a message in a language you did not plan for.
userId.conversationId is stored and reused.Retry-After.The full parameters live in the chat endpoint reference. If you are still deciding how to build the whole experience, building a custom AI chatbot for personalized support compares the options, and you can test the agent first on the free plan.
You should not, because the key would be visible to anyone. Call your own server from the browser and let the server call the API.
Store the conversationId returned by the first response and send it with every later message from the same customer.
Stream when a person is waiting on screen, because the first words appear sooner. For background jobs, such as drafting an email, a plain response is simpler.
Yes. Actions and procedures configured on the agent run the same way as in a widget chat, and a tool.call event tells your code when one runs.
Askocto answers the repeat questions across chat, email and voice and hands the hard ones to your team.
What a chatbot API does, the request and response you can expect, how streaming and user identity work, and the builds that make sense when you use an API instead of a widget.
Read articleA buyer's guide to pairing an AI agent with Zendesk: criteria to compare, questions for a demo, a two-week trial plan using your own tickets and a rollout checklist.
Read articleA buyer's guide for connecting an AI support agent to Zendesk through Sunshine: what to compare, what to ask in a demo, and how to test with your own tickets.
Read article