# Authentication

Generations need an API key, sent as a bearer token. Keep it on your server. The model list, status and price estimate need no key.

```bash cURL
export VIDLAB_API_KEY="vidlab_sk_your_key_here"

curl https://vidlab.ai/v1/balance \
  -H "Authorization: Bearer $VIDLAB_API_KEY"
```

```python Python
import os, requests

r = requests.get(
    "https://vidlab.ai/v1/balance",
    headers={"Authorization": f"Bearer {os.environ['VIDLAB_API_KEY']}"},
)
print(r.json())
```

```js JavaScript
const res = await fetch("https://vidlab.ai/v1/balance", {
  headers: { Authorization: `Bearer ${process.env.VIDLAB_API_KEY}` },
});
console.log(await res.json());
```

| Header | What it is |
| - | - |
| `Authorization` | `Bearer vidlab_sk_...`. The key starts with `vidlab_sk_`. |
| `x-api-key` | The same key, for clients that cannot set `Authorization`. |

## Make a key

Keys are made in the dashboard, from a signed-in browser: open [API keys](/api-keys), accept the Developer API terms and create one. An API key cannot make other keys.

- The full key is shown once, when you create it. VidLab stores only a hash, so a lost key cannot be shown again: make a new one.
- You can keep up to 25 live keys. Delete a key on the same page to revoke it at once.
- Keys have full access: they start and poll generations, read the balance.

## Key controls

A new key may do everything above. Each key's menu on [API keys](/api-keys) narrows what it may do. The server checks every one on every request; a refused call starts nothing and holds nothing.

| Control | What it does | Refused with |
| - | - | - |
| Expiry | The key stops working after a date (7, 30 or 90 days, a year, or a date you pick). Never, by default. A new date makes an expired key work again. | `401 key_expired` |
| IP allowlist | Only requests from up to 10 IPv4 or IPv6 addresses or CIDR ranges (such as `203.0.113.7` or `198.51.100.0/24`) may use the key. Any IP, by default. | `403 ip_not_allowed` |
| Allowed models | All models by default, including new ones as they arrive; or only the models you pick. With a key, `GET /v1/models` lists only the key's models, and the model page, the estimate, the quote and a generation refuse the others. A key limited to specific models cannot start work that picks its own models. | `403 model_not_allowed` |
| Safe-spend limits | The most credits (1 credit = $0.01) the key may spend per hour, per day and in total. 0 means no limit. | `429 key_spend_limit_reached`, `402 key_quota_exceeded` |

- **The address is the one that reached us.** Behind our own edge it is the address our proxy saw, never an `X-Forwarded-For` you send yourself. A key with an allowlist is refused when the address cannot be told.
- **We keep a log of where each key is used from.** Every request to the API is recorded with the address it came from (by the same rule), the key, and the request and generation ids, for 90 days. A key's menu on [API keys](/api-keys) shows its recent addresses under Recent IPs, with how many requests each made and how many the allowlist refused.
- **Spend counts as soon as it is reserved.** A generation reserves its price when it starts; that reservation counts against the key's limits until it settles, and a failed generation gives its room back at once. Two requests at the same moment can never both pass on the same room.
- **Windows are UTC.** The hourly limit resets on the hour, the daily limit at midnight UTC; the total never resets. The refusal says which limit was hit and when it resets (see [Rate limits](/docs/rate-limits#safe-spend-limits-per-key)).

## Keep it safe

- Put the key in an environment variable (`VIDLAB_API_KEY`) or your platform's secret store. Never in source code, a committed file, a URL or a prompt.
- Call the API from your server, never from a browser or a mobile app, where anyone can read the key.
- Use one key per app, so you can revoke one without breaking the rest. If a key leaks, delete it straight away.

## When it fails

| Status | Code | Why |
| - | - | - |
| 401 | `unauthorized` | The key is missing, unknown or revoked. |
| 401 | `key_expired` | The key's expiry date has passed. Set a new date on the key, or make a new key. |
| 402 | `key_quota_exceeded` | The key reached its total spend limit. |
| 403 | `ip_not_allowed` | The key has an IP allowlist and the request came from another address. |
| 403 | `model_not_allowed` | The key's allowed models leave this model out. |
| 403 | `api_key_required` | Generations take a `vidlab_sk_` key only; another kind of token was sent. |
| 403 | `developer_access_forbidden` | The account does not have API access yet. Write to [support@vidlab.ai](mailto:support@vidlab.ai). |
| 429 | `key_spend_limit_reached` | The key reached its hourly or daily spend limit. `key_limit` says which, and `Retry-After` when it resets. |
