Error handling

Check the HTTP status first. When the API returns a typed JSON error, its top-level _tag identifies the error and the other fields give details. Branch on the status and _tag, not on human-readable text, and handle unrecognized or non-JSON responses.

Example response

An invalid access token can return 401 Unauthorized with:

{
  "_tag": "AuthenticationError",
  "reason": "invalid_access_token"
}

Common status codes

StatusWhat to do
400 Bad RequestCheck the path, query, or body against the operation’s schema.
401 UnauthorizedCheck that the Bearer token is present, valid, and unexpired.
402 Payment RequiredCheck the organization’s subscription.
403 ForbiddenCheck the token’s scopes and the caller’s access to the organization and resource.
404 Not FoundCheck the ID and whether the caller can see the resource.
409 ConflictCheck whether the resource is in a state that allows this operation.
429 Too Many RequestsWait before retrying. See rate limits for the header and error body.
500 Internal Server ErrorCheck the result before repeating a write.
503 Service UnavailableThe operation may be temporarily unavailable. If the response includes Retry-After, wait at least that long.

Retry safely

  • Correct a rejected request before repeating it. For a 429, back off rather than immediately repeating it.
  • If a write times out or returns a server error, don’t assume it failed. Check the session or read its events before resending a request that could create a duplicate.
  • If an event stream fails after it opens, the HTTP status stays 200. Handle the stream failure and resume from your last saved event ID.

Not every response has a body. A successful session message send returns 204 No Content; don’t parse it as JSON or treat it as completion of the session’s work.