# Errors and rate limits

> The error body, what the SDK does with it, every code an agent can receive, and the limits behind a 429.

When the API refuses a call, it answers with a `4xx` or `5xx` status and the same JSON body every time. The status says what kind of failure it is; the `code` in the body says which one, and is what your code should branch on. This page lists every code an agent can receive, and the limits behind a `429` and a `402`.

## The error body

Say `invoice-flow` asks about Kiwi Studio's invoice with a `variant` its `File it` button does not have. Nothing is created, and the API answers `422`:

```json
{
  "error": {
    "code": "surface_invalid",
    "message": "The surface does not conform to the catalog, so the app could not draw it.",
    "details": [
      {
        "pointer": "surface.file.variant",
        "message": "must be equal to one of the allowed values",
        "component": "file",
        "property": "variant"
      }
    ]
  }
}
```

- `error.code`: one of the codes below. It does not change once published, so branch on it.
- `error.message`: a sentence for the person reading the log. It can change; do not parse it.
- `error.details`: only when the body you sent is at fault, one entry per problem. `pointer` is a dotted path into what you sent, `notification.quickActions[1].name`; `message` says what is wrong there. A surface that fails the [catalog](https://nibify.app/catalog) also names the `component` by the `id` you gave it, and its `property`, so you can search your own code for both.

## In the SDK

`@nibify/sdk` turns every failure into a `NibifyError` with the API's `status` and `code`, and its `details`. A call that never got an HTTP answer has `status: null` and `code: 'network_error'`; an answer with no code of ours has `http_<status>`.

```ts
import { NibifyError } from '@nibify/sdk';

try {
  await nibify.ask(surface, options);
} catch (error) {
  if (error instanceof NibifyError && error.code === 'surface_invalid') {
    for (const detail of error.details) console.error(detail.pointer, detail.message);
  }
  throw error;
}
```

The SDK retries a network failure, a `429` and a `5xx` on its own, waiting for `Retry-After` when there is one. It never retries `notify()`, `cancel()` or `nudge()`, and no other status is retried: a `401`, `402`, `404`, `409` or `422` will answer the same way until you change the call. Which calls are retried and how to send a request again without asking twice are in [Idempotency and retries](https://nibify.app/docs/idempotency-and-retries); the class itself is in the [SDK reference](https://nibify.app/docs/sdk#nibifyerror).

## Codes

The codes the endpoints of the agent API name in their responses, by status. The [REST reference](https://nibify.app/docs/rest/requests) lists them again under each endpoint.

| Code | Status | Description |
| --- | --- | --- |
| `unauthorized` | `401` | The credential is missing, malformed or unknown. Send it as `Authorization: Bearer <token>`. From every endpoint. |
| `api_key_revoked` | `401` | The API key was revoked. Create a new one in the dashboard. From every endpoint. |
| `plan_limit_reached` | `402` | The account’s plan allows no more of this: its Messages for the month in this Environment, `test` and `live` counted apart, or its Projects. Nothing was created. Not worth retrying until the month turns or the plan changes. From `POST /v1/requests` and `POST /v1/notifications`. |
| `request_not_found` | `404` | No Request with that id is visible to this credential. From `GET /v1/requests/{id}`, `DELETE /v1/requests/{id}`, `POST /v1/requests/{id}/nudge` and `GET /v1/requests/{id}/response`. |
| `thread_not_found` | `404` | No Thread with that id or key is visible to this credential. From `GET /v1/threads/{key}`. |
| `request_already_answered` | `409` | The Request was already answered, and the answer can be read. From `DELETE /v1/requests/{id}` and `POST /v1/requests/{id}/nudge`. |
| `request_already_expired` | `409` | The Request expired before it was answered. From `DELETE /v1/requests/{id}` and `POST /v1/requests/{id}/nudge`. |
| `request_already_dismissed` | `409` | The person already dismissed the Request. From `DELETE /v1/requests/{id}` and `POST /v1/requests/{id}/nudge`. |
| `request_already_cancelled` | `409` | The agent already cancelled the Request. From `DELETE /v1/requests/{id}` and `POST /v1/requests/{id}/nudge`. |
| `invalid_request_body` | `422` | The body or the query string does not match the expected shape. `details` points at each field. From `GET /v1/requests`, `POST /v1/requests`, `GET /v1/requests/{id}/response`, `POST /v1/notifications`, `GET /v1/threads` and `GET /v1/threads/{key}`. |
| `idempotency_conflict` | `422` | The Idempotency-Key was already used for a different body. Use a new key, or resend the original body unchanged. From `POST /v1/requests`. |
| `callback_url_invalid` | `422` | `callbackUrl` cannot be called: only public `https` endpoints are. From `POST /v1/requests`. |
| `surface_invalid` | `422` | The surface does not conform to the catalog, or does not fit the endpoint. `details` names the component and property. From `POST /v1/requests` and `POST /v1/notifications`. |
| `rate_limited` | `429` | A limit is spent: the Project’s per-second or daily one in this Environment, or the attempts at an authentication endpoint. Retry after the seconds in `Retry-After`. From every endpoint but `GET /v1/requests/{id}/response`. |

### Any endpoint

These can come back from any call, whatever the endpoint: a body that is not JSON or is too large, a path that does not exist, a failure on our side. The SDK retries an `internal_error` like any `5xx`; a `request_rejected` carries the status it was refused with.

| Code | Status | Description |
| --- | --- | --- |
| `malformed_request` | `400` | The request could not be read: the body is not valid JSON, or is cut short. |
| `not_found` | `404` | No endpoint answers this method and path, or a value in the path cannot name anything. |
| `payload_too_large` | `413` | The body is larger than the API accepts. |
| `unsupported_media_type` | `415` | The body is not `application/json`. |
| `request_rejected` | `4XX` | Any other request the API could not accept. The status says which. |
| `internal_error` | `500` | Something failed on Nibify’s side. Retrying may succeed. |

## Rate limits

Each project has two limits: a number of calls per second, and a volume over the last 24 hours. The day is a rolling window, so a burst at nine in the morning frees its budget at nine the next morning, not at midnight.

- **The budget is the project's.** Every API key of the project shares it, so a second key does not get a second budget.
- **`test` and `live` are counted apart.** A loop on `sk_test_…` cannot spend what your live agents need.
- **Only calls the API carries out count.** A call refused with `429` is neither carried out nor counted.
- **Waiting is free.** The long poll, `GET /v1/requests/{id}/response`, is never counted, however long and however often you wait.

Over either limit, the API answers `429` with `rate_limited` and a `Retry-After` header: a whole number of seconds, never below 1. The `message` says which of the two limits it was, and its value. When both are spent the per-second limit answers first. Wait for `Retry-After` and send again; the SDK does it for you.

```http
HTTP/1.1 429 Too Many Requests
Retry-After: 1
Content-Type: application/json

{
  "error": {
    "code": "rate_limited",
    "message": "…"
  }
}
```

## Plan limits

Your plan allows a number of live Messages a month, a number of `test` Messages a month, and a number of projects. The [pricing page](https://nibify.app/pricing) has the numbers.

- **Requests and notifications count together**, across every project of the account. A message you delete still counts.
- **`test` and `live` are counted apart.** Each has its own allowance, so a loop on `sk_test_…` never spends what your live agents need, and an `sk_live_…` key keeps working when the `test` allowance is spent. The demos the dashboard sends to your phone are not counted.
- **The month is the calendar month in UTC.** The count starts again on the first day.
- **We email you once a month**, when 80% of the live allowance is gone.

Once an allowance is spent, `POST /v1/requests` and `POST /v1/notifications` with a key of that environment answer `402` with `plan_limit_reached`, and nothing is created. There is no `Retry-After`: retrying is refused the same way until the month turns or the plan changes, so do not retry it. The `message` names the environment, the plan, its limit, the day the count starts again and where to upgrade. A retry with the `Idempotency-Key` of a request that was already created still returns it.

```http
HTTP/1.1 402 Payment Required
Content-Type: application/json

{
  "error": {
    "code": "plan_limit_reached",
    "message": "…"
  }
}
```

Many requests to the same person in a short time are never refused on that person's behalf. Every message reaches the inbox, and Nibify spaces out the pushes so the phone does not ring twenty times in a minute. That is about the sound, not about errors: no status or code tells you it happened.

## Next

- [Idempotency and retries](https://nibify.app/docs/idempotency-and-retries): Send a request again without asking twice, what the SDK retries on its own, and what to do with a 429.
- [SDK](https://nibify.app/docs/sdk): Every method and type @nibify/sdk exports: the client, ask(), notify(), the Request handle, the lists, webhooks.verify() and the component constructors.
- [Requests API](https://nibify.app/docs/rest/requests): Every /v1/requests endpoint: list the Requests of this Project, create a Request, read a Request, withdraw a Request, re-notify a pending Request, await the outcome of a Request.
