# Authentication (/docs/entwickler/authentication)

> Create API tokens, send them with each request, keep them out of client code, and revoke them.

Every request needs an API token in the `Authorization` header. A token belongs to the user account that created it: its requests count against that account, and it only sees files that this account uploaded.

```http
Authorization: Bearer pp-...
```

## Create a token
1. Sign in to PeakPrivacy and open [API Tokens](https://app.peakprivacy.ch/api-tokens).

2. Click **Generate Token**. Optionally name the token, for example after the application and environment. Only you see the name.

3. Copy the token from the **API Token Created** dialog. Tokens start with `pp-` and are shown only once.

If the page says that API token creation is not enabled for your account, ask PeakPrivacy to enable it. It is switched on per user account, not in your organisation's settings.

## Send the token

**curl**

```bash
curl https://api.peakprivacy.ch/v1/ai/models \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Accept: application/json"
```

**Python**

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.peakprivacy.ch/v1/ai",
    api_key=os.environ["PEAKPRIVACY_API_KEY"],  # sent as "Authorization: Bearer ..."
)
```

**JavaScript**

```js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.peakprivacy.ch/v1/ai",
  apiKey: process.env.PEAKPRIVACY_API_KEY, // sent as "Authorization: Bearer ..."
});
```

If a proxy strips or rewrites `Authorization`, send the token in an `api-token` header instead. Both headers work on every endpoint, including the Files API. If a request carries both, the API uses `api-token`.

```bash
curl https://api.peakprivacy.ch/v1/ai/models \
  -H "api-token: $PEAKPRIVACY_API_KEY" \
  -H "Accept: application/json"
```

## Keep tokens safe
A token gives full API access for its account, including the files uploaded with it.

- Call the API from your backend. Don't put tokens in browser code or in apps you distribute.
- Keep tokens out of source control, URLs and logs.
- Tokens don't expire. Use one token per application and environment, so you can revoke one without breaking the others. The **Last used** column shows which tokens are still in use.
- To rotate a token, generate a new one, deploy it, check that requests succeed, then revoke the old one.

## Revoke a token
1. Open [API Tokens](https://app.peakprivacy.ch/api-tokens) and find the token by its name or masked value.

2. Click **Revoke Token**, enter your account password and click **Revoke Token** in the dialog.

If your account has no password, for example because you signed up with Google, GitHub or LinkedIn, the dialog shows a notice instead of the password field and you can't revoke. Click **Profile** in the notice, set a password, then revoke the token.

> **Irreversible:** revoking takes effect immediately and can't be undone. Every application that uses the token gets `401` from its next request. The list keeps the token, marked **Revoked**.

## Errors
A missing, unknown or revoked token returns `401`:

```json
{ "error": "Unauthenticated" }
```

Check the header name, the `Bearer ` prefix and whether the token was revoked. Retrying with the same token won't help.

## See also
- [API quickstart](/docs/entwickler)
- [Errors and limits](/docs/entwickler/errors)
- [Files](/docs/entwickler/files)
