# Errors

> The error envelope, every public error code, and how to handle each.

Every failure answers with the same JSON envelope — `error.code`, a human-readable `error.message`, `error.meta` with details that depend on the code, and the request's `error.requestId` — and an HTTP status that follows from its code (for `internal`, from [where it failed](https://docs.koil.co/concepts/errors#platform-failures)).

For example, `POST /v1/publish-requests` sent without its `Idempotency-Key` header answers `400`:

```json
{
  "error": {
    "code": "validation_error",
    "message": "The Idempotency-Key header is required for this operation.",
    "meta": {
      "violations": [
        {
          "code": "missing_field",
          "path": "/headers/idempotency-key",
          "message": "Required header."
        }
      ]
    },
    "requestId": "req_Pp2rYsYGTAh0pZj1pvfF"
  }
}
```

Branch on `error.code`, never on `message`. The codes are stable; the table below is generated from the same table the API answers with.

| Code | Status | Retry | Meaning |
| --- | --- | --- | --- |
| `validation_error` | 400 | No | The request does not match the operation's input schema, fails an admission check, or the platform refused its content. `meta.violations` names every problem. |
| `not_supported` | 400 | No | The platform, collection or resource does not offer this operation, such as exporting a `key` Connection's credential, which Koil never returns. |
| `unauthorized` | 401 | No | No API key, or a key Koil does not accept (unknown, revoked, or with a prefix that does not match its mode). |
| `quota_exceeded` | 402 | No | A cap is reached: a free-plan cap, which upgrading lifts (`GET /v1/billing-account` reports them), or a sandbox limit. `meta.limit` is the cap. |
| `permission_denied` | 403 | No | The API key is valid but lacks a permission the operation requires; `meta.required` names it. Use a key created with that permission. |
| `scope_insufficient` | 403 | No | The credential lacks a scope the operation needs; `meta` names the missing scopes. |
| `connection_invalid` | 403 | No | The platform credential behind the Profile no longer works; the end user must re-authorize. |
| `not_found` | 404 | No | The resource does not exist in this Organization and mode. |
| `conflict` | 409 | No | The request collides with current state: an idempotency key reused with a different payload or still in flight, a run already in progress, a publish that has already started, or a resource that already exists (`meta.resourceType` and `meta.resourceId` name it). Retry only when the response carries `Retry-After` (the key's first request is still in flight, or a sandbox Profile's collection hasn't had its first poll), after that many seconds. |
| `rate_limit_exceeded` | 429 | After Retry-After | Koil's own rate limit for the key, Organization, Profile, or client address is spent (`meta.budgets` names which); retry after `Retry-After` seconds. |
| `platform_rate_limit` | 429 | After Retry-After | The platform throttled the call or the platform quota is spent; retry after `Retry-After` seconds. |
| `internal` | 500, 502, 504 | With backoff | Koil or the platform failed, and the status says which: 500 when Koil failed, including a platform refusing how Koil made the call; 502 when the platform failed or answered something Koil cannot use; 504 when Koil's call to the platform timed out. The message is generic; retry with backoff. |
| `service_unavailable` | 503 | After Retry-After | A service Koil needs to accept the request (API key validation or rate limiting) is unavailable. Nothing was attempted; retry after `Retry-After` seconds. The hosting edge can deliver this as a `504` with neither this body nor `Retry-After`, so treat a `502`, `503` or `504` alike as retryable, with backoff. |

## Request ids

Every response, success or failure, carries a `Request-Id` header (`req_…`), and an error repeats it as `error.requestId`. Log it with your own request, and quote it when you contact support: it finds the request in Koil's logs and traces.

## Validation errors

Every request is validated once, against the operation's input schema, and every problem is reported together. A `validation_error` always carries `meta.violations`, one entry per problem:

-   `code` — what is wrong: `missing_field`, `unknown_field`, `schema_error` (the value does not match its schema), `invalid_value` (it matches but is not acceptable, such as a cursor Koil did not issue), `invalid_combination` (fields that cannot be used together), `invalid_request` (the request as a whole, such as a platform refusing it), or a check's own code such as `media_expired`. Branch on it, not on `message`.
-   `path` — a JSON pointer to the problem. Path parameters, query fields and body fields share one pointer space, as the API reference lists them: `/profileId`, `/cursor`, `/publishRequests/1/args/media/0`. A header is `/headers/<name>` in lowercase, such as `/headers/idempotency-key` in the example above, and `""` is the request as a whole.
-   `message` — a human-readable explanation.

Media that does not meet a destination's requirements is refused with codes of its own, at the asset's path ([when media is checked](https://docs.koil.co/guides/publishing#when-media-is-checked)):

-   `media_type_mismatch` — the destination, or this `variant`, takes no media of the asset's type (an image where a video is required), or takes it only as one of several assets, or only on its own.
-   `media_below_min_width`, `media_below_min_height`, `media_below_min_long_edge`, `media_below_min_short_edge` — the asset is smaller than the destination's minimum; Koil never upscales.
-   `media_aspect_ratio_too_narrow`, `media_aspect_ratio_too_wide` — the asset's shape is outside what the destination takes; Koil never crops.
-   `media_duration_too_long`, `media_duration_too_short`, `media_frame_rate_too_low` — the video is outside the destination's duration or frame-rate limits; Koil never trims or adds frames.
-   `media_file_too_large` — an image, audio file or document is over the destination's size limit (Koil converts a video down to it, but nothing else), or a converted video is still over it.
-   `media_format_unsupported` — the destination does not take the audio file's or document's format; Koil never converts audio or documents.
-   `media_incompatible` — converted to fit, the asset would break one of the limits above; the message gives the size it would have.
-   `media_unsupported` — Koil cannot convert the asset (an unreadable container or duration).
-   `media_transform_not_entitled` — the asset needs converting, which needs a paid plan.
-   `media_expired` — the `mediaId` is too old to publish; upload it again.

Each message states the limit, and the asset's own value where it has one.

A `scope_insufficient` raised before the platform is called carries the same `meta.violations`, with `meta.required` and `meta.missing` naming the scopes.

A body field the schema does not declare is refused with the violation code `unknown_field` rather than silently dropped, so a misspelled optional field never passes as an omitted one. A body that must carry one of several fields and carries none, such as a post with no `text`, `link` or `media`, is refused with a `missing_field` at each of them, each message naming them all. Query-string values are converted to the schema's types before validation (`"true"` and `"1"` to a boolean, `"10"` to a number, a repeated key to an array); path parameters are strings.

## Platform failures

When a platform refuses a call, Koil maps it onto the codes above by whose fault it is: an expired or revoked credential is `connection_invalid`, a missing permission `scope_insufficient`, a platform throttle or spent quota `platform_rate_limit`, something deleted or gone `not_found`, an operation the platform does not offer `not_supported`, and a refusal of what you sent (too large, a duplicate post) `validation_error` with the platform's message. Platform response detail may appear in `meta` when it is safe to show.

Where the code alone does not say what to do, `meta.refusalReason` names why the platform refused, with one name across platforms, so you never key on a platform's own error numbers (`meta.platformCode`). It rides on whatever code the refusal maps to: a closed messaging window is a `validation_error` on Meta and a `platform_rate_limit` on TikTok. A failed publish request records it in its `error` too. The reasons are a closed set, published as the `RefusalReason` schema; treat one your client does not know as absent.

| Reason | Meaning |
| --- | --- |
| `messaging_window_closed` | The platform takes a message to this person only inside a window that has closed: WhatsApp's 24-hour customer service window, the Messenger and Instagram messaging window, or TikTok's allowance since the person last wrote. It reopens when the person writes again. |
| `recipient_rate_limited` | Too many messages to this one person in a short time; messages to others are unaffected. Retry after `Retry-After`. |
| `recipient_unavailable` | The person cannot receive messages from this Profile: they blocked it or deactivated their account, or, on WhatsApp, the number has no account or an outdated app. Retrying does not help. |
| `publishing_limit_reached` | The account's own publishing cap on the platform is spent: Instagram's 24-hour content publishing limit, YouTube's daily upload limit, or TikTok's limit on posts. Publish again once it resets. |

A failure on the platform's side, or in how Koil called it, is `internal`, never `validation_error`, and its status says where it happened: `502` when the platform failed (an outage, its own 5xx) or answered something Koil cannot use (a response missing the id of what it just created), `504` when Koil's call to the platform timed out, and `500` when Koil failed, including when the platform refused how Koil made the call (its method or encoding). Retry all three with backoff. `internal` errors never carry detail and never answer a 4xx status. A `503` is never the platform's: it is only `service_unavailable`, a service of Koil's own being unavailable.

`connection_invalid` and `scope_insufficient` both answer 403: the API key is fine, but the end user must re-authorize the Profile's Connection. `permission_denied` also answers 403, but it is about the key: it lacks a permission the operation needs (`meta.required`), and a key created with it succeeds. Only `unauthorized` (401) means the API key itself was refused.

## Retrying

The table's **Retry** column says what to do with each code: wait out the `Retry-After` header's seconds, retry with exponential backoff, or stop — those fail the same way until the request, the credential, or the resource changes. Writes that take an [idempotency key](https://docs.koil.co/concepts/idempotency) are safe to retry with the same key and payload: a request that failed runs again without repeating what it already did, and one that succeeded answers its result again.
