# Authentication

Every request carries a secret API key as a bearer token in the `Authorization` header:

```http
Authorization: Bearer sk_live_your_key
```

```bash
curl https://api.qelka.com/v1/account \
  -H "Authorization: Bearer $API_KEY"
```

Send the key in this header on every request, including `GET` requests. A request without a valid key gets a `401` with the error type `invalid_api_key`.

## Live and test keys

| What | Live key | Test key |
|---|---|---|
| Prefix | `sk_live_` | `sk_test_` |
| Results | Real checks | Simulated, deterministic responses |
| Credits | Debited per call | Never debited |
| Validation and rate limits | Same as test | Same as live |
| Bulk jobs | Yes | No, `POST /bulk` returns `403 forbidden` |

Both kinds work on the same base URL. Switching from test to live means changing the key, nothing else. [Test mode](/docs/test-mode) lists what test keys return.

## Create a key

Keys are created in the dashboard. Give each key a name that says where it runs, such as `signup-service-production` or `ci`. The dashboard shows each key's prefix and when it was last used, so a name and a prefix are enough to tell keys apart later.

A live key is shown once, when you create it. We store only a SHA-256 hash of it, so we cannot show it again or recover it. If you lose it, create a new key and revoke the old one.

Test keys are the exception: we also keep them encrypted, so the dashboard can show them again and put them in its ready-to-run examples. They cannot spend credits.

## Keep keys secret

Anyone who holds a live key can spend your credits. Treat it like a password.

- **Call the API from your server only.** Never put a key in a browser bundle, a mobile app or a desktop app: anyone can extract it. Your frontend calls your backend, your backend calls the API, and only the fields your interface needs go back to the browser.
- **Store keys in environment variables or a secret manager**, not in source code. Keep them out of version control, issue trackers and chat.
- **Use one key per service and environment.** You can then revoke one without breaking the others, and `last used` tells you which service still relies on it.
- **Log the prefix, never the key.** `GET /account` returns the prefix of the calling key, which is safe to log.

## Restricted keys

A key can be limited to some endpoints, chosen among `/phone`, `/email`, `/ip`, `/batch`, `/bulk` and `/account`. A signup service that only verifies emails, for example, does not need access to `/phone` or `/bulk`. A restricted key that calls an endpoint outside its list gets `403` with the error type `forbidden`, and nothing is billed.

Restricting keys limits the damage if one leaks: a key that can only call `/email` cannot start a bulk job on your balance.

## Rotate a key

1. Create a new key with the same restrictions.
2. Deploy it to the service that uses the old key.
3. Check in the dashboard that the old key is no longer being used.
4. Revoke the old key.

A revoked key stops working within 60 seconds. After that, requests made with it get `401 invalid_api_key`.

If a key has leaked, do it in the other order: revoke it first, then create and deploy the replacement. A few minutes of failed calls cost less than a balance spent by someone else.

## Find out which key you are using

`GET /account` costs no credits and describes the key that made the call:

```json
{
  "object": "account",
  "org": { "id": "org_01M84BDPNRQBDQEPZ7787ST5TH", "name": "Acme" },
  "plan": { "id": "starter", "interval": "month", "monthly_credits": 10000, "renews_at": "2026-12-23T00:00:00.000Z" },
  "credits": { "balance": 12450, "expiring": [{ "credits": 2450, "expires_at": "2027-01-23T00:00:00.000Z" }, { "credits": 10000, "expires_at": "2027-02-23T00:00:00.000Z" }] },
  "limits": {
    "per_key": { "requests": 200, "window_seconds": 10 },
    "per_org": { "requests": 1000, "window_seconds": 60 }
  },
  "key": { "id": "key_01M84BDPNREK4SFJN6ECRF2HYK", "name": "signup-service-production", "mode": "live", "prefix": "sk_live_8Hq2" }
}
```

`key.mode` is `live` or `test`. A startup check that reads it is a cheap way to make sure production never runs with a test key. The [account reference](/docs/api-reference/account) describes every field.

## Authentication errors

| Status | Type | Common causes |
|---|---|---|
| 401 | [invalid_api_key](/docs/errors#invalid_api_key) | Missing `Authorization` header, a scheme other than `Bearer`, a truncated or mistyped key, a revoked key |
| 403 | [forbidden](/docs/errors#forbidden) | A restricted key calling an endpoint outside its list, or a test key creating a bulk job |

Neither error is billed. Retrying will not help: fix the key or the header first.
