# Idempotency

> Retry any call safely. Your own ids are the duplicate keys.

Source: https://developers.loyx.se/guides/idempotency/

Tills lose connections, time out and retry. The API is built so that a retry never creates a second sale, product or store.

## Sales: your receipt id is the key

`external.external_transaction_id` on `POST /v1/transactions` is the duplicate key for sales. It never expires.

| You send | You get |
|---|---|
| A new id | `201 Created`, a new transaction. |
| An id LoyX has seen, with the same content | `200 OK`, the **original** transaction, and the header `Idempotent-Replayed: true`. Nothing new is booked. |
| An id LoyX has seen, with different content | `409 external_transaction_id_conflict`. Two receipts share one id. |

Two identical requests sent at the same moment are safe too: one gets `201`, the other `200` with the same transaction.

"Same content" means the same transaction type, currency, receipt references (including `external_transaction_id`), lines and discounts. The `member` and `metadata` fields are not compared, so adding a member on a retry does not cause a conflict.

How to pick the id:

- It must be unique per brand for your system, for all time.
- Use the id your till already gives the receipt. If that is only unique per store or register, combine them: `store12-reg3-000123`.
- Never reuse an id for a new sale, even after a void.

## Products: upsert by your product id

`POST /v1/products` and `POST /v1/products/bulk` match on `external_product_id`: a retry updates the same product instead of creating a second one. See [Products](/guides/products/).

## Stores: your store code is the key

A store's `external_store_id` is unique per brand. Creating a second store with the same code answers `409`. On a `409` after a retry, read the store back by its code and carry on.

## The Idempotency-Key header

On `POST /v1/transactions` you may also send an `Idempotency-Key` header, for example a UUID per sale. It is optional.

| You send | You get |
|---|---|
| A key LoyX has seen, with the same body | `200 OK`, the original transaction, `Idempotent-Replayed: true`. |
| A key LoyX has seen, with a different sale | `409 idempotency_key_reused` (`param`: `Idempotency-Key`). Use a new key for every sale. |

The receipt id rule above applies whether or not you send a key: the same `external_transaction_id` with the same content is always a replay, even under a new key or none. So the receipt id is your main protection, and the key is an extra one. Never put personal data in a key.

`POST /v1/products` and `POST /v1/products/bulk` honour the key too, for 24 hours, scoped to your API key, and replay the original status (`201` stays `201`). A resend while the first is still running is `409 idempotency_request_in_progress`. On other write endpoints the header is accepted and ignored; they are protected by the ids above.

## Retry rules for your client

- Retry on network errors, `429`, `502`, `503` and `504`, with backoff. Respect `Retry-After`.
- Do not retry other `4xx` answers unchanged. Fix the request.
- Treat `200` with `Idempotent-Replayed: true` as success.
