> The index of all pages is at https://docs.afffast.com/llms.txt.

# Errors

The form of an error response, the error types and how to handle them.

When a request fails, the API returns an HTTP status code of 400 or more and a JSON body with the same form each time:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "parameter_invalid",
    "message": "The limit must be between 1 and 100.",
    "param": "limit",
    "doc_url": "https://docs.afffast.com/errors/parameter-invalid",
    "request_id": "req_3kQ9x7WmZpL2vNd8TfYb4A"
  }
}
```

| Field             | Meaning                                                                                    |
| ----------------- | ------------------------------------------------------------------------------------------ |
| `type`            | The class of the error. See the table below.                                               |
| `code`            | A stable code in snake_case. Use it in your code. It never changes for the same problem.   |
| `message`         | A text for a developer. It can change. Do not parse it.                                    |
| `param`           | The name of the parameter or the field that caused the error. It can be missing.           |
| `doc_url`         | A link to the [page of the code](https://docs.afffast.com/errors.md) with the causes and the fix.                     |
| `request_id`      | The ID of the request. It is also in the response header `Request-Id`. Send it to support. |
| `details`         | A list of places that caused the error. A file import lists the rows (100 at most).        |
| `request_log_url` | A link to the request in the logs of the dashboard. It can be missing.                     |

## Error types

| Type                    | Status codes       | Meaning                                                                     |
| ----------------------- | ------------------ | --------------------------------------------------------------------------- |
| `invalid_request_error` | 400, 404, 409, 422 | The request is not valid, or the object does not exist.                     |
| `authentication_error`  | 400, 401           | The key is missing, not valid, or in the URL.                               |
| `permission_error`      | 403                | The key cannot do this: a scope, a role, a mode or a plan limit.            |
| `rate_limit_error`      | 429                | Too many requests. See [Rate limits](https://docs.afffast.com/docs/rate-limits.md).                    |
| `idempotency_error`     | 400, 409           | A problem with the `Idempotency-Key`. See [Idempotency](https://docs.afffast.com/docs/idempotency.md). |
| `api_error`             | 500                | An error on our side.                                                       |

The status codes mean:

- **400**: the API cannot read the request (not JSON, a key in the URL, an unknown `Api-Version`, a bad `Idempotency-Key`).
- **404**: the object or the URL does not exist.
- **409**: the request conflicts with the state of the data or with an earlier request.
- **422**: a value is not valid. `param` names it.

## Handle errors

1. Switch on `error.code`, not on the message or on the status code alone.
2. Do not retry a `4xx` error. Fix the request. The only exception is `429`: wait for `Retry-After` seconds.
3. Retry a `5xx` error with a growing wait. Use an `Idempotency-Key` on a `POST`, and send a new key for a new try (see [Idempotency](https://docs.afffast.com/docs/idempotency.md)).
4. Keep `request_id` in your logs. Send it to support when you ask for help.

The API validates a request before it runs. A request that is not valid never changes data.

All error codes are in [Error codes](https://docs.afffast.com/errors.md).
