# Error Handling > Error envelopes, every error code, and how to react to each Every QwryAI API error comes back as JSON with a machine-readable `code` and a readable `message`. Branch on `code`, never on `message`: messages can change, codes don't. The Chat API and the Tools API wrap errors slightly differently, so this page covers both. ## Error Response Format > [tab] Chat API > > Errors from `https://api.qwryai.com/api/v2` use this envelope: > > ```json > { > "error": { > "code": "ERROR_CODE", > "message": "Human-readable description", > "details": {} > } > } > ``` > > | Field | Type | Description | > | - | - | - | > | `code` | `string` | Machine-readable error code. Use it in your code. | > | `message` | `string` | Human-readable description. | > | `details` | `object` | Only on `VALIDATION_INVALID_BODY`: one entry per field that failed, keyed by field name. | > [tab] Tools API > > Errors from `https://api.qwryai.com/public/v1` set `success` to `false` and may add fields to `error`: > > ```json > { > "success": false, > "error": { > "code": "ERROR_CODE", > "message": "Human-readable description" > } > } > ``` > > | Field | Type | Description | > | - | - | - | > | `success` | `boolean` | Always `false` on an error; `true` with a `data` object on success. | > | `error.code` | `string` | Machine-readable error code. | > | `error.message` | `string` | Human-readable description. | > | `error.required` | `string` | On `INSUFFICIENT_PERMISSION`: the permission the tool needs. | > | `error.issues` | `array` | On `INVALID_ARGUMENTS`: `{ "path", "message" }` for each argument that failed. | > > > [!NOTE] > > > > A Tools API `429` has no `success` field: it's `{ "error": { "code": "INTEGRATION_RATE_LIMIT_EXCEEDED", "message": "…" } }`. ## Error Codes ### Chat API | Code | HTTP | Description | | - | - | - | | `VALIDATION_INVALID_BODY` | 400 | The body failed validation: a missing `message`, an unknown field, a `userId` with characters outside `a-z A-Z 0-9 . _ -`, or a body over 1 MB. `details` names each field. | | `VALIDATION_INVALID_JSON` | 400 | The body isn't valid JSON. | | `CHAT_RETRY_NO_USER_MESSAGE` | 400 | The message you asked to retry has no customer message before it. | | `AUTH_MISSING_API_KEY` | 401 | No `Authorization` header was sent. | | `AUTH_INVALID_API_KEY` | 401 | The header isn't `Bearer `, or the key doesn't exist or was deleted. | | `AUTH_EXPIRED_API_KEY` | 401 | The key has expired. Create a new one in **Workspace settings › API keys**. | | `CHAT_CREDITS_EXHAUSTED` | 402 | The workspace doesn't have enough message credits for this answer. Top up or upgrade in **Plans**. | | `AUTH_INSUFFICIENT_PERMISSIONS` | 403 | The key doesn't hold **Chat with agents**, or its creator is no longer in the workspace. | | `SUBSCRIPTION_API_RESTRICTED_PLAN` | 403 | The workspace's plan doesn't include API access. API keys need **Starter**, **Growth** or **Enterprise**. | | `ACCESS_COUNTRY_BLOCKED` | 403 | The request comes from a country the agent blocks in **Guardrails**. | | `CHAT_CONVERSATION_MISMATCH` | 403 | The conversation doesn't exist or belongs to a different agent. | | `AGENT_NOT_FOUND` | 404 | No agent with that id in your workspace. | | `RESOURCE_NOT_FOUND` | 404 | No such endpoint. Check the method and path. | | `RESOURCE_TOOL_CALL_NOT_FOUND` | 404 | No client action waiting for this `toolCallId`. It expired, or its result was already submitted. | | `CHAT_RETRY_MESSAGE_NOT_FOUND` | 404 | The `messageId` to retry isn't an agent answer in this conversation. | | `CHAT_CONVERSATION_NOT_ONGOING` | 409 | The conversation can't take a message now: it ended, a teammate took it over, or you asked for a retry while another answer was still being written or a client action was waiting for its result. | | `RATE_LIMIT_TOO_MANY_REQUESTS` | 429 | Too many requests, or too many open streams. Wait for `Retry-After` seconds. See [Rate limiting](/docs/api-reference/v2/authentication#rate-limiting). | | `INTERNAL_SERVER_ERROR` | 500 | Something failed on our side. If it keeps happening, contact support with the `x-request-id` value. | Once a stream has started, errors can't change the HTTP status. They arrive in the stream as an `error` event, followed by `finish` with `finishReason: "error"`. See [Streaming](/docs/api-reference/v2/streaming). ### Tools API | Code | HTTP | Description | | - | - | - | | `INVALID_ARGUMENTS` | 400 | The body isn't a JSON object, or an argument failed the tool's input schema. `issues` lists each one. | | `CHATBOT_ID_REQUIRED` | 400 | The tool works on one agent and the body has no valid `chatbotId`. Get one from `list_agents`. | | `TOO_MANY_CHANGES` | 400 | A single review can hold at most 20 changes. Split the request. | | `API_KEY_REQUIRED` | 401 | No credential was sent. | | `INVALID_API_KEY` | 401 | The API key is malformed, unknown or deleted. | | `INVALID_OAUTH_TOKEN` | 401 | The OAuth access token is malformed, unknown or expired. Refresh it. | | `AUTH_EXPIRED_API_KEY` | 401 | The key has expired. | | `API_ACCESS_PLAN_REQUIRED` | 403 | The workspace's plan doesn't include API access. | | `CREATOR_NOT_ACTIVE` | 403 | The member who created the credential is no longer in the workspace. | | `INSUFFICIENT_PERMISSION` | 403 | The credential doesn't hold the permission this tool needs; `required` names it. | | `UNKNOWN_TOOL` | 404 | No tool with that name for API clients. `GET /public/v1/tools` lists yours. | | `CHATBOT_NOT_FOUND` | 404 | No agent with that `chatbotId` in your workspace. | | `REVIEW_NOT_FOUND` | 404 | No review with that `reviewId` for this credential. A review can only be applied by the credential that prepared it. | | `NOTHING_TO_CHANGE` | 409 | The values you asked for are already set. | | `VALIDATION_INVALID_BODY` | 409 | The tool rejected the input on its own rules, such as a date range over 184 days. `message` says what to fix. | | `REVIEW_ALREADY_USED` | 409 | The review was already applied, or it failed. | | `REVIEW_EXPIRED` | 409 | More than 15 minutes passed since the review. Call the tool again. | | `STALE_REVIEW` | 409 | The resource changed after the review. Call the tool again for a fresh one. | | `RESULT_TOO_LARGE` | 413 | The answer is over 40,000 bytes. Use a narrower filter, a shorter date range or a later page. | | `INTEGRATION_RATE_LIMIT_EXCEEDED` | 429 | More than 2,000 requests in 15 minutes for this credential. Wait for `Retry-After` seconds. | | `TOOL_FAILED` | 500 | The tool failed on our side. Try again; if it keeps failing, contact support. | | `UNKNOWN_WRITE_OUTCOME` | 502 | The change was sent but not confirmed. Read the resource before you retry. | Some tools add codes of their own for problems in your workspace's data. Those use a `4xx` status with the same envelope. **Example with field-level details (`VALIDATION_INVALID_BODY`):** ```json { "error": { "code": "VALIDATION_INVALID_BODY", "message": "Invalid request", "details": { "message": "Required" } } } ``` **Example with argument issues (`INVALID_ARGUMENTS`):** ```json { "success": false, "error": { "code": "INVALID_ARGUMENTS", "message": "Invalid arguments for query_tickets: page: Invalid input: expected number, received undefined", "issues": [ { "path": "page", "message": "Invalid input: expected number, received undefined" } ] } } ``` ## Handling Errors ```javascript const response = await fetch( "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat", { method: "POST", headers: { Authorization: "Bearer YOUR_API_KEY", "Content-Type": "application/json", }, body: JSON.stringify({ message: "Hello", stream: false }), } ); if (!response.ok) { const { error } = await response.json(); const requestId = response.headers.get("x-request-id"); switch (error.code) { case "RATE_LIMIT_TOO_MANY_REQUESTS": { const retryAfter = Number(response.headers.get("Retry-After")); // Wait retryAfter seconds, then retry break; } case "CHAT_CREDITS_EXHAUSTED": // Tell the customer you'll get back to them; top up credits break; case "CHAT_CONVERSATION_NOT_ONGOING": // Start a new conversation without conversationId break; case "VALIDATION_INVALID_BODY": console.log("Fix these fields:", error.details); break; default: console.error(`[${error.code}] ${error.message} (request: ${requestId})`); } } ``` ```python import requests response = requests.post( "https://api.qwryai.com/public/v1/tools/query_tickets", headers={"Authorization": "Bearer YOUR_API_KEY"}, json={"chatbotId": "YOUR_AGENT_ID", "page": 1, "status": "open"}, ) body = response.json() if not response.ok: error = body["error"] if error["code"] == "INSUFFICIENT_PERMISSION": print("Create a key with:", error["required"]) elif error["code"] == "INVALID_ARGUMENTS": for issue in error["issues"]: print(issue["path"], issue["message"]) else: print(f"[{error['code']}] {error['message']}") else: tickets = body["data"]["items"] ```