# Till integration walkthrough

> The whole flow at the till, from setup to the receipt, with the calls in order.

Source: https://developers.loyx.se/guides/till-walkthrough/

This page walks through a complete till integration using only live endpoints. Each step links to its guide.

## Once, at setup

1. **Get credentials.** The brand gives you a client id and secret. Store the secret like a password. See [Authentication](/guides/authentication/).
2. **Map your stores.** For each till location, find the LoyX store by your own store code:
   `GET /v1/stores/external/{external_store_id}`. If a store has no code yet, set it with `PATCH /v1/stores/{store_id}` or ask the brand to. See [Stores](/guides/stores/).
3. **Decide your receipt id.** Pick an id that is unique per brand for all time, for example `{store}-{register}-{number}`. See [Idempotency](/guides/idempotency/).

## Every time a token expires

4. `POST /v1/auth/token` with your client id and secret. Cache the token until shortly before `expires_in` runs out. On any `401`, get a new token and retry the call once.

## At each sale

```
 cashier scans app QR ──► lookup ──► card ──► (sale is paid) ──► record sale
                           │                                        │
                           └─ 404: continue without a member ───────┘
```

5. **Identify.** The member shows the QR code in the LoyX app. Send the scanned text, unchanged:

   ```
   POST /v1/memberships/lookup
   {"identifier": "qr_code", "value": "<scanned text>"}
   ```

   Keep `membership_id`. On `404 qr_code_not_resolvable`, ask the member to refresh the code. On `404 membership_not_found`, continue as a guest. See [Identify a member](/guides/identify-member/).

6. **Show the card** (optional). The lookup answer already has the balance, unit and cycle. Add the member's usable rewards at this store with `GET /v1/memberships/{membership_id}/rewards?external_store_id=<your store code>`. See [Member card and rewards](/guides/member-card/).

7. **Record the sale** once it is paid: `POST /v1/transactions` with your receipt id in `external.external_transaction_id`, your store code in `external.external_store_id`, the member as `{"identifier": "membership_id", "value": "..."}`, and the lines. See [Record a sale](/guides/record-sale/).

   Send it **after** the receipt prints, from a local queue. The sale should never wait on LoyX.

## When things go wrong

| Situation | Do this |
|---|---|
| No answer, timeout, `502`/`503`/`504` | Retry the same request later. The receipt id makes it safe. |
| `429` | Wait `Retry-After` seconds. |
| `401` | Get a new token, retry once. |
| `409 external_transaction_id_conflict` | Two receipts share an id. Fix your id scheme; do not retry. |
| `422` | Fix the fields listed in `error.details`; do not retry unchanged. |
| The till is offline | Queue sales locally, send them when online, in order. Late sales are accepted. |

## Checklist before go-live

- [ ] Secrets are stored encrypted and never logged.
- [ ] Every sale carries a unique, stable `external_transaction_id`.
- [ ] A retried sale returns `200` with `Idempotent-Replayed: true`, not a second sale.
- [ ] QR text is sent unchanged.
- [ ] Amounts are integers in minor units.
- [ ] Sales are sent from a queue that survives a restart.
- [ ] Phone numbers, emails and national ids never appear in URLs or logs.
- [ ] Your client ignores unknown fields and enum values.
