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.

StatusmessageWhat to do
400invalid_requestFix the body. assistantId and message are required, and assistantId must be a valid ID.
401unauthorizedCheck the API key. See Authentication.
404not_foundThe agent isn't in the API key's workspace. Check assistantId.
429too_many_requestsWait for Retry-After, then retry.
502upstream_errorThe agent couldn't reply this time. Retry after a short pause.
403insufficient_scopeThe key can't send messages. Give it Send on Conversations.
503service_unavailableCareflow 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.

StatusmessageWhat to do
401unauthorizedCheck 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.
403insufficient_scopeThe 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.
429too_many_requestsWait for Retry-After, then retry. The key's 100 requests a minute are shared with everything else it does.
503service_unavailableCareflow 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.

StatusmessageWhat to do
400invalid_request, invalid idFix the request. The publishable key must start with pk_, and a message can't be empty.
401unknown_keyThe publishable key doesn't belong to any widget. Copy it again from the install snippet.
401session_expired, unauthorizedCreate a new session with the same visitorId and retry.
403origin_rejectedAdd the page's website to Allowed website origins.
403channel_disabledThe widget is switched off. Turn Widget on back on.
403not_publishedPublish the agent.
403forbiddenThe conversation belongs to another visitor.
404not_found, not foundThe agent, conversation or help page doesn't exist.
409voice_unavailableThe agent doesn't have voice turned on.
429too_many_requestsWait for Retry-After, then retry.
429cap_exceededThe widget has had its 1,000 messages for the day (UTC). Messages work again the next day.
502upstream_errorThe 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}.

StatusmessageWhat to do
404webhook not found or disabledCheck the URL, and that When called from outside is on for the automation.
429too_many_requestsWait for Retry-After, then retry.
500failed to initiate run, failed to enqueue jobThe 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