# Errors

> The API's three error shapes, how to read a validation 400, when to retry and the whole catalog with its exact texts.

The API's errors have the same shape as production's. There are three envelopes, depending on who answers:

| Who answers        | Shape                               | When                                                          |
| ------------------ | ----------------------------------- | ------------------------------------------------------------- |
| The services       | `{error, cause, message?, values?}` | Validation (400) and business rules (403, 404, 409, 422, 500) |
| The token endpoint | `{error, error_description}`        | OAuth2 errors of `POST /v1/auth/token`                        |
| The gateway        | `{message}`                         | 401, 403, 404, 413 and 429 before reaching a service          |

**A business rule (422)**

```json
{
  "error": "UnprocessableEntityApiError",
  "cause": "insufficient COP balance to withdraw 150000.00"
}
```

Compare the HTTP status and `error`. The `cause` text is stable, but it is meant for people: show it in your logs, do not branch on it.

## Validation errors

A validation 400 carries `values`: one message per field, keyed by its JSON path, naming the validator that failed.

**400 · A person without names**

```json
{
  "error": "BadRequestApiError",
  "cause": "error in struct validation",
  "values": {
    "destination.firstName": "failed validation on the \"required_if\" tag with value required \"EntityType person\"",
    "destination.lastName": "failed validation on the \"required_if\" tag with value required \"EntityType person\""
  }
}
```

If the body is not JSON, or a field has another type (an amount as a string, say), the answer is a 400 without `values`, carrying the decoder's cause:

**400 · Amount sent as text**

```json
{
  "error": "BadRequestApiError",
  "cause": "invalid json body: json: cannot unmarshal string into Go struct field CreateTransferOutRequest.amount of type float64"
}
```

## When to retry

| Status             | What to do                                                                       |
| ------------------ | -------------------------------------------------------------------------------- |
| 400, 403, 404, 422 | Do not retry as is: fix the request or the state.                                |
| 401                | Request a new token and retry once.                                              |
| 409                | With `Idempotency-Key`, the first call is still running: retry in a few seconds. |
| 429                | Wait the seconds in `Retry-After`.                                               |
| 5xx or timeout     | On operations that move money, check the state before retrying.                  |

> **Never retry a withdrawal blindly**
>
> Every `POST /v1/transfer` creates a new withdrawal. On a 5xx or a timeout, confirm whether the withdrawal was created before sending it again: in production, in the customer portal (the API read is still a preview); in the sandbox, with `GET /v1/transactions` or by using `Idempotency-Key`.

Every response carries `X-Request-Id`. Include it when you write to support.

## Catalog

Every cause the API can answer. Each page of the [reference](https://developers.zulu.com.co/en/reference.md) lists its operation's.

| HTTP | error | cause | Description |
| --- | --- | --- | --- |
| 400 | `BadRequestApiError` | `error in struct validation` | One or more fields fail their validation. `values` carries one message per field, keyed by its JSON path, naming the Go validator that failed. |
| 400 | `BadRequestApiError` | `invalid json body: %s` | The body is not valid JSON, or a field has the wrong type (a string where a number goes, for example). Answered before the fields are validated. |
| 400 | `BadRequestApiError` | `invalid value for "%s": %s` | A URL parameter can not be converted to its type (for example `page=abc`). |
| 401 | — | `Unauthorized` | The `Authorization: Bearer <access_token>` header is missing, or the token is invalid or expired. |
| 401 | `access_denied` | `Unauthorized` | The `client_id` or the `client_secret` is not valid. |
| 403 | — | `Forbidden` | The `X-API-Key` is missing, unknown, revoked or does not belong to the credentials of the token. |
| 403 | `access_denied` | `Client is not authorized to access this API` | The credentials are valid but the client is not enabled to use the API (revoked credential or suspended workspace). |
| 403 | `ForbiddenApiError` | `the user is not verified` | The user behind the credentials is not verified. In the sandbox: the workspace "verified" setting. |
| 404 | — | `Not Found` | The route does not exist. |
| 404 | `NotFoundApiError` | `transaction "%s" not found` | No transaction with that id exists in your workspace. |
| 404 | `NotFoundApiError` | `rate "%s->%s" not found` | There is no rate configured for that currency pair. |
| 404 | `NotFoundApiError` | `quote "%s" not found` | No quote with that id exists in your workspace. |
| 404 | `NotFoundApiError` | `recharge link "%s" not found` | No link with that slug exists in your workspace. |
| 404 | `NotFoundApiError` | `bre-b key "%s" not found` | The key does not exist (in your workspace, or in the Bre-B directory when resolving it). Test data: `@notfound`. |
| 404 | `NotFoundApiError` | `qr code "%s" not found` | No QR code with that id exists in your workspace. |
| 404 | `NotFoundApiError` | `deposit "%s" not found` | No deposit with that id exists in your workspace. |
| 409 | `ResourceConflictApiError` | `idempotency key "%s" is still being processed` | Another call with the same `Idempotency-Key` is still running. Retry in a few seconds to receive its response. |
| 409 | `ResourceConflictApiError` | `bre-b key "%s" already exists` | You already registered that key. |
| 413 | — | `Request Too Long` | The body exceeds the maximum size (64 KB in the sandbox). |
| 422 | `UnprocessableEntityApiError` | `idempotency key "%s" was used with a different request` | The `Idempotency-Key` was already used in the last 24 hours with a different body or operation. Use a new key for every distinct operation. |
| 422 | `UnprocessableEntityApiError` | `the user does not have an entity to withdraw from` | The user has no entity to withdraw from. |
| 422 | `UnprocessableEntityApiError` | `the beneficiary with document "%s" is not verified` | The beneficiary registered with that document is not verified. Test data: document `1111111111`. |
| 422 | `UnprocessableEntityApiError` | `the name does not match the beneficiary registered with document "%s"` | The name sent shares no word with the registered beneficiary's (format, accents and order are tolerated). Test data: document `2222222222`, registered as "Ana Pérez". |
| 422 | `UnprocessableEntityApiError` | `insufficient COP balance to withdraw %.2f` | The COP balance does not cover the amount plus the dispersion fee. The figure in the message is that sum. |
| 422 | `UnprocessableEntityApiError` | `the user does not have a COP balance account` | The user has no COP balance account. In the sandbox: the workspace "COP account" setting. |
| 422 | `UnprocessableEntityApiError` | `bank with code "%s" is not supported` | The `bankCode` is not an available bank (see `GET /v1/banks`). Test data: `9999`. |
| 422 | `UnprocessableEntityApiError` | `account type "%s" is not supported` | Unsupported account type. Body validation stops it first (400), so in practice it does not appear. |
| 422 | `UnprocessableEntityApiError` | `transaction "%s" cannot move from %s to %s` | The requested status does not follow the current one (a `COMPLETED` transaction never changes again, and FX never goes through `SENT`). |
| 422 | `UnprocessableEntityApiError` | `the fx market is closed` | The FX market is closed (`REAL` hours: 08:05–15:30 on Bogotá business days). The sandbox defaults to `ALWAYS_OPEN`. |
| 422 | `UnprocessableEntityApiError` | `SAME_DAY settlement is not available after %s` | After the cutoff `SAME_DAY` is not available; quote `NEXT_DAY` or `SECOND_DAY`. |
| 422 | `UnprocessableEntityApiError` | `quote "%s" is expired` | The quote expired (it lives 30 seconds). Request a new one. |
| 422 | `UnprocessableEntityApiError` | `quote "%s" has already been used` | Each quote serves a single conversion. |
| 422 | `UnprocessableEntityApiError` | `origin or destination amount wasn't calculated with factor "%v"` | You sent `originAmount` and `destinationAmount` and they do not match the quote's factor. Send only one of them. |
| 422 | `UnprocessableEntityApiError` | `insufficient %s balance to convert %.2f` | The balance of the origin currency is not enough. |
| 422 | `UnprocessableEntityApiError` | `%s amount %.2f is below the minimum of %.2f` | One side of the conversion is below the minimum (`GET /v1/limits`). |
| 422 | `UnprocessableEntityApiError` | `%s amount %.2f is above the maximum of %.2f` | One side of the conversion exceeds the maximum (`GET /v1/limits`). |
| 422 | `UnprocessableEntityApiError` | `amount %v has too many decimals for %s (max %d)` | The amount has more decimals than its currency allows: COP in whole pesos, USD up to 2 decimals. |
| 422 | `UnprocessableEntityApiError` | `the workspace already has %d active recharge links` | You already hold the maximum of active links (`CREATED` or `AWAITING_PAYMENT`). Cancel one or wait until it is paid or expires. |
| 422 | `UnprocessableEntityApiError` | `amount %d is outside the allowed range of %d to %d` | The amount is outside the range allowed for PSE links. |
| 422 | `UnprocessableEntityApiError` | `recharge link "%s" is %s` | The link's status does not allow the operation (a `PAID` or `EXPIRED` link can no longer be cancelled or paid). |
| 422 | `UnprocessableEntityApiError` | `recharge link "%s" has no fixed amount: amount is required` | The link has an open amount: the simulator needs the `amount` the payer "typed". |
| 422 | `UnprocessableEntityApiError` | `amount %d does not match the amount of recharge link "%s"` | The link has a fixed amount and the `amount` sent differs. Omit it or send the same one. |
| 422 | `UnprocessableEntityApiError` | `the workspace already has %d bre-b keys` | You reached the workspace maximum of keys. Delete one to create another. |
| 422 | `UnprocessableEntityApiError` | `bre-b key "%s" is reserved` | Keys starting with `@zulu` and the sandbox test data are reserved. |
| 422 | `UnprocessableEntityApiError` | `bre-b key "%s" is blocked` | The key is blocked and does not receive payments. |
| 422 | `UnprocessableEntityApiError` | `the workspace does not have an active bre-b key` | To create a QR without `key`, the workspace needs at least one active key (`POST /v1/bre-b/keys`). |
| 422 | `UnprocessableEntityApiError` | `amount %d is above the bre-b maximum of %d` | Bre-B accepts up to 12,110,000 COP per transfer. |
| 422 | `UnprocessableEntityApiError` | `qr code "%s" is %s` | The QR code no longer takes payments: it expired, or it is dynamic and was already paid. |
| 422 | `UnprocessableEntityApiError` | `qr code "%s" is STATIC: amount is required` | A static QR has no amount: the simulator needs the payment's `amount`. |
| 422 | `UnprocessableEntityApiError` | `qr code "%s" is DYNAMIC: its amount can not be changed` | A dynamic QR already fixes the amount: omit `amount`. |
| 422 | `UnprocessableEntityApiError` | `the top-up would leave the %s balance above the sandbox cap of %.2f` | The simulated balance has a cap per currency (10,000,000,000 COP and 10,000,000 USD). |
| 422 | `UnprocessableEntityApiError` | `document type "%s" is not supported` | The `documentType` of a beneficiary being registered is not in the catalog (`GET /v1/document-types`) or does not apply to its entity type (`NIT` is for companies). It is checked on registration, like the bank; an already registered beneficiary keeps its document. |
| 422 | `UnprocessableEntityApiError` | `destination field "%s" is not valid` | A value being registered can not be stored: the account must have 1 to 20 digits (spaces and hyphens are ignored), the document 1 to 20 characters, and names can not be blank (up to 80 characters; the company name up to 200). The message names the field. |
| 429 | — | `Too Many Requests` | You exceeded the request limit of your credentials. Wait the seconds in `Retry-After`. |
| 429 | `too_many_requests` | `Too many requests` | Too many token requests. Reuse the current token until it expires (`expires_in`). |
| 500 | `InternalServerApiError` | `[code:1] unexpected error` | Internal error. On operations that move money, check the state before retrying so the operation is not duplicated. |
| 500 | `InternalServerApiError` | `unexpected error %s` | The withdrawal failed at an internal step. Check the withdrawal before retrying so the payment is not duplicated. |
| 501 | — | `Not Implemented` | The operation exists in the contract but its simulation is not built in the sandbox yet. |
