# API errors

When the REST API refuses a request, the answer is JSON with a stable code, a sentence for people, and whether sending it again can help.

## The error shape

```json
{
  "code": "validation_failed",
  "message": "The body field is required.",
  "request_id": "01M88Q4X0Q7ZH6VG1VA35RVVQJ",
  "retryable": false,
  "details": {
    "body": ["The body field is required."]
  }
}
```

| Field | What it is |
|---|---|
| `code` | A stable name for what went wrong. Branch on this, never on the message. |
| `message` | One or two sentences for a person. The wording can change. |
| `request_id` | The id of the request, the same value as the `X-Request-Id` header. Quote it when you ask us about a request. |
| `retryable` | `true` when the same request can succeed later without being changed. |
| `details` | More about the error, when there is more. Left out otherwise. |

## Status and code

The HTTP status tells you the kind of problem; the code says which one. Every code, with its status:

<div class="table-scroll"><table><thead><tr><th>Code</th><th>HTTP status</th><th>What it means</th><th>Can a retry help</th></tr></thead><tbody><tr><th scope="row"><code style="white-space:nowrap">access_denied</code></th><td>403</td><td>The key&#039;s scope or the person&#039;s role does not allow this.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">not_found</code></th><td>404</td><td>Nothing with that id exists in this workspace. An id that belongs to another workspace gives the same answer.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">validation_failed</code></th><td>422</td><td>A value is missing or not valid. The message says which, and details names the fields when the request itself was malformed.</td><td>After the request is corrected</td></tr><tr><th scope="row"><code style="white-space:nowrap">version_conflict</code></th><td>409</td><td>What you sent was built on an older state: someone changed it, or it was already decided.</td><td>After reading the current state again</td></tr><tr><th scope="row"><code style="white-space:nowrap">quota_exceeded</code></th><td>429</td><td>A plan limit is reached, the plan does not include the feature, or the same thing was asked for too often in a short time. The message says which.</td><td>After the month turns, a plan change, or the wait the message names</td></tr><tr><th scope="row"><code style="white-space:nowrap">feature_unavailable</code></th><td>422</td><td>The feature or integration is switched off, not authorized yet, or not part of the plan.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">connection_required</code></th><td>422</td><td>The channel, ad account or CRM is not connected, or its access has expired.</td><td>After a person connects it again</td></tr><tr><th scope="row"><code style="white-space:nowrap">needs_authorization</code></th><td>422</td><td>The integration is built but not authorized or configured yet, so it cannot be used.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">approval_required</code></th><td>422</td><td>A person has not approved this in the app yet.</td><td>After a person approves in the app</td></tr><tr><th scope="row"><code style="white-space:nowrap">approval_invalid</code></th><td>409</td><td>The approval no longer covers this exact content, channel and time. Nothing was sent.</td><td>After a new approval</td></tr><tr><th scope="row"><code style="white-space:nowrap">provider_rejected</code></th><td>422</td><td>The outside service refused the request.</td><td>Only when the message names something you can change</td></tr><tr><th scope="row"><code style="white-space:nowrap">provider_unavailable</code></th><td>503</td><td>The outside service did not answer or is having trouble.</td><td>Yes, later</td></tr><tr><th scope="row"><code style="white-space:nowrap">publication_uncertain</code></th><td>422</td><td>The channel gave no clear answer, so it is not known whether the post exists. The post is marked as uncertain until a person says what happened.</td><td>No. A person checks the channel first</td></tr><tr><th scope="row"><code style="white-space:nowrap">duplicate</code></th><td>409</td><td>The same thing already exists or is already under way.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">consent_required</code></th><td>422</td><td>The person asked not to be contacted and is on the suppression list.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">unsupported</code></th><td>422</td><td>The channel, connection or format cannot do this.</td><td>No</td></tr><tr><th scope="row"><code style="white-space:nowrap">confirmation_required</code></th><td>422</td><td>An assistant must ask the person and send person_confirmed: true before this is recorded.</td><td>After the person says yes</td></tr><tr><th scope="row"><code style="white-space:nowrap">unauthenticated</code></th><td>401</td><td>The key is missing, mistyped, revoked or expired, or its workspace is no longer active.</td><td>After the key is corrected</td></tr><tr><th scope="row"><code style="white-space:nowrap">rate_limited</code></th><td>429</td><td>Too many requests in a minute.</td><td>Yes, when the minute has passed</td></tr><tr><th scope="row"><code style="white-space:nowrap">request_failed</code></th><td>4xx</td><td>Another refused request, for example a method the address does not take. The HTTP status says which.</td><td>After the request is corrected</td></tr><tr><th scope="row"><code style="white-space:nowrap">server_error</code></th><td>5xx</td><td>Something failed on our side. The request_id finds it in our logs.</td><td>Yes, later. Send the same Idempotency-Key</td></tr></tbody></table></div>

For a call to the REST API, some of them mean something more exact:

- `unauthenticated` and `access_denied`: see [Authentication](https://heymysocial.com/docs/api/authentication).
- `not_found` is also the answer for an address that does not exist.
- `request_failed` with the status `405`: the address exists but not with that method.
- `version_conflict`: the item changed since the `lock_version` you sent.
- `duplicate`: the `Idempotency-Key` was used with another request, or the first request with it is still running.
- `validation_failed` also covers a thing that is not in a state that allows the request.
- `quota_exceeded`: the workspace is read-only because the plan allows fewer workspaces than the organization has. Waiting does not help.
- `server_error` with the status `503`: the service is briefly unavailable, and `retryable` is `true`.

An unexpected failure on our side answers `500`. Its body is JSON but may hold only a `message`, without a `code`. Treat every `5xx` as something to retry.

## Validation details

For `validation_failed`, `details` usually has one entry for each field that was refused, and each entry is a list of sentences. Nested fields are written with dots, items of a list with their position:

```json
{
  "code": "validation_failed",
  "message": "account.currency is required: amounts are never stored without a currency and it is never guessed.",
  "request_id": "01M88Q4X5Y22WW5DJB5RA9FCXX",
  "retryable": false,
  "details": {
    "account.currency": ["account.currency is required: amounts are never stored without a currency and it is never guessed."]
  }
}
```

Some errors carry other facts in `details` instead:

| Where | `details` |
|---|---|
| A planned time the clocks skip | `{"reason": "dst_gap"}` |
| `version_conflict` | `{"current": 3}`: the `lock_version` the item has now |
| An approval request that is not ready | `{"issues": [...]}`: what the checks found |
| `quota_exceeded` | `{"limit_key": "workspaces", "plan": "..."}` |

## Errors inside a batch

The operations that take a batch do not fail as a whole because one item is wrong. They answer `200` and report each item:

- [Send CRM records in a batch](https://heymysocial.com/docs/api/operations/send-crm-records): `failed`, and `result: "error"` on the record.
- [Import ad results](https://heymysocial.com/docs/api/operations/import-ads): `rows_failed`, and `errors` with the index of each row.
- [Send events](https://heymysocial.com/docs/api/operations/ingest-events): `rejected`, and `status: "rejected"` with `errors` on the event.

So a `200` is not the end of the check. Read the counts.

## How to retry

Retry when `retryable` is `true`, when the status is `5xx`, and when no answer arrived at all. Do not retry anything else: the same request will get the same answer.

1. Send the retry with the same `Idempotency-Key` and the same body as the first attempt. If the first attempt did go through, you get its answer back instead of a second draft or a second import. See [idempotency](https://heymysocial.com/docs/api#idempotency).
2. Wait before each retry, and wait longer each time: for example 1, 2, 4, 8 seconds, each with a little random spread, and stop after a handful of attempts.
3. After `429` `rate_limited`, wait the number of seconds in the `Retry-After` header.
4. After `409` `duplicate` with `retryable: true`, the first request is still running. Wait a second or two and send it again to get its answer.
5. After a `4xx` that is not retryable, change the request. A changed request needs a new `Idempotency-Key`.

Keep the `request_id` of a failure you cannot explain. With it we can find the request. One answer leaves it out of the body: a single event that is not valid ([Send events](https://heymysocial.com/docs/api/operations/ingest-events)). The `X-Request-Id` header is still there.
