# API Errors

> The error object, what each type and status means, the request id, and the codes to handle.

Source: https://imario.ai/docs/api/errors

## The error object

A call that does not succeed answers with an HTTP status and one JSON object:

```json
{
  "error": {
    "type": "permission_error",
    "code": "insufficient_scope",
    "message": "This key does not have the `studies:write` scope.",
    "param": null,
    "request_id": "req_3f9a1c0b7d2e4a6f8b1c3d5e",
    "required_scope": "studies:write"
  }
}
```

| Field | Meaning |
|---|---|
| `type` | The kind of failure. Each type belongs to one status family (table below) |
| `code` | What went wrong, in stable snake_case. Branch on this |
| `message` | An English sentence for a person. It may be reworded; do not parse it |
| `param` | The request field at fault, when there is one, such as `audience_ids` or `cursor` |
| `request_id` | The request's id, the same as its `Request-Id` header |

Some errors add fields that help: `required_scope` and `required_role` on a permission error, `credits` or `cap` and `spent` on a credits error (a few calls name theirs differently, such as `required` and `remaining` on a chat message; the endpoint's reference says which), `maximum` when a limit is reached. A request that fails validation lists every field at fault, up to ten, in `errors`: `[{"param": "options", "message": "..."}]`.

## Types and statuses

| `type` | Status | Meaning |
|---|---|---|
| `invalid_request_error` | 400, also 405 and 413 | The request is wrong: a missing or invalid field, a method the route does not take, a file that is too large. Fix it before you retry |
| `authentication_error` | 401 | No credential, or one that is unknown, revoked or expired |
| `insufficient_credits_error` | 402 | Not enough credits, or the daily limit or a member's monthly cap would be passed. Nothing was charged |
| `permission_error` | 403 | The key lacks the scope or role, the address is not allowed, or the call is not open to this key |
| `not_found_error` | 404 | No such object in this workspace, or no such route |
| `conflict_error` | 409 | The object's state does not allow it now, or an `Idempotency-Key` clash |
| `rate_limit_error` | 429 | Too many requests for this key, too many studies queued or running at once, or a daily cap reached. Wait for `Retry-After` |
| `api_error` | 500, 502, 503 | A failure on iMario's side, a connected provider that did not answer, or a service that is briefly unavailable |

An object in another workspace answers 404, never 403, so an id from elsewhere reveals nothing.

## The request id

Every response, success or failure, carries a `Request-Id` header such as `req_3f9a1c0b7d2e4a6f8b1c3d5e`, and an error repeats it in `request_id`. Log it. **API activity** in **Settings > MCP & API** lists recent requests with their ids, and support can find a request from its id.

## Codes you will meet

| Code | Status | What to do |
|---|---|---|
| `missing_api_key` | 401 | Send `Authorization: Bearer <key>` |
| `invalid_api_key` | 401 | The key is unknown, revoked or expired, or it is a personal key whose holder left the workspace. Use a working key |
| `insufficient_scope` | 403 | Use a key that holds `required_scope`, or add the scope to a new key |
| `role_not_allowed` | 403 | The key acts with a role below `required_role`. Use a key whose holder or service role is high enough |
| `livemode_only` | 403 | Test keys cannot change members, keys or connections. Use a live key |
| `live_object_read_only` | 403 | A test key cannot change or delete a live object, such as a live Library item or a live report's share link |
| `ip_not_allowed` | 403 | The key's allowlist does not include this address |
| `account_suspended` | 403 | The account the key acts for is suspended. Contact support@imario.ai |
| `audience_rejected` | 403 | The workspace may not use one of the audiences. Read the reason; do not swap in another audience without asking |
| `parameter_invalid` | 400 | A field is missing or invalid. See `param` and `errors` |
| `kind_mismatch` | 400 | The request sent a field that another kind of study reads. Remove it |
| `cursor_invalid` | 400 | Pass back a `next_cursor` exactly as the API returned it |
| `idempotency_key_invalid` | 400 | An `Idempotency-Key` must be 1 to 255 characters |
| `insufficient_credits` | 402 | Add credits, or make the study smaller. Nothing was charged |
| `daily_budget_exceeded` | 402 | Today's API budget would be passed. Wait for 00:00 UTC, make the call smaller, or have an owner or admin raise the daily limit |
| `member_cap_exceeded` | 402 | The member's monthly cap in a pooled workspace would be passed |
| `resource_not_found`, and codes such as `study_not_found` | 404 | Check the id and that it belongs to this workspace |
| `route_not_found` | 404 | Check the method and the path, including `/v1` |
| `study_already_started` | 409 | Only a study that is still `queued` or `waiting_for_audience` can be canceled |
| `study_already_ended` | 409 | The study has already finished, so there is nothing to cancel |
| `idempotency_key_reused` | 409 | The key was used with a different request. Use a new key for a new request |
| `request_in_progress` | 409 | The first request with this key is still running. Retry shortly |
| `key_limit_reached` | 409 | The workspace holds 50 working keys. Revoke one first |
| `rate_limited` | 429 | Wait the seconds in `Retry-After`, then slow down |
| `too_many_auth_failures` | 429 | Too many requests without a working key came from this address. Check the key, then retry after `Retry-After` |
| `too_many_active_runs` | 429 | The key's member already has the most studies queued or running at once. Wait for one to finish (`Retry-After` 30). Nothing was charged |
| `screened_audience_cap` | 429 | Today's limit on new screened audiences is reached. Try again tomorrow (`Retry-After` 3600) |
| `provider_unavailable` | 502 | The connected source (a CRM, a survey tool) did not answer. Retry later |
| `internal_error` | 500 | Retry with the same `Idempotency-Key`; if it keeps happening, send support the request id |
| `temporarily_unavailable` | 503 | Wait the seconds in `Retry-After` and retry |
| `reply_failed`, `insight_failed` | 503 | The person, or the AI read of a group, gave no answer. Nothing was charged for a message. Retry |

Other codes are specific to one endpoint and named in its [reference](https://imario.ai/docs/api/reference) entry, such as `no_difference` for an AI read of a group that answers like everyone else. New codes may appear in v1 (see [API versioning](https://imario.ai/docs/api/versioning)): treat an unknown code by its `type`.

## When to retry

- **429 and 503**: retry after the seconds in `Retry-After`.
- **500**: retry with the same `Idempotency-Key`, a few times, with growing waits.
- **409 `request_in_progress`**: retry after a few seconds.
- **Other 4xx**: do not retry the same request. Change it first.

A spending call that failed with a 4xx charged nothing. For POST requests, an `Idempotency-Key` makes every retry safe: see [Idempotent requests](https://imario.ai/docs/api/idempotency).
