# API tokens

> Create, view, and delete API tokens, and use them to call Bitiful's data, CDN, and certificate APIs.

An API token is used to call Bitiful's HTTP APIs, for example to query bucket capacity, pull access logs, purge the CDN cache, and fetch certificate contents. It's a different credential from an AccessKey, and the two can't be used interchangeably.

|                    | API token                            | AccessKey                                                                                  |
| ------------------ | ------------------------------------ | ------------------------------------------------------------------------------------------ |
| Where it's used    | The API at `https://api.bitiful.com` | The S3 protocol: SDKs, rclone, presigned URLs, and so on                                   |
| Permission scope   | The whole main account               | The buckets and directories granted to its sub-user                                        |
| Where to create it | Avatar › **API Token**               | **Object storage › Sub-users & Keys (对象存储 › 子账户\&Key)**                                    |
| Docs               | This page                            | [Sub-users and AccessKeys](https://docs.bitiful.com/en/bitiful-s4/handbook/access-keys.md) |

## Create a token

### Open the API Token page

Click the avatar in the upper-right corner › **API Token**, or open <https://console.bitiful.com/apiToken> directly.

### Create

Click **Create Token (创建 Token)** in the upper-right corner. After you see "创建成功" (created successfully), the new token appears in the list and is usable immediately.

Each account can have up to 3 tokens. If you already have 3, creating another shows "最多只能创建3个token" (at most 3 tokens can be created). Tokens have no name and no expiration time, so keep your own record of where each token is used.

## View and copy

By default, a token in the list shows only the first and last 4 characters. Click the eye icon on the left to show the full content, then click the copy icon next to it. You can view the full content again at any time.

## Use a token

Put the token as is in the `Authorization` request header, **without a `Bearer` prefix**:

```bash
curl -H "Authorization: <API Token>" \
  "https://api.bitiful.com/s4/data/<bucket-name>/usage"
```

When authentication fails, it returns HTTP 401:

```json
{"message":"鉴权失败"}
```

The message means "authentication failed".

Available endpoints:

[**Object storage API**](https://docs.bitiful.com/en/developer/api/s4.md)

Query capacity, file counts, and access logs.

[**Static CDN API**](https://docs.bitiful.com/en/developer/api/cdn.md)

Query traffic, request counts, and logs, and purge the cache.

[**Certificate management API**](https://docs.bitiful.com/en/developer/api/certs.md)

List, fetch, and upload certificates.

> **Warning**
>
> A token has API permissions for the whole account, including reading certificate private keys. Use it only on the server. Don't put it in web pages, apps, public repositories, or logs.

## Delete and rotate

Click **Delete (删除)** in the list and confirm in "确定删除？" (delete it?). It stops working immediately after deletion, programs using it get a 401, and it can't be recovered.

To rotate tokens regularly: create a new token first, switch your programs over, and delete the old one after you confirm everything works.

## Impact of an overdue balance

When your account has an overdue balance, query endpoints (GET) still work, but POST endpoints such as cache purge and certificate upload return HTTP 403:

```json
{"message":"您的账号已欠费，暂时无法访问该接口"}
```

The message means "your account is overdue, this endpoint is temporarily unavailable".

## FAQ

<details>
<summary>It keeps returning 401 “鉴权失败” (authentication failed)</summary>

Check in order: whether you added an extra `Bearer `; whether you used an AccessKey by mistake; whether the token has been deleted; and whether you copied extra characters.

</details>

<details>
<summary>Can I limit a token to read only one bucket?</summary>

No. A token's permissions cover the whole account. To restrict permissions by bucket or directory, use a sub-user's AccessKey over the S3 protocol.

</details>

<details>
<summary>Can a token be used to upload files?</summary>

No. For uploads and downloads, use an AccessKey and an [S3 SDK](https://docs.bitiful.com/en/developer/sdk/intro.md).

</details>
