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

# Errors

> The format of API errors, the error types and how to decide between fixing the request, trying again or escalating.

The API uses conventional HTTP status codes. `2xx` responses mean success, `4xx` mean a problem with the request and `5xx` mean a problem on Routa's side.

## Error format

API errors share the same structure, in the style of the payment APIs you probably already know:

```json theme={null}
{
  "error": {
    "type": "invalid_request_error",
    "code": "template_name_invalid",
    "message": "A template name may only contain lowercase letters, digits and underscores (a-z, 0-9, _), up to 512 characters.",
    "param": "name",
    "request_id": "req_01J8..."
  }
}
```

| Field | Description |
| - | - |
| `type` | The category of the error. See the table below. |
| `code` | **Stable**, machine-readable identifier. Use it in your logic. |
| `message` | Human-readable text, in English. It can change: do **not** parse it. |
| `param` | The request field at fault, when the API can point at it: on `request_invalid` and on template errors. It uses the request names, with positions starting at 0. See [Template errors](https://docs.routa.chat/en/errors/error-codes.md#templates). |
| `doc_url` | Reserved for a link to the documentation of the code. No error carries it today: ignore it if it shows up. |
| `details` | Optional object with actionable numeric or text values, such as `reason` (see below). |
| `request_id` | Request ID (`req_...`). Quote it when you contact support. |

> [!NOTE]
>
> `code` is part of the contract: it is never renamed. New codes can be added at any time, so handle unknown codes generically, using `type` and the HTTP status.

The same `request_id` also comes in the `Routa-Request-Id` header of **every** response.

### Actionable details

Some errors carry `details` with values you can use in your code:

| Code | Fields in `details` |
| - | - |
| `message_quota_exceeded` | `limit` (allowance for the period), `used` (used) and `resets_at` (when the allowance renews, ISO 8601). |
| `free_messages_exhausted` | `limit` (free messages) and `used` (used). It has no `resets_at`, because this allowance does not renew. |
| `project_limit_exceeded`, `channel_limit_exceeded` | `limit`. |
| `project_not_deletable` | `reason`: `has_enabled_channel` or `last_project`. |
| `insufficient_permission` | `permission` (the required permission). |
| `template_name_invalid` | `max_length`. |
| `too_many_webhook_endpoints` | `max`. |
| `content_rejected`, `invalid_recipient`, `template_variables_invalid`, `template_components_invalid`, `template_media_invalid`, `invalid_webhook_url`, `usage_range_invalid`, `invalid_project_name`, `invalid_api_key_name`, `invalid_external_ref` | `reason`, when the cause has a name, and the values quoted in the message. See [Reasons for each code](https://docs.routa.chat/en/errors/error-codes.md#reasons-for-each-code). |

## Error types

| `type` | Status | Meaning |
| - | - | - |
| `invalid_request_error` | `411`, `413`, `422` | The request is invalid. Fix it before you try again. |
| `authentication_error` | `401` | Missing, invalid, revoked or expired key. On partner routes, the partner key. |
| `permission_error` | `403` | The key is valid but lacks the required scope. |
| `not_found_error` | `404` | The resource does not exist **or** belongs to another project. |
| `conflict_error` | `409` | Idempotency or state conflict. |
| `billing_error` | `402` | A plan limit blocks the operation. |
| `rate_limit_error` | `429` | Rate limit. Wait for `Retry-After`. |
| `api_error` | `5xx` | Problem on Routa's side. You can try again. |

> [!NOTE]
>
> `billing_error` uses `402`, never `429`. Clients retry `429`, but waiting does not increase the allowance of a plan.

## HTTP status codes

| Status | When |
| - | - |
| `200` | Read or action completed. |
| `201` | Resource created (webhooks, templates, media). |
| `202` | `POST /v1/messages`: accepted, sending is asynchronous. |
| `400` | The body is not valid JSON. The response does not use the error format. |
| `401` | Missing or invalid API key. |
| `402` | Plan allowance or limit used up. |
| `403` | Insufficient scope. |
| `404` | The resource does not exist or belongs to another project. |
| `409` | Idempotency or state conflict. |
| `411` | `Content-Length` required on uploads that do not use chunked encoding. |
| `413` | Body larger than the route limit. |
| `422` | Request outside the documented format, or semantically invalid. |
| `429` | Rate limit. |
| `503` | Routa cannot process the request right now. Try again. |

## Malformed request

When the body, the query or the path do not have the documented format, for example because a required field is missing or a field has the wrong type, the API responds `422 request_invalid`:

```json theme={null}
{
  "error": {
    "type": "invalid_request_error",
    "code": "request_invalid",
    "message": "\"channel\" is missing or does not have the documented format.",
    "param": "channel",
    "request_id": "req_01J8..."
  }
}
```

`param` names the field, such as `template.button_parameters.0.index`. It is absent when the API cannot point at a field, and it never repeats a key you made up, such as one in `metadata`. The response does not return the value you sent.

A body that is not valid JSON gets a plain text `400` (`Bad Request`), without the `error` object.

## How to handle each error

> [accordion] 4xx (except 409 and 429): fix the request
>
> Trying again without changing anything is pointless. Read `code`, `param` and `message`, fix it and send another request. The exception is `409 idempotency_in_progress`, which is worth a new attempt.

> [accordion] 429: wait and try again
>
> Respect the `Retry-After` header and use exponential backoff with jitter. See [Rate limits](https://docs.routa.chat/en/reliability/rate-limits.md).

> [accordion] 5xx: try again with the same idempotency key
>
> Failures on Routa's side are safe to retry **if you use `Idempotency-Key`**. Resend with the same key. See [Idempotency](https://docs.routa.chat/en/reliability/idempotency.md).

> [accordion] Network failure or timeout
>
> You do not know whether the request arrived. Resend with the same `Idempotency-Key`. It guarantees there is no duplicate message.

## Errors in the SDK

The SDK turns error responses into typed classes. See [Error handling](https://docs.routa.chat/en/sdk/error-handling.md).

## Code catalog

See every code, with its cause and fix, in [Error codes](https://docs.routa.chat/en/errors/error-codes.md).


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