# Errors

> Every error has the same shape and a fixed status for each code. Branch on `code`. Show `message` to people.

## The shape

```json
{
  "error": {
    "code": "insufficient_scope",
    "message": "This key can only read. Make one that can act in Settings → AI agents.",
    "requestId": "req_8fK2mQ9xLp3VtR7nYc1B",
    "meta": {
      "required": "campaigns:write"
    }
  }
}
```

- **`code`** is stable. Write your logic against it.
- **`message`** is a sentence written for the person, and safe to show them. It can change, so never parse it.
- **`requestId`** is `req_` and 20 letters and digits. Every response, success or not, also sends it as the `Request-Id` header. Log it.
- **`meta`** is there when there's more to say, such as `meta.issues` for a failed validation or `meta.required` for a missing scope.

## Codes

| Code | Status | What it means | What to do |
| --- | --- | --- | --- |
| `invalid_request` | 400 | Bad JSON, a field that failed validation or that we don't know (`meta.issues`), an unknown query parameter, or no `Idempotency-Key` on an outbound call. | Fix the request. Don't retry it as it is. |
| `unauthorized` | 401 | The token is missing, wrong, expired or revoked, or it's a key from the other environment. | Refresh the OAuth token, or check the key and the host. The message names the right host for a key. |
| `insufficient_scope` | 403 | The token lacks the scope. `meta.required` names it, and `WWW-Authenticate` carries `scope=`. | Ask the person to connect again with that scope, or make a key that can act. |
| `forbidden` | 403 | Never allowed for this account: the wrong side (a creator calling a brand operation), email not confirmed, or the account is paused. | Show the message. The person sorts it out on yapfactory.com. |
| `not_found` | 404 | It doesn't exist, or it isn't yours. We never say which. | Check the id. Don't retry. |
| `conflict` | 409 | Its state doesn't allow this, such as editing a live brief. Also a request whose `Idempotency-Key` is still running. | Show the message, which is written for people. Read the thing again before deciding what to do. If `meta.handoff` is there, the step happens on yapfactory.com: give the person `meta.handoff.url`. |
| `idempotency_conflict` | 409 | The same `Idempotency-Key` came with a different body. | Use a new key for a new request. |
| `rate_limited` | 429 | Over the limit for this connection, or for the person across all their connections. | Wait `Retry-After` seconds, then retry. |
| `internal` | 500 | Our fault. | Retry with backoff. If it keeps happening, send us the `requestId`. |

## Validation errors

A `400 invalid_request` from a failed validation lists each problem in `meta.issues`, with the field's path, so you can point at the right input. A field we don't know is a problem too, so a typo never passes quietly.

```json
{
  "error": {
    "code": "invalid_request",
    "message": "Unknown field: brief.key_message. See https://yapfactory.com/developers/api/create_campaign_draft.",
    "requestId": "req_8fK2mQ9xLp3VtR7nYc1B",
    "meta": {
      "issues": [
        {
          "path": "brief.key_message",
          "message": "Unknown field"
        }
      ]
    }
  }
}
```

## Retrying

- Retry `429` after `Retry-After`, and `500` with backoff. Nothing else gets better by retrying.
- Send the same `Idempotency-Key` when you retry a `POST`, `PATCH` or `DELETE`. Then a retry can never do the thing twice. See [idempotency](https://yapfactory.com/developers/pagination.md#idempotency).

## On the MCP server

Tools return the same `message` as an `isError` result, so the assistant can tell the person what went wrong in plain words. There are no status codes or `code` values to branch on there: the sentence is the error.

Stuck? Email [support@yapfactory.com](mailto:support@yapfactory.com) with the `requestId`.

---

HTML version: https://yapfactory.com/developers/errors
