# Authentication and API keys

Every request to `/v1` carries an API key as the bearer token:

```http
Authorization: Bearer mor_test_1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d
```

A key is `mor_live_` or `mor_test_` followed by 32 hexadecimal characters. Regoria exchanges it for a short-lived internal token on each request; you never see or handle that token.

## Test and live

A key has a **mode**. A `mor_test_` key works on the sandbox base URL (`https://api.sandbox.staging.regoria.com`) only; a `mor_live_` key on the live one (`https://api.staging.regoria.com`) only. The other combination answers `401`. See [Test mode and the sandbox](http://docs.staging.regoria.com/guides/test-mode).

## Scopes

A key carries the **scopes** it was created with; an operation needs the scope named in its reference entry (`x-kit-scope`). A call outside the key's scopes answers `403 insufficient_scope`; the error's `scope` names the missing one.

| Scope | Lets the key |
|---|---|
| `v1.merchants.read` | read your merchant profile |
| `v1.api_keys.manage` | list, create and revoke API keys |
| `v1.users.read` | list the users of your account |
| `v1.products.read` / `v1.products.write` | read / manage products |
| `v1.prices.read` / `v1.prices.write` | read / add prices |
| `v1.checkout_sessions.write` | open, read and expire checkout sessions |
| `v1.orders.read` / `v1.orders.write` | read orders and receipts / create server-side orders, fulfil |
| `v1.customers.read` / `v1.customers.write` | read / manage customers |
| `v1.portal_sessions.write` | mint portal links |
| `v1.charges.read` | read charges and refunds |
| `v1.refunds.write` | request refunds |
| `v1.payment_methods.read` / `v1.payment_methods.write` | read / remove saved cards |
| `v1.subscriptions.read` / `v1.subscriptions.write` | read / act on subscriptions |
| `v1.balance.read` | read your balance |
| `v1.reports.read` | balance transactions, summary, payout reconciliation |
| `v1.disputes.read` | read disputes |
| `v1.webhook_endpoints.manage` | manage webhook endpoints, deliveries and replays |
| `v1.terms.read` | read the current terms |

Give a key only what its server does. A key cannot carry a scope the person creating it does not hold themselves.

## Store-bound keys

A merchant account can run several **stores** (brands, sites). A key names one store; what it creates (products, checkout sessions, webhook endpoints) belongs to that store, and what it lists is scoped to it. The dashboard's key form picks the store; `POST /v1/api-keys` takes `store_id` (default: the merchant's default store).

## Creating and listing keys

Keys are created in the dashboard (**Developers → API keys**) or with the API:

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/api-keys \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"name": "Fulfilment server", "mode": "test", "scopes": ["v1.orders.read", "v1.orders.write"]}'
```

The answer is the key record plus `key`, the secret itself, **shown once**. Store it in your secret manager, never in source control or logs. `GET /v1/api-keys` lists the records (prefix, scopes, last use); the secret is never returned again.

## Rotation

1. Create a new key with the same scopes.
2. Deploy it to your servers.
3. Revoke the old one: `DELETE /v1/api-keys/{key}`.

Revocation takes effect within a minute everywhere: Regoria caches the exchanged token for at most that long. The record stays listed with `revoked_at` (`include_revoked=true`).

## Handling keys safely

- Keep keys server-side. Never ship one in a browser, a mobile app or a public repository.
- Log the key's `prefix` (`mor_test_1a2b`) when you must log something, never the key.
- One key per server or job, so a leak is one revocation.
- The webhook signing secret is a different secret with a different job: see [Webhooks](http://docs.staging.regoria.com/guides/webhooks).

## Headers the API reads

| Header | Purpose |
|---|---|
| `Authorization: Bearer <key>` | the API key |
| `Idempotency-Key` | required on the operations that create money state ([Idempotency](http://docs.staging.regoria.com/guides/idempotency)) |
| `X-Timezone` | the IANA zone date filters are read in ([Pagination](http://docs.staging.regoria.com/guides/pagination)) |
| `X-SDK` | sent by the SDKs (`typescript/1.2.0`) so support can see which SDK you run |
| `X-Correlation-Id` | optional; echoed back, minted when absent ([Errors](http://docs.staging.regoria.com/guides/errors)) |
