> ## Documentation Index
> Fetch the complete documentation index at: /llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotency

> Safely retry requests without creating duplicate records.

The Playto API supports idempotency for safely retrying requests without accidentally performing the same operation twice. This is useful when an API call fails due to a network connection error, and you want to retry the request without worrying about creating duplicate invoices, products, or customers.

To perform an idempotent request, attach an `Idempotency-Key` header to your request.

```bash
curl -X POST https://api.playto.so/v1/invoices/ \
  -H "X-API-KEY: sk_live_your_api_key_here" \
  -H "Idempotency-Key: your_unique_key_here" \
  -d '{ ... }'
```

### How it works

When you send a `POST` request with an `Idempotency-Key` header, we will save the resulting status code and body of the first request made for any given key. Subsequent requests with the same key will return the exact same result, including HTTP status code and response body.

* **Duration:** Idempotency keys are cached for **24 hours**.
* **Conflicts:** If you reuse an `Idempotency-Key` with a *different* request body, the API will return a `409 Conflict` error (`idempotency_conflict`).
* **Concurrent requests:** If a request is still processing, concurrent requests with the same key are rejected with the same `409 Conflict` error (`idempotency_conflict`) to prevent race conditions.
* **Errors:** If the original request fails (any `4xx` or `5xx` response), the key is not stored, so you can retry with the same key. Only successful (`2xx`) responses are replayed.

### How to generate a key

The API accepts any string up to **255 characters**, but we strongly recommend using a **V4 UUID** (Universally Unique Identifier).

You should generate the key on your backend immediately before making the API call. If the request fails due to a network error, you must reuse that exact same key on the retry.

```javascript Node.js
// Using the built-in Node.js crypto module
const idempotencyKey = crypto.randomUUID();

fetch('https://api.playto.so/v1/customers/', {
  method: 'POST',
  headers: {
    'X-API-KEY': 'sk_live_...',
    'Idempotency-Key': idempotencyKey,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: "Jane Smith", email: "jane@acme.com" })
});
```

```python Python
# Using the built-in uuid library
import uuid
import requests

idempotency_key = str(uuid.uuid4())

requests.post(
    'https://api.playto.so/v1/customers/',
    headers={
        'X-API-KEY': 'sk_live_...',
        'Idempotency-Key': idempotency_key
    },
    json={'name': 'Jane Smith', 'email': 'jane@acme.com'}
)
```

### Supported Endpoints

Idempotency is supported on all `POST` (create) endpoints:

* `POST /v1/products/`
* `POST /v1/invoices/`
* `POST /v1/customers/`
