# Authentication

> API keys, scopes, rotation, and the short-lived tokens for phones and browsers.

## API keys

Your app authenticates every request with an API key in the `Authorization` header:

```bash
curl https://api.aqoon.si/v1/applications \
  -H "Authorization: Bearer $AQOONSI_KEY" \
  -H "Accept: application/vnd.api+json"
```

A key looks like `aq_live_…` or `aq_test_…`. The prefix tells you its [environment](environments.md). A request without a valid key returns `401`.

## Who owns a key

A key belongs to your **account**, not to the member who created it. It keeps working when that member leaves, so your app never depends on one person's access. The dashboard records who created each key and when it was last used.

Each key works in exactly one environment of one application.

## Creating keys

Owners and developers create keys under **Developers → API tokens** in the [dashboard](https://app.aqoon.si/developers/keys). Choose the application, the environment, the scopes and, optionally, an expiry date.

The secret is shown **once**. Store it straight away where only your app can read it, such as a secret manager or an environment variable: Aqoonsi stores only a hash and cannot show it again.

Never put a key in code that ships to people's phones or browsers, in a public repository or in a log. The Aqoonsi SDKs refuse API keys; give them a [capture or document token](#capture-and-document-tokens) instead.

## Scopes

A key can do only what its scopes allow. A request outside them returns `403`, and the error names the missing permission. Give each key the fewest scopes it needs. A key created with no scopes gets `applications:read` only.

| Area           | Scopes                                                                                          |
| -------------- | ----------------------------------------------------------------------------------------------- |
| People         | `people:read`, `people:write`, `consents:read`, `consents:write`, `claims:read`, `claims:write` |
| Verifications  | `verifications:read`, `verifications:write`                                                     |
| Businesses     | `businesses:read`, `businesses:write`                                                           |
| Screening      | `screening:read`, `screening:write`                                                             |
| Risk           | `risk:read`, `risk:write`                                                                       |
| Cases          | `cases:read`, `cases:write`                                                                     |
| Reference data | `applications:read`, `registry:read`                                                            |

Some permissions are never granted to a key, whatever you choose:

- managing members, API keys, webhooks and account settings;
- decisions reserved for people: deciding a case, reviewing a screening match, and overriding a risk level. See [roles](accounts-and-teams.md#roles).

## Rotating and revoking

**Rotate** a key to get a new secret with the same name, environment and scopes. The old secret keeps working for 24 hours, so you can deploy the new one without downtime. Rotation raises the event `apiKey.rotated`.

**Revoke** a key to stop it immediately. Revoke at once if a key may have leaked.

A revoked or expired key cannot be rotated: create a new one.

## Rate limits

Each environment has a request limit, shared by its keys. Each capture token and document token has a limit of its own, well above what a person on a device needs. Above a limit the API returns `429` with `Retry-After` and `RateLimit-*` headers. Wait for the time in `Retry-After` before retrying. See [errors](errors.md#retrying).

## Capture and document tokens

A person's phone or browser never holds an API key. Your app issues a short-lived token for one task and passes it there:

| Token          | Prefix | Issued with                            | Allows                                                             | Lifetime                                            |
| -------------- | ------ | -------------------------------------- | ------------------------------------------------------------------ | --------------------------------------------------- |
| Capture token  | `aqs_` | `POST /v1/verifications/{id}/sessions` | Completing one verification: challenges, captures, document images | At most 30 minutes; ends when the verification ends |
| Document token | `aqd_` | `POST /v1/businesses/{id}/sessions`    | Uploading documents for one business                               | 30 minutes                                          |

Both are returned once, in `meta.secret`. A capture token can be bound to one browser origin. See [capture](capture.md).

## Credentials at a glance

| Credential        | Held by                                           | Lifetime                 |
| ----------------- | ------------------------------------------------- | ------------------------ |
| API key           | Your app                                          | Until revoked or expired |
| Capture token     | A person's phone or browser, for one verification | At most 30 minutes       |
| Document token    | A business's staff, for one business              | 30 minutes               |
| Dashboard session | A member of your team                             | Until they sign out      |
