# Error Codes

All API errors follow a consistent JSON format:

```json
{
  "error": {
    "code": "ERROR_CODE",
    "message": "Human-readable description"
  }
}
```

## Complete Error Reference

### 400 — Bad Request

| Code                     | Description                                                                              |
| ------------------------ | ---------------------------------------------------------------------------------------- |
| `INVALID_FIELD`          | Field value is invalid (empty, wrong characters, too long)                               |
| `INVALID_SEQUENCE`       | Journal entry sequence must start at 1 and increment without gaps                        |
| `INVALID_BALANCE_POLICY` | Unknown `balance_policy` value (must be `ALWAYS_POSITIVE`, `ALWAYS_NEGATIVE`, or `NONE`) |

### 401 — Unauthorized

| Code           | Description                                             |
| -------------- | ------------------------------------------------------- |
| `UNAUTHORIZED` | Missing or invalid `X-Api-Key` / `X-Api-Secret` headers |

### 403 — Forbidden

| Code                 | Description                                              |
| -------------------- | -------------------------------------------------------- |
| `INVALID_CREDENTIAL` | `api_secret` does not match the expected HMAC derivation |

### 404 — Not Found

| Code                        | Description                                    |
| --------------------------- | ---------------------------------------------- |
| `ACCOUNT_NOT_FOUND`         | Account UUID does not exist                    |
| `JOURNAL_ENTRY_NOT_FOUND`   | Journal entry UUID does not exist              |
| `TRANSACTION_NOT_FOUND`     | Transaction UUID has no journal entries        |
| `IDEMPOTENCY_KEY_NOT_FOUND` | Key not found or expired (past configured TTL) |
| `CREDENTIAL_NOT_FOUND`      | `api_key` not found or already revoked         |

### 409 — Conflict

| Code                        | Description                                                                                   |
| --------------------------- | --------------------------------------------------------------------------------------------- |
| `DUPLICATE_IDEMPOTENCY_KEY` | Key already used within the idempotency window                                                |
| `DUPLICATE_API_KEY`         | `api_key` already exists when registering a credential                                        |
| `INTEGRITY_VIOLATION`       | Row hash mismatch — possible direct database tampering. Also blocks ledger archive operations |

### 422 — Unprocessable Entity

| Code                 | Description                                                               |
| -------------------- | ------------------------------------------------------------------------- |
| `INSUFFICIENT_FUNDS` | `ALWAYS_POSITIVE` policy violated — balance would go below 0              |
| `INVALID_BALANCE`    | `ALWAYS_NEGATIVE` policy violated — balance would go above 0              |
| `ASSET_MISMATCH`     | Journal entry's declared asset does not match one of the account's assets |

### 500 — Internal Server Error

| Code             | Description             |
| ---------------- | ----------------------- |
| `INTERNAL_ERROR` | Unexpected server error |

## Handling Errors

### Retryable Errors

* **500 `INTERNAL_ERROR`** — may be transient; retry with backoff
* **Network timeouts** — the transaction may or may not have been committed; check `GET /idempotency/:key` or `GET /recovery` before retrying

### Non-Retryable Errors

* **400** errors — fix the request payload
* **401** — check your credentials
* **403** — the credential pair is invalid; regenerate from the Console
* **404** — the resource does not exist
* **409 `DUPLICATE_IDEMPOTENCY_KEY`** — the transaction was already processed; query `GET /idempotency/:key` for the original response
* **409 `INTEGRITY_VIOLATION`** — contact support; indicates database tampering
* **422** — business rule violation; adjust the transaction (add funds, fix asset, etc.)

### Reconciliation After Timeouts

If your request times out and you don't know if the transaction was committed:

1. Query `GET /idempotency/<your-key>` — if it returns 200, the transaction was committed
2. If it returns 404, the transaction was **not** committed — safe to retry with the same key
3. After a server restart, use `GET /recovery` to check the last batch before the restart


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://entrytarget.gitbook.io/docs/api-reference/error-codes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
