# Authentication

> Exchange your client id and secret for a one-hour access token.

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

The API uses the OAuth 2 **client credentials** flow. You hold a client id and a client secret for one brand. You trade them for an access token, and send that token on every call.

## 1. Get your credentials

The brand creates an API key for you in the LoyX Portal and shares the **client id** and **client secret** with you. The secret is shown once, when it is created; afterwards the Portal shows only its last four characters. Store it like a password: in a secret store, never in source code, never in logs.

One key belongs to one brand. If you serve several brands, you hold one key per brand.

## 2. Get an access token

```bash
curl -X POST https://api.loyx.se/partners/v1/auth/token \
  -u "$LOYX_CLIENT_ID:$LOYX_CLIENT_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials"
```

The client id and secret go in an HTTP Basic `Authorization` header. The body is a form with `grant_type=client_credentials`.

```json
{
  "access_token": "loyx_EXAMPLE_TOKEN",
  "token_type": "bearer",
  "expires_in": 3600,
  "scope": "members:read sales:write rewards:read stores:read"
}
```

`scope` lists, space separated, what this token may do. You do not ask for scopes: the token carries the scopes the brand gave the key.

## 3. Call the API

```
Authorization: Bearer loyx_EXAMPLE_TOKEN
```

The token decides which brand you act on. You never send a brand or company id in the body.

## Token lifetime

A token lives **one hour** (`expires_in` is in seconds; read it rather than hard-coding 3600). It is opaque: do not parse it. There are no refresh tokens: ask for a new token when the old one is close to expiry.

- Cache the token and reuse it across calls and registers. Do not fetch a token per request; the token endpoint has its own, low rate limit.
- On a `401` from any call, get a new token and retry that call **once**. Tokens can end early, for example when the brand changes the key's scopes.

## Scopes

Each key carries the scopes the brand granted. A call outside them answers `403 insufficient_scope`, with `error.param` set to `scope`.

| Scope | Allows |
|---|---|
| `members:read` | `POST /v1/memberships/lookup` and `GET /v1/memberships/{id}/loyalty`. |
| `rewards:read` | `GET /v1/memberships/{id}/rewards`. |
| `sales:write` | `POST /v1/transactions`, and reading back transactions (`GET /v1/transactions...`). |
| `products:read` | `GET /v1/products...` and `GET /v1/stores/{id}/products`. |
| `products:write` | Creating, changing and removing products, including bulk upserts. |
| `stores:read` | `GET /v1/stores...`. |
| `stores:write` | Creating, changing and removing stores. |

`rewards:redeem` and `webhooks:manage` are reserved for [features in preview](/preview/) and cannot be granted yet. Ask the brand for only the scopes your integration uses. A till that looks members up, shows rewards and sends sales needs `members:read`, `rewards:read`, `sales:write` and `stores:read`.

## Rotating the secret

Rotate secrets regularly, and at once if one may have leaked. The brand rotates the key in the LoyX Portal and picks an overlap of 0 to 168 hours (default 24):

- During the overlap, **both** the old and the new secret work at the token endpoint. Deploy the new secret in that window.
- After the overlap, only the new secret works. Tokens already issued keep working until they expire.
- An overlap of 0 ends the old secret **and every live token of the key** at once. Use it when a secret has leaked.

## Revocation

When the brand revokes a key, its live tokens fail on the next call (`401 invalid_token`) and the token endpoint refuses the key (`401 inactive_client`). There is nothing to retry: ask the brand for a new key.

## Errors

| HTTP | `code` | Meaning |
|---|---|---|
| 400 | `unsupported_grant_type` | `grant_type` is not `client_credentials`. |
| 401 | `invalid_basic_auth` | The Basic header is missing or malformed. |
| 401 | `invalid_client` | Unknown client id or wrong secret. |
| 401 | `inactive_client` | The key is switched off or revoked. |
| 401 | `missing_token` | A call came without an `Authorization` header. |
| 401 | `invalid_authorization_header` | The header is not `Bearer <token>`. |
| 401 | `invalid_token` | The token is unknown, expired or ended. Get a new one. |
| 403 | `insufficient_scope` | The key lacks the scope this call needs. |
| 429 | `rate_limit_exceeded` | Too many requests. Wait `Retry-After` seconds. See [Rate limits](/guides/rate-limits/). |
| 503 | `token_endpoint_unavailable` | The token endpoint cannot check limits right now. Retry with backoff. |

All of them use the standard [error shape](/guides/errors/).
