Errors
Every response shares one envelope. Branch on the HTTP status and the short reason in `message`, and show people the sentence in `error`.
Successes and failures come back in the same JSON shape. On success, code is SUCCESS and the result is in data. On failure, code is ERROR, message holds a short reason your code can branch on, and error holds a sentence for people.
{ "code": "SUCCESS", "message": "ok", "data": { "reply": "Yes, breakfast is included." } }
{ "code": "ERROR", "message": "invalid_request", "error": "assistantId and message are required" }
A few failures leave error out and put everything in message, such as webhook not found or disabled. Don't assume error is always there.
Conversations
These come from POST /v1/conversations/messages.
| Status | message | What to do |
|---|---|---|
| 400 | invalid_request | Fix the body. assistantId and message are required, and assistantId must be a valid ID. |
| 401 | unauthorized | Check the API key. See Authentication. |
| 404 | not_found | The agent isn't in the API key's workspace. Check assistantId. |
| 429 | too_many_requests | Wait for Retry-After, then retry. |
| 502 | upstream_error | The agent couldn't reply this time. Retry after a short pause. |
| 403 | insufficient_scope | The key can't send messages. Give it Send on Conversations. |
| 503 | service_unavailable | Careflow couldn't check the key just now. Retry after a short pause. |
Agents, knowledge, analytics, tools, variables and integrations
These endpoints are shared with the dashboard, so the same request works with an API key or a dashboard sign-in. The failures every one of them can return are below. The API Reference lists the ones specific to each endpoint, such as a missing field or an unknown ID.
| Status | message | What to do |
|---|---|---|
| 401 | unauthorized | Check the API key. A value that doesn't start with sk_ is read as a dashboard sign-in, so it fails with missing bearer token or query param or invalid or expired token. |
| 403 | insufficient_scope | The key doesn't have the access this request needs. The error names the scope, such as this API key needs the agents:write scope. A Manager can add it on the key's page. |
| 429 | too_many_requests | Wait for Retry-After, then retry. The key's 100 requests a minute are shared with everything else it does. |
| 503 | service_unavailable | Careflow couldn't check the key just now. Retry after a short pause. |
insufficient_scope is decided by the request's method. A GET needs read access to the area, and any other method needs write, including the few POST requests that only look things up, such as previewing a variable or asking an agent's knowledge a question.
Web widget
These come from the /v1/web/* endpoints.
| Status | message | What to do |
|---|---|---|
| 400 | invalid_request, invalid id | Fix the request. The publishable key must start with pk_, and a message can't be empty. |
| 401 | unknown_key | The publishable key doesn't belong to any widget. Copy it again from the install snippet. |
| 401 | session_expired, unauthorized | Create a new session with the same visitorId and retry. |
| 403 | origin_rejected | Add the page's website to Allowed website origins. |
| 403 | channel_disabled | The widget is switched off. Turn Widget on back on. |
| 403 | not_published | Publish the agent. |
| 403 | forbidden | The conversation belongs to another visitor. |
| 404 | not_found, not found | The agent, conversation or help page doesn't exist. |
| 409 | voice_unavailable | The agent doesn't have voice turned on. |
| 429 | too_many_requests | Wait for Retry-After, then retry. |
| 429 | cap_exceeded | The widget has had its 1,000 messages for the day (UTC). Messages work again the next day. |
| 502 | upstream_error | The agent couldn't reply this time. Let the visitor try again. |
In your chat window, treat origin_rejected, channel_disabled and not_published on page load as "show nothing". They mean the widget isn't meant to run on this page right now.
Automation triggers
These come from POST /webhooks/workflow/{webhookId}.
| Status | message | What to do |
|---|---|---|
| 404 | webhook not found or disabled | Check the URL, and that When called from outside is on for the automation. |
| 429 | too_many_requests | Wait for Retry-After, then retry. |
| 500 | failed to initiate run, failed to enqueue job | The run didn't start. Retry after a short pause. |
Retry the right failures
Retry 429, 500, 502 and 503. Never retry 403 with insufficient_scope: the key needs more access first. Wait for the Retry-After header on a 429, and back off for a few seconds on the others. Don't retry other 4xx responses: they need a different request, not the same one again.
A retried message can be answered twice if the first attempt actually reached the agent before your client gave up. Allow up to two minutes for a reply before timing out, so slow replies aren't retried.
async function careflow(path, options = {}, attempts = 3) {
for (let i = 1; ; i++) {
const res = await fetch(`https://api.trycareflow.com${path}`, {
...options,
headers: { "Content-Type": "application/json", ...options.headers },
});
const body = await res.json();
if (body.code === "SUCCESS") return body.data;
const retryable = [429, 500, 502, 503].includes(res.status);
if (!retryable || i >= attempts) {
const err = new Error(body.error || body.message);
err.status = res.status;
err.reason = body.message;
throw err;
}
const wait = Number(res.headers.get("Retry-After")) || 2 * i;
await new Promise((r) => setTimeout(r, wait * 1000));
}
}
Where to go next
- Rate limits explains the
429responses. - Authentication explains each
401. - The API Reference lists the exact responses for every endpoint.