# Authentication

> The key format, where to keep a key, per-key restrictions, rotation and what to do if a key leaks.

Canonical documentation: https://popnow.ai/docs/authentication

Canonical Markdown: https://popnow.ai/docs/markdown/authentication

Authenticate API requests by sending an API key in the `Authorization` header:

```http
Authorization: Bearer sk-popnow-3f9c0a…
```

Keys in the URL query string are not accepted. Your popnow\.ai website login does not authenticate API requests.

The [OpenAI-compatible routes](https://popnow.ai/docs/openai-videos) under `/v1` also accept the key in an `x-api-key` header. The native `/api/v1` routes only read `Authorization`.

## What a key looks like [#key-format]

A key is `sk-popnow-` followed by 48 lowercase hexadecimal characters. After the initial full-key display, the API console shows only the first few characters. If you lose the key, create a new one; the full value cannot be recovered.

## Create a key [#create-a-key]

1. Sign in at [popnow.ai](https://popnow.ai) and open the [API console](https://popnow.ai/api/console/keys).
2. Choose **Create key** and give it a name that identifies its environment or service, such as `production-backend`.
3. Copy the full key immediately. It is shown only once.

You can hold several keys at once — one per environment or per service is a good habit, because you can then revoke one without touching the others. All keys on an account spend from its PopNow balance, shown in USD. A new account starts at $0; add funds in [API billing](https://popnow.ai/api/console/billing).

## Where to keep it [#where-to-keep-it]

* In an environment variable or your platform's secret store (`POPNOW_API_KEY`), not in source code.
* Never in a browser, a mobile app or any other code that runs on someone else's device. Anyone who can read the key can spend your balance. Put your own server in between and let it call PopNow.
* Not in logs. If you log request headers, drop `Authorization` first.

## Restrict what a key can do [#restrictions]

Each key can carry its own limits, set in the API console:

| Restriction    | What it does                                                                                | Error when it blocks a request |
| -------------- | ------------------------------------------------------------------------------------------- | ------------------------------ |
| Allowed models | The key can call only the models you list                                                   | `40301`                        |
| IP allowlist   | Requests must come from these addresses or ranges                                           | `40302`                        |
| Spend caps     | Daily, monthly and lifetime USD limits: `dailySpendCap`, `monthlySpendCap`, `totalSpendCap` | `40202`                        |

By default a key has no restrictions and no caps. Give production keys the narrowest set that still works.

## Tasks belong to the key that created them [#task-scope]

`recordInfo`, `retry`, `purge`, the task list and usage are scoped to tasks created **with the same key**. Querying a task created with another key on your account returns `404`, just like querying a task that does not exist. Tasks started on the website are not accessible through API keys. Use the API console for an account-wide view.

## Rotation [#rotation]

To replace a key without downtime:

1. Create the new key.
2. Deploy it everywhere the old one was used.
3. Check the console until the old key's last-used time stops updating.
4. Revoke the old key.

## If a key leaks [#after-a-leak]

1. **Revoke it now** in the API console. Revocation takes effect on the next request.
2. **Create a replacement** and deploy it.
3. **Check what it was used for**: the console's logs and usage show every task the key created.
4. **Tighten the replacement** with an IP allowlist and a spend cap.

Contact [support](https://popnow.ai/contact) if you see spending you did not make.

## What a failure looks like [#errors]

All three authentication problems return HTTP `401` with `code: 401`; `msg` tells them apart:

| `msg` says                         | What happened                                      | What to do                                                                                      |
| ---------------------------------- | -------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| Missing credentials                | No `Authorization` header arrived                  | Check that the header is set and that the environment variable is loaded in the running process |
| API key format is not recognized   | Something arrived, but it is not a PopNow key      | Make sure it starts with `sk-popnow-`; you may have pasted a different value                    |
| Credentials are invalid or expired | The key does not exist, was revoked or has expired | Create a new key instead of retrying with the invalid one                                       |

A valid key can still be refused with `403` — for example when the account's email address is not verified yet (`40310`). See [Errors](https://popnow.ai/docs/errors#permissions).
