> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chatling.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Errors

> The error format, error types, and error codes.

Chatling uses conventional HTTP status codes to indicate whether a request succeeded. Failed requests return a 4xx or 5xx status and a JSON body with an `error` object. Handle errors by `code`, which doesn't change, not by `message`, whose wording can.

## Error format

```json theme={null}
{
  "error": {
    "type": "not_found_error",
    "code": "resource_not_found",
    "message": "No conversation with this ID exists, or this API key can't access it.",
    "param": null,
    "request_id": "req_8fK2mQ9xLp4TzR7vWc1Ns3Yb"
  }
}
```

| Field | Type | Description |
| - | - | - |
| `type` | string | The type of error returned. See [Error types](#error-types). |
| `code` | string | A short string identifying the error. See [Error codes](#error-codes). Codes don't change, but new codes may be added. |
| `message` | string | A human-readable message with more details about the error. Don't parse it. |
| `param` | string or null | If the error is parameter-specific, the parameter related to the error, so you can point to it, such as by highlighting a form field. Nested fields use dot notation, such as `variables.plan`, or `member_ids.1` for the second item. `null` if no single parameter caused the error. |
| `request_id` | string | The ID of the request, also returned in the `X-Request-Id` header. Include it when you contact support. |
| `errors` | array | For `validation_failed` only. See [Validation errors](#validation-errors). |

## Validation errors

If parameters are invalid, the API returns 422 `validation_failed` and lists every invalid parameter in `errors`, each with its own `param` and `message`. The top-level `param` and `message` match the first entry.

```json theme={null}
{
  "error": {
    "type": "invalid_request_error",
    "code": "validation_failed",
    "message": "The name field is required.",
    "param": "name",
    "request_id": "req_3Hq7Vb2NcX9kLm4PzT6sWd1R",
    "errors": [
      { "param": "name", "message": "The name field is required." },
      { "param": "color", "message": "The color must be a hex color such as #3B82F6." }
    ]
  }
}
```

## Error types

| Type | Status | Description |
| - | - | - |
| `authentication_error` | 401 | No valid API key was provided. |
| `permission_error` | 403 | The API key or the project's plan doesn't allow the request. |
| `not_found_error` | 404, 410 | The resource doesn't exist, or the API key can't access it. |
| `invalid_request_error` | 400, 405, 409, 413, 422 | The request has invalid parameters or conflicts with the resource's current state. |
| `idempotency_error` | 409, 422 | An `Idempotency-Key` was reused for a different request, or its first request is still running. |
| `rate_limit_error` | 429 | Too many requests hit the API too quickly. Retry after `Retry-After` seconds. |
| `api_error` | 500, 503 | Something went wrong on Chatling's end. These are rare. |

## Error codes

Every `code` the API returns, grouped by `type`. New codes may be added, so handle unknown codes by their `type`.

### `authentication_error`

No valid API key was provided.

| Code | Status | Description |
| - | - | - |
| `missing_api_key` | 401 | No API key was provided in the `Authorization` header. |
| `invalid_api_key` | 401 | The API key doesn't exist or was revoked. |
| `api_key_expired` | 401 | The API key has expired. |

### `permission_error`

The API key or the project's plan doesn't allow the request.

| Code | Status | Description |
| - | - | - |
| `insufficient_permission` | 403 | The API key doesn't have permission to perform the request. |
| `project_suspended` | 403 | The project is suspended or no longer exists. |
| `plan_feature_unavailable` | 403 | The project's plan doesn't include this feature, such as API access, attachments, auto-sync, or a workflow action. |
| `agent_limit_reached` | 403 | The plan's limit on AI agents and chatbots was reached. |
| `agent_suspended` | 403 | The agent is suspended. It can't be updated, duplicated, or reply. |
| `ai_credits_exhausted` | 403 | The project has no AI credits left, so the agent can't reply. |
| `agent_credit_limit_reached` | 403 | The agent reached its AI credit limit. |
| `user_blocked` | 403 | The user is blocked from messaging this agent. |
| `knowledge_base_limit_reached` | 403 | The plan's knowledge base character limit was reached. |
| `resync_credits_exhausted` | 403 | The project has no resync credits left. |
| `invalid_attachment_url` | 403 | The attachment URL is invalid or expired. List the conversation's messages to get a new one. |

### `not_found_error`

The resource doesn't exist, or the API key can't access it.

| Code | Status | Description |
| - | - | - |
| `resource_not_found` | 404 | The resource doesn't exist, or the API key can't access it. |
| `route_not_found` | 404 | No endpoint matches the method and path. |
| `attachment_unavailable` | 410 | The channel no longer has the file. |

### `invalid_request_error`

The request has invalid parameters or conflicts with the resource's current state. Fix the request before retrying, except for `resource_busy` and `source_not_ready`, which you can retry unchanged later.

| Code | Status | Description |
| - | - | - |
| `invalid_json` | 400 | The request body isn't valid JSON. |
| `invalid_idempotency_key` | 400 | The `Idempotency-Key` header isn't 1 to 255 printable ASCII characters. |
| `method_not_allowed` | 405 | The endpoint doesn't support this HTTP method. |
| `request_too_large` | 413 | The request body is too large. |
| `validation_failed` | 422 | One or more parameters are invalid. `errors` lists each one. |
| `invalid_cursor` | 422 | The pagination cursor is invalid or expired. Start again from the first page. |
| `resource_busy` | 409 | Another request for this project is still running. Retry shortly. |
| `conversation_ended` | 409 | The conversation has ended and can't take new messages or attachments. |
| `reply_window_closed` | 409 | The channel's reply window is closed, so team members can't reply. |
| `wrong_channel` | 409 | The action is only available for conversations on the API channel. |
| `contact_already_exists` | 409 | A contact with this email already exists. |
| `message_not_rateable` | 409 | Only AI-written answers can be rated. |
| `channel_not_connected` | 409 | The channel isn't connected to the agent. Connect it in the dashboard first. |
| `unsupported_agent_type` | 409 | The endpoint is available for AI agents only, and the agent is a chatbot. |
| `whatsapp_send_failed` | 409 | Meta didn't send the WhatsApp template. `message` includes Meta's reason. |
| `system_variable_read_only` | 409 | System variables can't be updated or deleted. |
| `workflow_not_published` | 409 | The workflow was never published, so it can't be turned on or off. Publish it in the dashboard first. |
| `knowledge_base_linked` | 409 | The agent uses another agent's knowledge base and has no sources of its own. |
| `source_already_exists` | 409 | The knowledge base already has a source with this URL. |
| `source_not_ready` | 409 | The source is still training. Retry after it's processed or failed. |
| `resync_already_queued` | 409 | The source is already queued for a resync. |
| `resync_unavailable` | 409 | This type of source, such as a document, can't be resynced. |
| `upload_already_used` | 409 | The upload was already added as a knowledge base source. |
| `attachments_unavailable` | 409 | The conversation doesn't accept these attachments in its current mode. |
| `too_many_pending_attachments` | 409 | Too many uploaded files are waiting to be sent. Send or delete some first. |
| `unsupported_file_type` | 422 | The file type isn't supported. |
| `file_too_large` | 422 | The file exceeds the size limit. |
| `file_type_not_allowed` | 422 | The AI agent's attachment settings don't allow this file type. |
| `invalid_image` | 422 | The image can't be read. |
| `attachment_rejected` | 422 | The image violates the content policy. |
| `too_many_attachments` | 422 | The message has more attachments than allowed. |
| `attachment_not_found` | 422 | An attachment doesn't exist in this conversation, or was already sent. |
| `attachment_not_allowed` | 422 | An attachment is no longer allowed in this conversation. |

### `idempotency_error`

The `Idempotency-Key` can't be used for this request.

| Code | Status | Description |
| - | - | - |
| `idempotency_key_reused` | 422 | The key was already used for a different request. |
| `idempotency_request_in_progress` | 409 | The first request with this key is still running. Retry shortly. |

### `rate_limit_error`

Too many requests hit the API too quickly. Retry after `Retry-After` seconds.

| Code | Status | Description |
| - | - | - |
| `rate_limit_exceeded` | 429 | The API key's rate limit, or the agent's per-user message limit, was reached. |

### `api_error`

Something went wrong on Chatling's end. Retry shortly.

| Code | Status | Description |
| - | - | - |
| `internal_error` | 500 | An unexpected error occurred. Include the `request_id` if you contact support. |
| `service_unavailable` | 503 | The API is temporarily unavailable. |
| `message_not_saved` | 503 | The team message couldn't be saved. If the request was Send a WhatsApp template, the template was already sent, so don't retry. |
| `end_failed` | 503 | The conversation couldn't be ended. |
| `upload_failed` | 503 | The file couldn't be stored. |
| `delete_failed` | 503 | The attachment couldn't be deleted. |
| `attachment_check_unavailable` | 503 | The image couldn't be checked against the content policy. |
| `analytics_unavailable` | 503 | Analytics couldn't be loaded. |
| `whatsapp_request_failed` | 503 | Meta couldn't be reached or rejected the request. |

## Handling errors

* Retry `rate_limit_error` after the number of seconds in `Retry-After`, and `api_error` after a short wait.
* Send `POST` requests with an [`Idempotency-Key`](https://docs.chatling.ai/api-reference/v3/introduction#idempotent-requests), so a retry doesn't run the request twice.
* Retry `resource_busy` and `source_not_ready` later, unchanged. Don't retry other errors without changing the request.

```javascript theme={null}
const response = await fetch(`https://api.chatling.ai/v3/conversations/${conversationId}/replies`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
    "Idempotency-Key": replyId,
  },
  body: JSON.stringify({ text: "Thanks, I've refunded your order." }),
});

if (!response.ok) {
  const { error } = await response.json();

  switch (error.code) {
    case "validation_failed":
      for (const problem of error.errors) {
        console.log(`${problem.param}: ${problem.message}`);
      }
      break;
    case "conversation_ended":
      // The conversation has ended, so don't retry.
      break;
    default:
      if (error.type === "rate_limit_error" || error.type === "api_error") {
        const seconds = Number(response.headers.get("Retry-After") ?? 2);
        // Wait, then send the same request with the same Idempotency-Key.
      }
      console.error(`[${error.code}] ${error.message} (request ${error.request_id})`);
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.