# Static CDN

> The Bitiful S4 static CDN API: query data transfer, request counts, and real-time logs, and purge the CDN cache.

Query a CDN domain's edge and origin fetch traffic, request counts, and real-time logs for the last 3 days, and purge the cache by URL or directory.

## Authentication

Every endpoint is authenticated with the `Authorization` request header, whose value is your console API token (no `Bearer` prefix). Get one at [Console › API Token](https://console.bitiful.com/apiToken).

```bash
curl -H "Authorization: <API Token>" "https://api.bitiful.com/..."
```

> **Warning**
>
> An API token can access the resources in your account. Use it on the server only, and never put it in frontend code or a public repository.

## Base URL

```text
https://api.bitiful.com
```

## Time parameters

- For the traffic and request count endpoints, `start_time` and `end_time` are Unix timestamps in **seconds**, and `period` determines whether the data is aggregated by hour, day, or month.
- For the real-time logs endpoint, `start_time` is a Unix timestamp in **nanoseconds**. Logs earlier than it are returned, and it also serves as the pagination cursor.

## Responses and errors

Every endpoint returns JSON. On success the status code is `200`, and submit-type endpoints return `{"message":"ok"}`. On failure the matching status code is returned, with the reason in `message`:

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

| Status code | Example `message`                                                                                                                                           | Description                                                                                             |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| `401`       | `鉴权失败` (authentication failed)                                                                                                                              | `Authorization` wasn't sent, or the API token is invalid.                                               |
| `403`       | `参数不合法` (invalid parameter)                                                                                                                                 | A parameter is missing, the time range is wrong, the `period` value is wrong, or the JSON is malformed. |
| `403`       | `异常错误` (unexpected error), `请求失败` (request failed)                                                                                                          | The domain doesn't belong to the current account.                                                       |
| `403`       | `目前只支持查看最近3天的日志` (only the last 3 days of logs can be viewed)                                                                                               | The logs endpoint's `start_time` is missing, earlier than 3 days ago, or not in nanoseconds.            |
| `403`       | `每次刷新的文件夹个数必须<=20个` (the number of folders per purge must be <=20), `URL刷新余量不足` (not enough URL purge quota), `目录刷新余量不足` (not enough directory purge quota) | The per-request or daily purge quota is exceeded.                                                       |
| `403`       | `您的账号已欠费，暂时无法访问该接口` (your account is overdue, this endpoint is temporarily unavailable), `您当前已欠费，请先充值` (you have an overdue balance, please top up first)   | You can't purge the cache while your balance is overdue.                                                |
| `403`       | `发生错误，请稍后重试` (an error occurred, try again later)                                                                                                           | A URL in the purge doesn't belong to the current account, or the submission failed.                     |
| `500`       | `异常错误`, `请求失败`                                                                                                                                              | Server error. Try again later.                                                                          |

## Quotas

| Item            | Limit                                                                    |
| --------------- | ------------------------------------------------------------------------ |
| URL purge       | 5000 per account per day, shared with the console                        |
| Directory purge | 100 per account per day, at most 20 per request, shared with the console |
| Real-time logs  | Last 3 days, at most 1000 entries per request                            |

## Examples

**Query traffic for the last 24 hours**

```bash
END=$(date +%s)
START=$((END - 86400))

curl -G "https://api.bitiful.com/cdn/data/cdn.example.com/traffic" \
  -H "Authorization: <API Token>" \
  --data-urlencode "start_time=${START}" \
  --data-urlencode "end_time=${END}" \
  --data-urlencode "period=hourly"
```

**Query error logs**

```bash
# Use the current time (in nanoseconds) for the first request. When paging, switch to the last entry's access_time from the previous page
curl -G "https://api.bitiful.com/cdn/data/cdn.example.com/logs" \
  -H "Authorization: <API Token>" \
  --data-urlencode "start_time=$(date +%s)000000000" \
  --data-urlencode "codes=4XX,5XX" \
  --data-urlencode "limit=100"
```

**Purge a directory**

```bash
curl -X POST "https://api.bitiful.com/cdn/cache/refresh" \
  -H "Authorization: <API Token>" \
  -H "Content-Type: application/json" \
  -d '{"type":"directory","url_list":["https://cdn.example.com/static/"]}'
```

For purge rules and default cache durations, see [Cache, purge, and prefetch](https://docs.bitiful.com/en/cdn/handbook/cache.md).
