# Authentication Every call carries an API key as a bearer token, from server-side code only. How to send it, what each refusal means, and how to keep keys safe. The API knows who is calling from the API key, and bills the call to that key's Account. There are no sessions, OAuth or signed requests: one header does it. ## Send the key as a bearer token Put the key in the `Authorization` header as `Bearer` followed by the key. That is the only way to pass one: never in the URL or the body. Every sample in these docs reads the key from `CHESSDRIVE_API_KEY`. Set it as the [Quickstart](/docs/quickstart) shows, then any call works the same way. ```bash curl curl https://api.chessdrive.io/v1/generate \ -H "Authorization: Bearer $CHESSDRIVE_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "knight", "messages": [{ "role": "user", "content": "In one sentence, what is a fork?" }] }' ``` ```python Python import os import requests r = requests.post( "https://api.chessdrive.io/v1/generate", headers={"Authorization": f"Bearer {os.environ['CHESSDRIVE_API_KEY']}"}, json={ "model": "knight", "messages": [{"role": "user", "content": "In one sentence, what is a fork?"}], }, ) print(r.status_code, r.json()) ``` ```js JavaScript const res = await fetch("https://api.chessdrive.io/v1/generate", { method: "POST", headers: { Authorization: `Bearer ${process.env.CHESSDRIVE_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "knight", messages: [{ role: "user", content: "In one sentence, what is a fork?" }], }), }); console.log(res.status, await res.json()); ``` ## What a key looks like A key starts with `ck_`, then 43 random characters and a 6-character checksum. The prefix and the checksum make a key easy to recognise, so a scanner can find one pasted into logs or code. The key is shown once, when you create it in [API keys](/dashboard/keys). After that the Dashboard shows only a hint of its last characters. If you lose a key, create a new one and revoke the old. ```text Key ck_••••••••••••••••••••••••••••••••••••••••7Qf2Lm ``` ## Server-side only Call the API from your server, a backend function or a script, never from a browser or a mobile app. Anything shipped to a user's device can be read, and every call made with a key is billed to your Account. If your web app needs answers, call the API from your backend and pass the result on. The Model list is one exception: `GET /v1/models` is public and needs no key, so a page can read it. A **Study key** is the other. When your agent builds you a Study, a chess page of your own, it makes a key for that one page, so the page can call Engine and Generate when you ask. A Study key calls only those two, up to $1 a day, and expires after 7 days. Engine and Generate answer it from any web page, a file opened from your computer included. It can't read your usage or Balance, make another key or sign in to the Connector: those answer `403 key_not_allowed`. Your agent makes it with the Connector's `study_key` tool, `chessdrive study-key` or `POST /v1/study_keys`, and [API keys](/dashboard/keys) lists it under Study keys, by its Study's name, where you can revoke it. A copy of the page you download carries no key; it shows an "Add your key" line instead, and a key pasted there stays in that browser. ## When a key is refused A refused call is never billed. A `401` means the key itself was not accepted; a `403` means the key is fine but may not make this call. | Status | Code | Meaning | Next step | |---|---|---|---| | 401 | `missing_api_key` | No `Authorization: Bearer` header. | Send the header. Check the variable is set in the process that makes the call. | | 401 | `invalid_api_key` | Not a key we issued, or mistyped. | Copy the key again, or create a new one. | | 401 | `revoked_api_key` | The key was revoked. | Create a new key. Revoking is permanent. | | 401 | `expired_api_key` | The key passed its expiry date. | Create a new key. Expiry is fixed when a key is made. | | 401 | `disabled_api_key` | The key is disabled. | Enable it again in API keys, or use another key. | | 403 | `key_not_allowed` | The key is restricted to other APIs, or to another Model, or it's a Study key off Engine and Generate. | Use a key allowed for this API and Model, or change the call. | | 403 | `account_suspended` | The Account is suspended. | Contact us at contact@edgecaselabs.dev. | Every refusal carries a `request-id`. Quote it if you write to us. The [Errors](/docs/errors) page lists every code the API can return. ## Keeping keys safe - **One key per app and environment.** Name it after where it runs, like "Production: tournament app". When one leaks, you revoke only that one, and Usage shows spend per key. - **Keep keys out of code.** Read them from an environment variable or your platform's secret store, and keep `.env` files out of version control. - **Set a Key limit.** Customize, when you create a key, can cap what it may spend per day, week or month, or in total. A leaked or looping key then can't drain your Balance. Calls past it are refused with `402 key_limit_reached` until the limit resets. Your Account's Spend limit applies too. - **Restrict it.** A key can be limited to some APIs, and to one Model, such as a Knight-only key. Calls outside that are refused with `403 key_not_allowed`. - **Let it expire.** Keys last a year unless you choose otherwise. "Never" is offered, but a key that never expires stays dangerous if it leaks. - **Rotate without downtime.** Create the new key, deploy it, check its last use in API keys, then revoke the old one. - **If a key leaks, revoke it.** Revoking takes effect at once and can't be undone. To pause a key instead, disable it: calls are refused with `disabled_api_key` until you enable it again.