# Errors

> The API reports errors as RFC 9457 Problem Details with a request_id, a machine readable code, and an optional errors array.

Source: https://docs.wpwaterfall.com/developers/errors

Every error is an RFC 9457 Problem Details object with `type`, `title`, `status`, `detail`, `request_id`, and `code`. Branch on `code`, never on `detail`: we may reword a sentence, and a code stays. Field validation adds an `errors` array, and a conflict with a running job adds its `job_id`.

```json
{
  "type": "https://docs.wpwaterfall.com/developers/errors#missing_scope",
  "title": "Forbidden",
  "status": 403,
  "detail": "This API key is missing the users:write scope.",
  "request_id": "01J9K2Q7M4Z8X0T6Y3B5N1C2D4",
  "code": "missing_scope"
}
```

A validation error (`422`) lists each bad field.

```json
{
  "type": "https://docs.wpwaterfall.com/developers/errors#validation_failed",
  "title": "Unprocessable Entity",
  "status": 422,
  "detail": "Validation failed.",
  "request_id": "01J9K2Q7M4Z8X0T6Y3B5N1C2D4",
  "code": "validation_failed",
  "errors": [{ "field": "email", "message": "Enter a valid email address." }]
}
```

## What does type mean?

`type` has one meaning: the address of the problem's `code` on this page, `https://docs.wpwaterfall.com/developers/errors#<code>`. Open it to read what the code means. It always ends with the same word as `code`, so branch on `code` and never parse `type`.

## What do the status codes mean?

| Status | When |
|---|---|
| 400 | The request body is not valid JSON. |
| 401 | Missing or revoked key. |
| 402 | The company has no active site license, or every site license is in use, and the request creates a real site; or the action needs a payment method. `code` is `no_plan`, `plan_inactive`, or `no_free_credit`. Set `test` to `true` for a free test site. |
| 403 | Authenticated, but the key or role cannot do this. A session that needs a recent sign-in answers with `code: "step_up_required"`. |
| 404 | Unknown path or resource. |
| 409 | Conflict, such as a duplicate slug, a job already running, or a cap that waiting does not lift (the most test sites, the most open log streams). A running job answers with `code: "job_already_running"` and its `job_id`. Follow that job instead of starting it again. |
| 422 | Validation failed, a POST without an `Idempotency-Key`, an `Idempotency-Key` reused with a different body, or a WP-CLI command that is not on the allowlist. |
| 429 | Rate limited, or an allowance for an hour, a day, or a week is used up. Every 429 carries `Retry-After` with the seconds to wait. See [Rate limits](https://docs.wpwaterfall.com/developers/rate-limits). |
| 5xx | Our fault. Retry a POST with the same `Idempotency-Key`. |

## Which codes can I branch on?

When no specific code applies, `code` is the default of the status:

| Status | Default code |
|---|---|
| 400 | <span id="bad_request"></span>`bad_request` |
| 401 | <span id="unauthenticated"></span>`unauthenticated` |
| 402 | <span id="payment_required"></span>`payment_required` |
| 403 | <span id="forbidden"></span>`forbidden` |
| 404 | <span id="not_found"></span>`not_found` |
| 405 | <span id="method_not_allowed"></span>`method_not_allowed` |
| 409 | <span id="conflict"></span>`conflict` |
| 410 | <span id="gone"></span>`gone` |
| 413 | <span id="payload_too_large"></span>`payload_too_large` |
| 415 | <span id="unsupported_media_type"></span>`unsupported_media_type` |
| 422 | <span id="validation_failed"></span>`validation_failed` |
| 423 | <span id="locked"></span>`locked` |
| 428 | <span id="precondition_required"></span>`precondition_required` |
| 429 | <span id="rate_limited"></span>`rate_limited` |
| 500 | <span id="internal_error"></span>`internal_error` |
| 501 | <span id="not_implemented"></span>`not_implemented` |
| 503 | <span id="unavailable"></span>`unavailable` |

Any other status answers `http_` and the number, such as `http_418`. These specific codes say what to do next:

| Code | Status | What to do |
|---|---|---|
| <span id="missing_scope"></span>`missing_scope` | 403 | The API key lacks the scope this route needs. Use a key with that scope. |
| <span id="session_required"></span>`session_required` | 403 | Only a signed-in session can call this route. No API key can. |
| <span id="api_key_route_forbidden"></span>`api_key_route_forbidden` | 403 | No API key can call this route. |
| <span id="idempotency_key_required"></span>`idempotency_key_required` | 422 | Send an `Idempotency-Key` header on the POST. |
| <span id="idempotency_key_too_long"></span>`idempotency_key_too_long` | 422 | Use a key of 255 characters or fewer. |
| <span id="idempotency_key_reused"></span>`idempotency_key_reused` | 422 | The key was used with another body. Send a new key. |
| <span id="idempotency_key_in_flight"></span>`idempotency_key_in_flight` | 409 | The first request with this key is still running. Wait for `Retry-After`, then repeat it. |
| <span id="job_already_running"></span>`job_already_running` | 409 | Follow the job in `job_id` instead of starting it again. |
| <span id="too_many_test_sites"></span>`too_many_test_sites` | 409 | The company holds the most free test sites it may. Delete one first. Waiting does not help. |
| <span id="too_many_log_streams"></span>`too_many_log_streams` | 409 | You already follow the most logs you may at once. Close one first. Waiting does not help. |
| <span id="too_many_live_streams"></span>`too_many_live_streams` | 409 | The company already holds the most live streams it may at once. Close one, or read the events with a normal request. |
| <span id="too_many_businesses"></span>`too_many_businesses` | 409 | One person can own only so many businesses. Close one you no longer use. Waiting does not help. |
| <span id="invitation_limit"></span>`invitation_limit` | 429 | The company sent its invitations for the hour. `Retry-After` says when it may send again. |
| <span id="add_on_request_limit"></span>`add_on_request_limit` | 429 | The company sent its add-on requests for the day. `Retry-After` says when it may ask again. |
| <span id="add_on_change_limit"></span>`add_on_change_limit` | 429 | The company changed add-ons as often as it may in an hour. `Retry-After` says when it may again. |
| <span id="transfer_limit"></span>`transfer_limit` | 429 | The company started its site transfers for the hour. `Retry-After` says when it may start another. |
| <span id="region_not_available"></span>`region_not_available` | 403 | The service is not available in the location the request came from. Sign up, new sites, and purchases are refused there. Nothing to retry. |
| <span id="backup_download_limit"></span>`backup_download_limit` | 429 | The company used its backup downloads for the week. `Retry-After` says when the week ends. |
| <span id="step_up_required"></span>`step_up_required` | 403 | The session needs a recent sign-in. Ask the person to confirm their password, then repeat the call. |
| <span id="wordpress_not_installed"></span>`wordpress_not_installed` | 409 | The environment has no WordPress, so this WordPress tool does not apply. |

A POST that failed with a 5xx is safe to retry with the same `Idempotency-Key` and body. Quote `request_id`, the status, and the `detail` when you write to support, and never send the key. It is also in the `x-request-id` response header.
