# Idempotency and retries

A timeout is not a failure. It is an unknown result. The safe way to handle an unknown result is to retry with the same identity, not to create a second message.

## Send with an idempotency key

Add `Idempotency-Key` to a send request. The key is scoped to the workspace and the endpoint.

```bash
curl -sS -X POST "$BASE/api/practices/$PRACTICE_ID/send" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: appointment-4821-reminder-1" \
  -d '{
    "route": "outgoing",
    "message_kind": "termin",
    "from": "praxis@example.at",
    "to": ["patient@example.at"],
    "subject": "Ihr Termin am 24.11.",
    "text_body": "Guten Tag, Ihr Termin ist am 24.11. um 09:30."
  }'
```

The first request creates the message. A retry with the same key returns the same message identity. It does not create a second send.

### Key rules

- Use a key that describes the business event, not a random value per attempt.
- Keep the key stable across retries of the same event.
- Do not reuse a key for different content. That is a conflict, not a new message.
- A key is not a substitute for consent, suppression, or content checks.

## What to retry

| Status | Meaning | Retry? |
| --- | --- | --- |
| `202` | Accepted for delivery | No |
| `400` | Validation error | No, fix the request |
| `401` | Missing or invalid token | No, fix the credential |
| `403` | Workspace or permission denied | No |
| `404` | Unknown workspace, campaign, or contact | No |
| `409` | Conflict, including a reused key with different content | No |
| `422` | The request is understood but cannot be processed | Usually no |
| `429` | Rate limited | Yes, after `Retry-After` |
| `500`, `502`, `503`, `504` | Temporary server or provider failure | Yes, with backoff |

Use exponential backoff with jitter. Do not retry in a tight loop.

## Webhooks

Every webhook delivery is signed. Verify the signature before parsing the body, and reject a payload whose timestamp is outside your tolerance.

- Treat the event ID as the idempotency key on your side.
- Store the event ID before applying the side effect.
- If the same event arrives twice, return success and do nothing.
- A failed delivery is retried with a visible state. Do not assume one attempt.

## Errors

Errors are JSON with a code, a message, and a hint.

```json
{
  "code": "recipient_suppressed",
  "error": "The recipient is on the suppression list.",
  "hint": "Remove the suppression only if the contact has asked to receive mail again."
}
```

The code is stable. The message is for humans. The hint is the next step.

## Sandbox

Use the sandbox and the simulator address before production access. The same validation, consent, suppression, and content gate apply. A sandbox that skips the gate teaches the wrong lesson.

## Related

- [Send API](/api/send)
- [Errors](/api/errors)
- [Webhooks](/api/webhooks)
- [Compatible send APIs](/api/compat)
- [Idempotent API](https://werkmail.eu/features/idempotent-api)
