Integrate the Askocto API
Create an API key, send your first chat request, handle errors and page through lists with the Askocto REST API in curl and JavaScript.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
Use the REST API when you want your own backend to talk to an agent, read conversations or manage tickets. This guide takes you from a new key to a working chat call, then covers the patterns you need in production: errors, pagination and streaming.
Create an API key
- Open Workspace Settings > API keys.
- Select Create key and give it a name that says where it will be used.
- Copy the key. It is shown once and starts with
oc_live_for production oroc_test_for testing. - Store it in an environment variable on your server.
Keys act on the whole workspace, so never ship one in a web page or a mobile app. For apps, use the Android SDK or the iOS SDK, which do not need a key.
Make your first request
All requests go to https://api.askocto.co, use JSON, and carry the key as a bearer token. Start by listing your agents.
curl https://api.askocto.co/v2/agents \
-H "Authorization: Bearer $OCTO_API_KEY"
You should see a JSON object with a data array of agents and a nextCursor value.
Send a chat message
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"}'
The same call in 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",
}),
})
const data = await res.json()
console.log(data.message.content, data.conversationId)
Send the returned conversationId with your next request to continue the conversation. Details are in Chat with an agent.
Handle errors
Errors use one format with a code, a message and a requestId. Check res.ok and read the code.
if (!res.ok) {
const { error } = await res.json()
if (error.code === "rate_limited") {
const wait = Number(res.headers.get("Retry-After") ?? 5)
// wait, then retry
}
throw new Error(`${error.code}: ${error.message} (${error.requestId})`)
}
Retry rate_limited and server_error with a growing delay. Do not retry invalid_request, unauthorized, forbidden or not_found. The full list is in Error handling.
Page through lists
List endpoints accept limit (1 to 100, default 20) and cursor, and return nextCursor. Keep requesting until it is null. See Pagination.
Stream answers
Set "stream": true on the chat request to receive server-sent events as the answer is written. See Streaming.
Tip: Use an
oc_test_key in development and in your test suite. Switch to anoc_live_key only in production configuration.