# Errors, retries and idempotency

## The error envelope

A failed request returns an HTTP status and a JSON body in the same envelope shape:

```json
{
  "success": false,
  "message": "Enter a valid recipient",
  "data": {
    "request_id": "req_01...",
    "error": { "code": "INVALID_DESTINATION" }
  }
}
```

- `message` is written for a person and may change.
- `data.error.code` is stable; branch on it.
- `data.request_id` identifies the request in logs and support.

Agent routes use the same envelope with agent-specific codes such as `agent_budget`, `agent_paused` or `agent_scope_denied`.

## Status families

| Status | Meaning | Typical cause |
| --- | --- | --- |
| `400` | Malformed request | Bad JSON, invalid field, missing required value |
| `401` | Not authenticated | Missing, expired or revoked credential |
| `402` | No funds or entitlement | Wallet, subscription or inbound entitlement missing |
| `403` | Not allowed | Scope, role, workspace or policy refusal |
| `404` | Not found | Unknown or inaccessible resource (both look identical) |
| `409` | Conflict | Idempotency conflict, stale revision, suppressed recipient |
| `422` | Unprocessable | Channel unqualified, invalid content, policy refusal |
| `423` | Locked | Agent paused or send budget exhausted |
| `429` | Rate limited | Key request limit or daily quota reached |
| `500` | Server error | Unexpected failure; safe to retry a read |
| `503` | Unavailable | Provider, policy store or transport unavailable |

## Idempotency

Sends accept `Idempotency-Key`. The key is bound to the exact normalized request:

- Reuse the same key with the same payload and you receive the original result, marked `replay: true` or `idempotent-replay: true`.
- Reuse it with a different payload and you get `409`.
- Omit it and retrying a timeout can send twice.

Generate one key per user action, not per HTTP attempt:

```bash
KEY="order-4821-welcome"
curl -X POST 'https://api.bonga.africa/api/v2/messages' \
  -H "Authorization: Bearer $BONGA_API_KEY" \
  -H "Idempotency-Key: $KEY" \
  -H 'Content-Type: application/json' \
  --data '@message.json'
```

Agent sends accept the same header. The MCP tools forward it when the client supplies it.

## Retry policy

| Situation | Retry? |
| --- | --- |
| `429` | Yes, after `Retry-After` |
| `500`, `502`, `503` on a read | Yes, with backoff |
| `503` on a send with an idempotency key | Yes, same key and payload |
| `503` on a send without a key | Only after checking status |
| `409` with an idempotency conflict | No; the key was reused with another payload |
| `400`, `401`, `403`, `422` | No; fix the request or credential |
| `submission_unknown` | Do not resend automatically; reconcile status first |

Use exponential backoff with jitter for retries. A provider that may have accepted a message is never retried blindly: `submission_unknown` exists so you can look before sending again.

## Async work and statuses

A `202 Accepted` means Bonga recorded the request; it does not prove provider delivery. Track the returned message or job:

```bash
curl 'https://api.bonga.africa/api/v2/messages/msg_0123456789abcdef' \
  -H "Authorization: Bearer $BONGA_API_KEY"
```

| Status | Meaning |
| --- | --- |
| `queued` | Recorded, not attempted |
| `scheduled` | Waiting for its send time |
| `sent` | Confirmed provider submission |
| `delivered`, `read` | Provider receipt observed |
| `failed` | Definite failure; inspect `error` |
| `submission_unknown` | The provider may have accepted it; reconcile, never auto-resend |

## Retry-safe agent work

Agent keys can read their own approval and sending status. When a send returns `pending_approval`, do not rephrase and retry to get around the rule. Wait for a person, then read the approval with `GET /api/v1/agent/approvals/{id}` or the MCP `get_approval` tool.

## Next

- [Messaging API v2](/docs/messaging-api-v2)
- [Webhooks](/docs/webhooks)
- [API reference](/docs/api-reference)
