# Errors

A refusal is an HTTP status and a body in the shape of [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) (`Content-Type: application/problem+json`):

```json
{
  "type": "about:blank",
  "title": "This key lacks the `write` scope",
  "status": 403,
  "code": "api.scope_missing",
  "requestId": "c4b2f0f8-7b0e-4a39-9a3e-1f7c2c6d9e51"
}
```

- **`code`** is stable and machine-readable. Branch on it — never on `title` or `detail`, which are written for people and may change or be translated.
- **`errors`**, on a validation failure (`request.validation_failed`) and on a malformed path id, names each field that was refused: `{ "code": ["Too small: expected string to have >=3 characters"] }`.
- **`context`**, on some refusals, carries facts you can act on: `{ "limit": 500, "remaining": 0, "resetsAt": "2026-11-25T09:12:00.000Z" }` on `bulk.over_daily_cap`, `{ "tierId": "…" }` on `order.sold_out`.
- **`requestId`** identifies the request in Lupeno's logs. Every answer, good or bad, also carries it in the `X-Request-Id` header. Send your own `X-Request-Id` (letters, digits, `.`, `_`, `:`, `-`, up to 128) and Lupeno uses it; otherwise it makes a UUID. Quote it when you write to us at [hello@lupeno.com](mailto:hello@lupeno.com).

## Statuses

| Status | Means | Do |
|---|---|---|
| `400` | The request is malformed: a `cursor` this API did not give out (`api.bad_cursor`), an id in the path that is not a UUID (`http.400`, with `errors` naming it), a create without an `Idempotency-Key` (`api.idempotency_key_required`) | Fix the request; retrying will not help |
| `401` | No key, an unknown key or a revoked one (`api.unauthorized`); a key past the end date it was made with (`api.key_expired`) | Check the `Authorization: Bearer …` header; replace an expired key with a new one |
| `403` | The key may not do this: a missing scope (`api.scope_missing`), a live key from Lupeno's own pages (`api.live_key_in_browser`) | Use a key with the right scope, from your server |
| `404` | No such thing — or not one this key can see (`not_found`). A key limited to one event gets `404` for every other event. A path that does not exist at all answers `http.404` | Check the id and the key's reach; check the path |
| `409` | The thing is not in a state that allows this (`order.sold_out`, `order.test_limit_reached`, `ticket.not_assignable` …), the organization's daily reach is spent (`bulk.over_daily_cap`, see [Limits](https://lupeno.com/developers/guides/getting-started#limits)), or an idempotent create is still running (`api.idempotency_in_flight`) | Read the `code`; retry an in-flight create shortly; after `bulk.over_daily_cap`, wait until `context.resetsAt` |
| `422` | The body or the query failed validation (`request.validation_failed`, with `errors`), or a value the rules refuse — a discount code that does not apply (`discount.*`), an `Idempotency-Key` reused with another body (`api.idempotency_key_reused`) | Fix the field `errors` names; read the `code` |
| `429` | Too many requests for this key (`request.rate_limited`) | Wait the `Retry-After` seconds, then continue |
| `5xx` | Our fault | Retry with backoff; creates with the same `Idempotency-Key` |

## Retrying

Retry `429`, `5xx` and network failures; never retry `4xx` unchanged. Back off exponentially (1 s, 2 s, 4 s … capped at a minute) and honour `Retry-After` when it is there. A create retried with its [idempotency key](https://lupeno.com/developers/guides/idempotency) can never happen twice.

## With the SDK

The client retries `429`, `5xx`, network failures and timeouts itself for what is safe to repeat — reads, `PUT`, `DELETE`, and creates with their `Idempotency-Key` — twice by default, honouring `Retry-After` up to a minute; `createLupeno({ retries, timeoutMs })` changes that ([the SDK guide](https://lupeno.com/developers/guides/sdk#retries-and-timeouts)). `unwrap` turns a refusal into a thrown `LupenoError` carrying `status`, `code`, `requestId` and `retryAfter` (seconds, when the API asked you to wait):

```ts
import { LupenoError, unwrap } from '@lupeno/sdk';

try {
  unwrap(await lupeno.POST('/v1/orders/{orderId}/cancel', { params: { path: { orderId } } }));
} catch (error) {
  if (error instanceof LupenoError && error.status === 404) {
    // already gone, or not this key's to see
  } else {
    throw error;
  }
}
```
