# Authentication and API keys

> How Inola API keys work — the Authorization header, permissions, expiry dates, revocation, and where a key is kept so that it does not leak.

This page describes how requests to the Inola API are authenticated: the API key, its permissions, expiry and
revocation. It needs access to the console of your [organization](https://inola.ai/docs/reference/glossary.md#organization) to create keys.

## The Authorization header

Every request carries an [API key](https://inola.ai/docs/reference/glossary.md#api-key) in the `Authorization` header:

```text
Authorization: Bearer <the secret of the key>
```

Live keys start with `ck_live_`. A request without the header, or with an unknown or revoked key, returns
`401 invalid_api_key`.

## Keys and organizations

Keys are created in the console ([https://console.inola.ai](https://console.inola.ai), section **API keys**). A key belongs to an
organization; the usage of all its keys is billed to that organization, and files and batches created with one key
are visible to every key of the same organization.

The secret is shown once, when the key is created. It cannot be shown again: a lost secret is replaced by a new key.

## Permissions

A key has one or more permissions ([scopes](https://inola.ai/docs/reference/glossary.md#scope)):

| permission | allows |
|---|---|
| `inference` | requests to models (`POST /v1/infer`), files and batches (`/v1/files`, `/v1/batches`) |
| `models:read` | the model list (`GET /v1/models`) |
| `usage:read` | usage statistics |

A request to an endpoint the key has no permission for returns `403 insufficient_scope`.

## Expiry and revocation

- A key can have an **expiry date**. After it, requests with the key return `401 api_key_expired`.
- A **revoked** key stops working within seconds; its requests return `401 invalid_api_key`.
- A key of a suspended organization returns `403 account_suspended`; a key of an organization with no balance left
  returns `402 payment_required`.

Replacing a key without downtime: create a new key, deploy it where the old one was used, check that requests with the
new key succeed, then revoke the old key.

## Where the key is kept

The key is a secret: anyone who has it can make requests billed to your organization. The examples in this
documentation read it from the `INOLA_API_KEY` environment variable. On servers, a secret manager or the
environment of the process keeps it out of the source code and the repository. A key placed in browser or mobile
app code can be read by every user of the app, so requests with it are made from a server.

## Example: a request and an authentication error

The scripts below send one request and report an authentication problem by its code.

### curl

```bash
curl -sS -i "https://api.inola.ai/v1/infer" \
  -H "Authorization: Bearer $INOLA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "cordon-mini", "input": {"state": "Meeting notes, 14 October: the vendor contract renews on 1 March."}}'
```

### Python

```python
import os
import sys

import requests

API_URL = "https://api.inola.ai"
API_KEY = os.environ.get("INOLA_API_KEY")
if not API_KEY:
    sys.exit("INOLA_API_KEY is not set")

response = requests.post(
    f"{API_URL}/v1/infer",
    headers={"Authorization": f"Bearer {API_KEY}"},
    json={"model": "cordon-mini", "input": {"state": "Meeting notes, 14 October: the vendor contract renews on 1 March."}},
    timeout=30,
)
if response.status_code in (401, 402, 403):
    error = response.json()["error"]
    sys.exit(f"The key was refused: {response.status_code} {error['code']}: {error['message']}")
response.raise_for_status()
print("The key works; request id", response.headers["X-Request-Id"])
```

### JavaScript

```javascript
const API_URL = "https://api.inola.ai";
const apiKey = process.env.INOLA_API_KEY;
if (!apiKey) {
  console.error("INOLA_API_KEY is not set");
  process.exit(1);
}

const response = await fetch(`${API_URL}/v1/infer`, {
  method: "POST",
  headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
  body: JSON.stringify({ model: "cordon-mini", input: { state: "Meeting notes, 14 October: the vendor contract renews on 1 March." } }),
});
if ([401, 402, 403].includes(response.status)) {
  const { error } = await response.json();
  console.error(`The key was refused: ${response.status} ${error.code}: ${error.message}`);
  process.exit(1);
}
if (!response.ok) {
  console.error(`Request failed with status ${response.status}`);
  process.exit(1);
}
console.log("The key works; request id", response.headers.get("X-Request-Id"));
```

A revoked or mistyped key returns:

```text
HTTP/2 401
content-type: application/json
x-request-id: req_01JBXR2D9F4H6K8M0P3S5V7X9Z

{"error": {"code": "invalid_api_key", "message": "Invalid API key."}}
```

## Authentication errors

| status | code | cause |
|---|---|---|
| 401 | [`invalid_api_key`](https://inola.ai/docs/reference/errors.md#invalid_api_key) | the header is missing, or the key is unknown or revoked |
| 401 | [`api_key_expired`](https://inola.ai/docs/reference/errors.md#api_key_expired) | the key is past its expiry date |
| 402 | [`payment_required`](https://inola.ai/docs/reference/errors.md#payment_required) | the organization has no balance left, or a payment failed |
| 403 | [`insufficient_scope`](https://inola.ai/docs/reference/errors.md#insufficient_scope) | the key lacks the permission for this endpoint |
| 403 | [`account_suspended`](https://inola.ai/docs/reference/errors.md#account_suspended) | the organization is suspended |

Rejected requests are not billed.

## Next steps

- [Quickstart](https://inola.ai/docs/quickstart.md) — the first request.
- [Error codes](https://inola.ai/docs/reference/errors.md) — every code and what to do.
- [Glossary](https://inola.ai/docs/reference/glossary.md) — the terms used here.
