# Errors

Every error has the same shape:

```json
{
  "errors": [
    { "key": "validation", "field": "lines.0.quantity", "message": "The quantity must be at least 1." }
  ]
}
```

- `key` is stable: branch on it, never on `message`.
- `field` names the request field at fault on `422`.
- `scope` names the scope a `403 insufficient_scope` wanted; `permission` the permission a `403 permission_denied` wanted.
- `retry_after` (seconds) accompanies `425`, `429` and `503`, and the `Retry-After` header carries the same number.
- `correlation_id` appears on `500`; quote it to support.

## The keys

| Status | Key | When |
|---|---|---|
| 401 | `unauthenticated` | no bearer token |
| 401 | `token_invalid`, `token_expired` | the key is unknown, malformed or revoked |
| 401 | `wrong_env` | a test key on the live base URL or the reverse |
| 403 | `insufficient_scope` | the key lacks the operation's scope (`scope` says which) |
| 403 | `permission_denied` | the action is not available to your account (for example live orders before approval) |
| 404 | `not_found` | nothing you may see at that id; another merchant's record looks the same as a missing one |
| 409 | `version_conflict` | the `version` you sent is stale: reload the record and retry |
| 409 | `idempotency_conflict` | the `Idempotency-Key` was used with a different body |
| 409 | `customer_exists` and other domain conflicts | the operation names them in the reference |
| 422 | `validation` | a field is invalid; one entry per field |
| 422 | `idempotency_key_required` | the operation needs an `Idempotency-Key` |
| 422 | `invalid_sort`, `too_many_sorts`, `export_too_large` | list parameters out of bounds |
| 425 | `stale` | a read after a write is not yet visible; retry after `Retry-After` |
| 429 | `rate_limited` | over the limit: see [Rate limits](http://docs.staging.regoria.com/guides/rate-limits) |
| 500 | `internal_error` | our fault; the `correlation_id` identifies the request |
| 503 | `unavailable` | a dependency is down; retry after `Retry-After` |

Domain-specific keys (`webhook_endpoint_limit`, `customer_ref_immutable`, `setup_lines`, …) are listed on the operation that answers them.

## Retrying

- Retry `425`, `429` and `503` after `Retry-After` seconds, with a cap on attempts.
- Retry a timeout or connection error on an idempotent operation with the **same** `Idempotency-Key`.
- Never retry a `4xx` other than those three without changing the request.

## Correlation ids

Every response carries `X-Correlation-Id` (and the same value as `X-Request-Id`). Send your own on the request, under either name, to have it echoed; otherwise one is minted. Log it next to your own request id and quote it when you write to [support@staging.regoria.com](mailto:support@staging.regoria.com).

## Versioned writes

Records you update (`PATCH /v1/products/{product}`, `POST /v1/products/{product}/archive` and `/unarchive`) carry a `version`. Send the version you read; a `409 version_mismatch` means someone changed the record in between. Reload, reapply your change, send again.
