# Errors and rate limits

> What a refused request answers, how to repeat one safely, and when to wait and try again.

## Errors

A refused request answers one of these statuses:

| Status | Means                                                                                             |
| ------ | ------------------------------------------------------------------------------------------------- |
| `400`  | The request lacks something every such request needs, such as a required `Idempotency-Key`        |
| `401`  | No token, or an expired or unknown one: get a new one, see [Authentication](/docs/authentication) |
| `403`  | The token is valid, but lacks the route's scope                                                   |
| `404`  | Nothing of yours at this address: an unknown id, or another organization's                        |
| `409`  | The request does not fit the resource's state, or repeats an `Idempotency-Key` with another body  |
| `422`  | A field is missing or not valid                                                                   |
| `429`  | Too many requests: wait for `Retry-After` seconds                                                 |
| `500`  | The API failed: quote the `request_id` when you ask for help                                      |
| `503`  | Not now: wait for `Retry-After` seconds, then send the same request again                         |

The body is a Problem document (RFC 9457), sent as `application/problem+json`:

```json
{
  "type": "about:blank",
  "title": "Forbidden",
  "status": 403,
  "code": "insufficient_scope",
  "request_id": "0193b2f4-6c1d-7a3e-9b52-8f3c1d2e4b5a"
}
```

| Field        | Means                                                                          |
| ------------ | ------------------------------------------------------------------------------ |
| `code`       | What happened, in snake_case. Codes are stable: branch on them, not on `title` |
| `request_id` | The request, as YOU(th)'s logs name it. Quote it when you ask for help         |

| `code`                     | Status | Means                                                                 |
| -------------------------- | ------ | --------------------------------------------------------------------- |
| `idempotency_key_required` | `400`  | The route requires an `Idempotency-Key`, and the request has none     |
| `idempotency_key_invalid`  | `400`  | The `Idempotency-Key` is empty, or longer than 255 characters         |
| `invalid_token`            | `401`  | No token, an expired or unknown one, or one asked for without `scope` |
| `integration_revoked`      | `401`  | The integration was revoked: use another one's credentials            |
| `insufficient_scope`       | `403`  | The token lacks the route's scope                                     |
| `not_found`                | `404`  | Nothing of yours at this address                                      |
| `idempotency_conflict`     | `409`  | The `Idempotency-Key` was used with another body                      |
| `idempotency_in_progress`  | `409`  | A request with the `Idempotency-Key` runs now: wait for `Retry-After` |
| `internal_error`           | `500`  | The API failed                                                        |
| `identity_unavailable`     | `503`  | The API cannot check the token now: wait for `Retry-After`            |
| `timed_out`                | `503`  | The API gave up on the request: wait for `Retry-After`                |
| `temporarily_unavailable`  | `503`  | The API can't finish the request now: wait for `Retry-After`          |

A route's own page lists the codes only it answers. New codes can appear: treat one you do not know
by its status.

## Repeat a request safely

Send an `Idempotency-Key` header of your own, a UUID for example, of at most 255 characters, with a
`POST` that creates something. Within 24 hours of the latest attempt, the same key with the same
body answers what the first request answered, and creates nothing new. Use it whenever you try
again after a timeout. A request that was refused or failed keeps nothing: the same key and body
try again, and go on with what it left half done.

| Request                                                               | Answers                                         |
| --------------------------------------------------------------------- | ----------------------------------------------- |
| The same key, the same body                                           | What the first request did                      |
| The same key, another body                                            | `409`, `idempotency_conflict`                   |
| The same key, while a request with it runs                            | `409`, `idempotency_in_progress`, `Retry-After` |
| No key, on [`POST /v1/access-code-batches`](/docs/access-codes/order) | `400`, `idempotency_key_required`               |

Ordering a batch requires the key, because a repeat without one would order twice. Every other
`POST` that creates something accepts it.

## Rate limits

A client that sends too many requests gets `429`, and a request the API cannot serve now gets
`503`. When the answer carries a `Retry-After` header, wait that many seconds, then try again;
without one, wait a few seconds, and longer after each failure. Go by the status
of a `429`, whatever its body.

Ordering access codes also has a daily limit: your organization's batches ordered in one day, from
00:00 UTC, hold at most 1,000 codes together, unless your YOU(th) contact had it raised. An order past
it answers `422` `count_over_limit` and makes nothing: see
[The daily limit](/docs/access-codes/order#the-daily-limit).

A CSV file of access codes holds at most 50,000. A list longer than that answers `422`
`too_many_codes`: filter it by `batch_id` or a time range, or read it a page at a time as JSON. See
[Read the codes](/docs/access-codes/order#read-the-codes).
