# Authentication (/docs/authentication)

## API key [#api-key]

Your API key is on your [TorBox settings page](https://torbox.app/settings) under **API key**. Copy it, or
refresh it to get a new one. The key gives full access to your account, so keep it out of source control and
client-side code.

## Bearer header [#bearer-header]

Most endpoints take the key in the `Authorization` header:

```http
Authorization: Bearer <your api key>
```

Use [Get User Data](/operations/get_user_data) to verify your key.

```python
import os
from torbox import TorBox

client = TorBox(api_key=os.environ["TORBOX_API_KEY"])
result = client.user.get_user_data()
print(result)
```

[View endpoint](/operations/get_user_data)

A missing key is answered with `401 NO_AUTH`, a wrong one with `401 BAD_TOKEN`. A good key that is not allowed to
do something (for example because of the plan) gets `403`. See [Errors](/docs/errors).

## `token` query parameter (download links) [#token-query-parameter-download-links]

The download link endpoints (`/torrents/requestdl`, `/usenet/requestdl`, `/webdl/requestdl` and their `/batch`
variants) take the key as the `token` query parameter instead:

```text
https://api.torbox.app/v1/api/torrents/requestdl?token=APIKEY&torrent_id=NUMBER&file_id=NUMBER&redirect=true
```

With `redirect=true` this URL works as a permalink that redirects to the CDN link. To invalidate such
permalinks, reset your API key or delete the item from your dashboard.

<Callout type="warn">
A URL with `token=` contains your key. Share it only with people you would give your key to.
</Callout>

## Device code flow [#device-code-flow]

For TVs and other devices where typing a key is hard, apps can obtain a user's key with the device code flow
(`GET /v1/api/user/auth/device/start`, then `POST /v1/api/user/auth/device/token`). The steps are described on
[Start Device Code Authorization](/operations/start_device_code_authorization_process) and [Get Token From Device Code](/operations/get_token_from_device_code_process).

## Sign in with TorBox (OAuth) [#sign-in-with-torbox-oauth]

TorBox runs an OAuth 2.0 authorization server for MCP clients and other apps:

| | |
| --- | --- |
| Metadata | `https://api.torbox.app/.well-known/oauth-authorization-server` |
| Client registration | `POST https://api.torbox.app/register` (RFC 7591) |
| Authorization | `https://api.torbox.app/authorize` (authorization code, PKCE `S256` required) |
| Token | `POST https://api.torbox.app/token` |

The user approves the request on [torbox.app/oauth/device](https://torbox.app/oauth/device). The access token you
receive **is the user's API key**: there is no refresh token and it does not expire. Use it exactly like a key,
in the `Authorization: Bearer` header.

### Signing in on this site [#signing-in-on-this-site]

The **Sign in with TorBox** button in the header uses that same flow. After you approve, this site:

- keeps your key only in an encrypted, `httpOnly` session cookie (never in `localStorage`), and never logs it,
- fills it into the [API reference](/reference) playground, so "Send" runs requests against **your** account,
- forgets it when you click **Sign out** (the cookie is deleted).

**Resetting your API key in the [TorBox settings](https://torbox.app/settings) revokes it everywhere**, including
this site and every app you approved. Signing out here only deletes this site's copy.

The docs are fully readable without signing in.