# API errors
The status codes the Crate API returns, the shape of an error body, and how to fix the common errors.
The API reports the result of each request with an HTTP status code. A code in the `2xx` range means it worked. Any other code is an error, and the body says what went wrong.
## The error body
Most errors are JSON with one `errors` key. It holds a message:
```json
{ "errors": "Token is missing the 'read_products' scope" }
```
When Crate rejects a record, `errors` lists the problems for each field:
```json
{ "errors": { "name": ["can't be blank"] } }
```
> [!NOTE]
> A `429` from the rate limit is plain text, not JSON. Check the status code before you parse the body.
## Status codes
| Status | Meaning | What to do |
| --- | --- | --- |
| `200 OK` | The request worked. The body has the result. | Nothing. |
| `201 Created` | A record was created. The body has the new record. | Store its `id`. |
| `204 No Content` | A record was deleted. There is no body. | Nothing. |
| `400 Bad Request` | A required parameter is missing. The message names it. | Fix the request. |
| `401 Unauthorized` | The key is missing, wrong, expired or revoked. | Check the header, or create a new key. |
| `403 Forbidden` | The key is valid but lacks the permission. The message names the scope. | Edit the key's permissions, or use another key. |
| `404 Not Found` | The record isn't in your store, or the path is wrong. | Check the id and the path. |
| `422 Unprocessable Entity` | Crate understood the data but rejected it. The body lists each problem. | Fix the fields and send again. |
| `429 Too Many Requests` | The key went over its limit. | Wait for the `Retry-After` time. |
| `500` and above | Something failed on Crate's side. | Retry after a short wait. |
## Fix the common errors
**401 "Missing bearer token".** The `Authorization` header didn't reach Crate, or it isn't in the form `Bearer <key>`. A key sent in the address instead of the header also gets this error.
**401 "Invalid or expired access token".** The key is wrong, past its expiry, or revoked. On the **API keys** page, an expired key shows **Expired** and a revoked key shows **Revoked**. Create a new key.
**403 "Token is missing the '…' scope".** A read needs the group's read scope. A create, change or delete needs its write scope. Open the key's menu, choose **Edit**, and tick the permission the message names.
**404 for a record you can see in the dashboard.** The key belongs to another store, or the id belongs to a different resource. A record from another store always returns `404`, never `403`.
**422 when you create or update a record.** Read the field names in `errors`. A blank name or a price that isn't a number are the usual causes.
## Handle errors in your code
- Branch on the status code, not the message text. Messages can change.
- Retry `429` and `5xx` responses, and wait longer after each try.
- Don't retry `400`, `401`, `403`, `404` or `422` unchanged. They fail the same way again.
- Log the status, the path and the `errors` value. Never log the key.
- If a write times out, you can't tell whether it worked. Look the record up before you create it again.
