# Idempotency

- Page: https://bytesms.com/docs/idempotency (markdown: https://bytesms.com/docs/idempotency.md)
- API base URL: https://api.bytesms.com/api (public resources under /api/v1)
- Auth: `Authorization: Bearer tp_live_…` — an API key from Dashboard → API Keys; keep it server-side (e.g. `BYTESMS_API_KEY`)
- All docs in one file: https://bytesms.com/llms-full.txt · OpenAPI 3.1: https://bytesms.com/openapi.json

Retry Bytesms email sends safely with the Idempotency-Key header (24-hour window).

## How it works

Network errors and timeouts leave you not knowing whether an email was accepted. Send an `Idempotency-Key` header with `POST /api/v1/emails` or `POST /api/v1/emails/batch` and retry with the same key: Bytesms accepts the email once and returns the original response to every retry.

**cURL**

```bash
# Send with an idempotency key
curl -X POST 'https://api.bytesms.com/api/v1/emails' \
  -H 'Authorization: Bearer tp_live_xxxxxxxxx' \
  -H 'Idempotency-Key: welcome-user-123' \
  -H 'Content-Type: application/json' \
  -d '{"from":"Acme <hello@example.com>","to":["customer@example.com"],"subject":"Welcome","html":"<p>Welcome aboard.</p>"}'
```

**Node.js**

```js
// Send with an idempotency key
const res = await fetch("https://api.bytesms.com/api/v1/emails", {
  method: "POST",
  headers: {
    Authorization: "Bearer tp_live_xxxxxxxxx",
    "Idempotency-Key": "welcome-user-123",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "from": "Acme <hello@example.com>",
    "to": [
      "customer@example.com"
    ],
    "subject": "Welcome",
    "html": "<p>Welcome aboard.</p>"
  }),
});
console.log(await res.json());
```

**Python**

```python
import requests

# Send with an idempotency key
res = requests.post(
    "https://api.bytesms.com/api/v1/emails",
    headers={"Authorization": "Bearer tp_live_xxxxxxxxx", "Idempotency-Key": "welcome-user-123"},
    json={
        "from": "Acme <hello@example.com>",
        "to": [
            "customer@example.com"
        ],
        "subject": "Welcome",
        "html": "<p>Welcome aboard.</p>"
    },
)
print(res.json())
```

## Rules

- A key is 1–256 characters; anything else is `422 invalid_idempotency_key`.
- Keys are scoped to the workspace and remembered for 24 hours.
- Same key + same body → the stored response (for example the same `{ id }`), no second email.
- Same key + a different body → `409 idempotency_key_conflict`. Bodies are compared after sorting JSON keys, so key order doesn't matter.
- Two concurrent requests with the same key still create only one email.
- Single sends and batch sends keep separate key spaces.

**409 Conflict**

```json
{
  "statusCode": 409,
  "message": "This Idempotency-Key was already used with a different request body. Use a new key for a different email.",
  "name": "idempotency_key_conflict"
}
```

> **Note: Choosing keys**
>
> Derive the key from what makes the email unique in your system — e.g. `order-4711-receipt` or a UUID stored with the job that sends it — rather than generating a new random key on each retry.
