Chat with an agent
POST/v2/agents/{agentId}/chat
Send a customer message to an agent and get its reply as JSON or as a stream of server-sent events, in a new or existing conversation.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
Sends a message to an agent and returns its answer. Call it from your server when you build your own chat interface, connect a channel that Askocto does not cover, or test answers from a script.
Request
POST /v2/agents/{agentId}/chat
Path parameters:
| Name | Type | Required | Description |
|---|---|---|---|
agentId |
string | Yes | The ID of the agent, starting with agt_. |
Body fields:
| Name | Type | Required | Description |
|---|---|---|---|
messages |
array | Yes | The messages to send. Each item has a role (user) and a content string. |
conversationId |
string | No | Continue this conversation, starting with conv_. Leave it out to start a new one. |
userId |
string | No | Your ID for the person, starting with usr_. Groups their conversations, see User conversations. |
stream |
boolean | No | true returns server-sent events instead of JSON. Defaults to false. |
metadata |
object | No | Your own key-value pairs, stored with the conversation. |
When you pass a conversationId, send only the new message. The agent already has the earlier ones. Without it, a conversation is created and its ID comes back in the response.
Example request
curl -X POST https://api.askocto.co/v2/agents/agt_8f3k2m/chat \
-H "Authorization: Bearer $OCTO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"messages": [{ "role": "user", "content": "Where is my order?" }],
"userId": "usr_3b6t0z",
"metadata": { "page": "/account/orders" }
}'
const response = await fetch("https://api.askocto.co/v2/agents/agt_8f3k2m/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_3b6t0z",
metadata: { page: "/account/orders" },
}),
})
const reply = await response.json()
console.log(reply.message.content)
Example response
A successful call returns 200 OK.
{
"conversationId": "conv_5n2p8w",
"message": {
"id": "msg_7c1k9s",
"role": "assistant",
"content": "I can check that for you. What is your order number?",
"sources": [{ "id": "src_91xa7d", "title": "Shipping and delivery" }]
},
"usage": { "credits": 1 }
}
Response fields
| Name | Type | Description |
|---|---|---|
conversationId |
string | The conversation the message belongs to. Send it back to continue the chat. |
message |
object | The reply: id, role (assistant), content and the sources it used. |
toolCalls |
array | Present when the agent needs your app to run a client action. content is then empty. |
usage.credits |
integer | The credits used by this reply. |
With "stream": true, the reply arrives as message.delta, message.done, tool.call and error events, see Streaming. When toolCalls is present, run the actions and answer with Submit a tool result. To regenerate an answer, use Retry a message.
Errors
| Status | Code | When |
|---|---|---|
| 400 | invalid_request |
messages is missing or empty, a message has no content, or userId or conversationId has the wrong format. |
| 401 | unauthorized |
The API key is missing, malformed or deleted. |
| 404 | not_found |
The agent or the conversation does not exist in your workspace. |
| 429 | rate_limited |
Too many requests. Wait for the number of seconds in the Retry-After header. |
| 500 | server_error |
The reply failed on our side. Retry with backoff. |
A failure after a stream has started arrives as an error event instead. See Error handling.