Docs / Authentication
Authentication
One bearer key on every route except /v1/health. Four operator routes also want the admin token. Auth failures are decided before the quota check, so a bad key never spends your month.
Source: docs/API.md §3.
Three arms. The app resolves them in this order for a presented bearer.
3.1 Database keys (the customer path)
Issued with backend/scripts/keys.py create --customer vg --plan starter --quota 45000 (or backend/scripts/onboard_customer.py, see docs/OPERATIONS.md §5.4). The key looks like cbn_<random>; only its SHA-256 hash is stored, so a lost key can only be replaced, never recovered. Present it as:
Authorization: Bearer cbn_...A database key carries a customer, plan and monthly billable-item quota, and every served response is metered against it.
3.2 Legacy env keys
NEWS_API_KEYS (comma separated) still works for backwards compatibility. These keys have no customer identity and no quota; /v1/usage and /v1/usage/history answer 501 for them.
3.3 Dev mode
If NEWS_API_KEYS is unset and the key table was read successfully and holds no rows, the app runs in DEV MODE: every route is open, and one loud warning is logged at startup. This is for local work only. Never leave a deployed box in this state.
Fail closed. An unreadable key table is not "no keys exist". If the count probe throws (SQLite locked, corrupt file, missing schema), authentication stays enforced: a request without a valid bearer answers 401 and no anonymous identity is ever issued. The same rule covers a key table whose last key was revoked. billing.key_count() returns 1 ("configured") on a read failure, and the request path is also structurally guarded (auth.auth_configured), so a probe that raises cannot open the API by accident. Dev mode is therefore reachable only from a successful read that returns zero.
3.4 Admin token
NEWS_ADMIN_TOKEN plus the header X-Admin-Token guards POST /v1/refresh, GET /v1/admin/usage, GET /v1/runs, GET /v1/sources/health and the three /v1/admin/webhooks/* routes (§12, "Webhook delivery admin"). Unset means those routes answer 503 and never run. Verified text: refresh is disabled: NEWS_ADMIN_TOKEN is not configured.
The admin token is never accepted cross-origin: the CORS policy does not allow the X-Admin-Token request header, so /v1/refresh cannot be driven from a browser on another site.
3.5 Failure shapes (verified)
| Situation | Status | Body | Headers |
|---|---|---|---|
No Authorization, auth configured | 401 | {"detail":"invalid or missing bearer token"} | WWW-Authenticate: Bearer |
| Wrong bearer | 401 | same | WWW-Authenticate: Bearer |
| Revoked key | 401 | {"detail":"this api key has been revoked"} | WWW-Authenticate: Bearer |
| Key table unreadable, no legacy keys configured | 401 | {"detail":"invalid or missing bearer token"} (fail closed, never dev mode) | WWW-Authenticate: Bearer |
/v1/refresh, /v1/admin/usage, /v1/runs, /v1/sources/health with a wrong or missing admin token | 401 | {"detail":"invalid admin token"} | WWW-Authenticate: X-Admin-Token |
/v1/usage, /v1/usage/history with a legacy/dev bearer | 501 | {"detail":"usage reporting requires a database-issued key; this bearer is a legacy NEWS_API_KEYS entry with no customer identity"} | — |
A client bearer is not accepted on the admin routes: GET /v1/runs with a valid client key answers 401 {"detail":"invalid admin token"} (measured). Auth failures never consume quota, because the identity is resolved before the quota check.
Which token each route wants
| Method | Path | Auth |
|---|---|---|
| GET | /fxs/v4/culture/all | client bearer |
| GET | /fxs/v4/{culture}/post/{rest} | client bearer |
| GET | /fxs/v4/{culture}/tag | client bearer |
| GET | /fxs/v4/{culture}/tag/filter/{tag_ids} | client bearer |
| GET | /v1/account | cookie session |
| DELETE | /v1/account/news-bot | cookie session + X-Requested-With: cabinhub-console |
| GET | /v1/account/news-bot | cookie session |
| PUT | /v1/account/news-bot | cookie session + X-Requested-With: cabinhub-console |
| POST | /v1/account/news-bot/refresh | cookie session + X-Requested-With: cabinhub-console |
| POST | /v1/account/otp | X-Requested-With: cabinhub-console (no session) |
| DELETE | /v1/account/session | cookie session + X-Requested-With: cabinhub-console |
| POST | /v1/account/session | X-Requested-With: cabinhub-console (no session) |
| GET | /v1/account/usage | cookie session |
| GET | /v1/account/usage/statement | cookie session |
| GET | /v1/account/usage/statement.csv | cookie session |
| GET | /v1/admin/accounts | X-Admin-Token + X-Admin-Actor |
| POST | /v1/admin/accounts | X-Admin-Token + X-Admin-Actor |
| GET | /v1/admin/keys | X-Admin-Token + X-Admin-Actor |
| POST | /v1/admin/keys | X-Admin-Token + X-Admin-Actor |
| GET | /v1/admin/keys/audit | X-Admin-Token + X-Admin-Actor |
| POST | /v1/admin/keys/{key_id}/revoke | X-Admin-Token + X-Admin-Actor |
| GET | /v1/admin/usage | X-Admin-Token |
| GET | /v1/admin/webhooks/deliveries | X-Admin-Token |
| POST | /v1/admin/webhooks/deliveries/{delivery_id}/replay | X-Admin-Token |
| GET | /v1/admin/webhooks/summary | X-Admin-Token |
| GET | /v1/digest/{locale} | client bearer + quota |
| GET | /v1/digest/{locale}/email | client bearer + quota |
| GET | /v1/digest/{locale}/email/{day} | client bearer + quota |
| GET | /v1/digest/{locale}/{day} | client bearer + quota |
| GET | /v1/feed/{locale} | client bearer + quota |
| GET | /v1/health | none |
| GET | /v1/images/{key} | client bearer + quota |
| HEAD | /v1/images/{key} | client bearer + quota |
| GET | /v1/items | client bearer + quota |
| GET | /v1/items/{item_id} | client bearer + quota |
| GET | /v1/items/{item_id}/related | client bearer + quota |
| GET | /v1/meta | client bearer + quota |
| POST | /v1/refresh | client bearer + X-Admin-Token |
| GET | /v1/runs | X-Admin-Token |
| GET | /v1/runs/{run_id} | client bearer + quota |
| GET | /v1/search | client bearer + quota |
| GET | /v1/site/digest/{locale} | none |
| GET | /v1/site/digest/{locale}/{day} | none |
| GET | /v1/site/feed/{locale} | none |
| GET | /v1/site/images/{key} | none |
| HEAD | /v1/site/images/{key} | client bearer + quota |
| GET | /v1/site/items | none |
| GET | /v1/site/items/{item_id} | none |
| GET | /v1/site/items/{item_id}/related | none |
| GET | /v1/site/markets | none |
| GET | /v1/site/markets/{symbol}/history | none |
| GET | /v1/site/meta | none |
| GET | /v1/site/search | none |
| GET | /v1/site/status | none |
| GET | /v1/site/storyline/{item_id} | none |
| GET | /v1/site/topics | none |
| GET | /v1/site/topics/{slug} | none |
| GET | /v1/sources/health | X-Admin-Token |
| GET | /v1/stats | client bearer + quota |
| GET | /v1/stream/{locale} | client bearer + quota |
| GET | /v1/usage | client bearer (no quota check) |
| GET | /v1/usage/history | client bearer (no quota check) |
| GET | /v1/version | none |
Source: docs/API.md §2 route table, cross-checked against clients/openapi.json.