# Errors and how to handle them

REST errors always return an `error` object; branch on `code`, not on the `message` text.

> Source: https://tokenearly.com/en/developers/errors · Docs index: https://tokenearly.com/en/developers/llms.txt · OpenAPI: https://api.tokenearly.com/v1/openapi.json

## Format

```json
{
  "error": {
    "code": "invalid_api_key",
    "message": "This API key does not exist or has been revoked.",
    "docs": "https://tokenearly.com/en/developers/errors"
  }
}
```

Every REST error uses this shape, including unknown paths (404 `not_found`), wrong HTTP methods (405 `method_not_allowed`) and server errors (5xx `internal_error`). MCP responses stay JSON-RPC: protocol problems (unknown tool, invalid arguments) are a JSON-RPC `error`; API errors (key, permission, rate limit and so on) come back as a tool result with `isError: true` whose content is the same `{"error": {"code", "message"}}`.

## HTTP errors

| code | HTTP | Meaning and handling |
|---|---|---|
| `missing_api_key` | 401 | No key sent. Use `Authorization: Bearer <key>`. |
| `key_expired` | 401 | The trial key has expired. Apply for a new trial key, or subscribe and create a regular key. |
| `invalid_api_key` | 401 | The key does not exist, or it was reset or revoked. |
| `plan_required` | 403 | Your plan does not include the developer API, or this part of it. |
| `ip_not_allowed` | 403 | The key has an IP allowlist and the request came from an address outside it. |
| `invalid_cursor` | 400 | `after` / `before` is not a valid id (an event id for events, an alert id for my_alerts), both were sent, or (events only) `updated_after` was combined with `before`. |
| `invalid_parameter` | 400 | A parameter has the wrong type or value (e.g. `limit` out of range). |
| `not_found` | 404 | No event with this id in your topics (GET /v1/events/{id}), or the requested path does not exist (e.g. a mistyped endpoint). |
| `method_not_allowed` | 405 | The path exists but not for this HTTP method (e.g. POST /v1/events). REST endpoints are GET only; only MCP uses POST. The `Allow` header lists the supported methods. |
| `rate_limited` | 429 | Over this key's request rate (REST and MCP combined; the interval is `limits.rest_interval_seconds` in `/v1/me`); wait `Retry-After` seconds and retry. |
| `internal_error` | 500 | Server-side error (5xx, same format). Retry with exponential backoff (1, 2, 4… seconds, capped). During a brief restart you may also get 502 / 503 / 504 without a JSON body; retry them the same way. |

## Stream errors

The stream first sends an `op = "error"` message, then closes with the matching code; only `invalid_message` (a bad client frame) leaves the connection open:

| code | Close code | Meaning and handling |
|---|---|---|
| `missing_api_key` | 4001 | No key sent. Use `Authorization: Bearer <key>`. |
| `key_expired` | 4001 | The trial key has expired. Apply for a new trial key, or subscribe and create a regular key. |
| `invalid_api_key` | 4001 | The key does not exist, or it was reset or revoked. |
| `plan_required` | 4003 | Your plan does not include the developer API, or this part of it. |
| `ip_not_allowed` | 4003 | The key has an IP allowlist and the request came from an address outside it. |
| `invalid_cursor` | 4400 | The `after` connection parameter is not a valid event id; closed with 4400. Fix it before reconnecting. |
| `too_many_connections` | 4029 | This key's stream connections are all in use (see `limits.streams_per_key` in `/v1/me`), so the new one is refused. Close the old connection, or give the other machine its own key. |
| `invalid_message` | stays open | A WebSocket client frame was rejected: not a JSON object, no string `op`, an unknown `op`, or `topics` empty / not a list of strings. The connection stays open; fix the frame and resend. |
| `slow_consumer` | 4008 | Too many undelivered events; reconnect with `after` to resume. |

## When to retry

- `429`: wait `Retry-After` seconds, then retry.
- `5xx` and network errors: retry with exponential backoff (1, 2, 4… seconds, capped).
- `401` / `403` / `400`: do not retry; fix the key, plan or parameters first.
