# 令牌管理（高级接口API）

使用 **账户权限密钥** 以编程方式管理您个人名下的 API 令牌，支持查询列表与详情、创建新令牌、修改额度/次数/有效期/模型与 IP 限制、启停状态、清零已用额度以及删除令牌。

> [!IMPORTANT]
> - **Base URL**：`https://ai.artsmcp.com/api/open/v1`
> - **鉴权请求头（三选一）**：`Authorization: Bearer <your_account_key>`、`X-Account-Api-Key: <your_account_key>` 或 `X-Api-Key: <your_account_key>`
> - **令牌标识与脱敏**：每把令牌的唯一路径标识为 `kid`（如 `086421`）；完整明文 `token_key` 仅在创建成功时返回一次，其余接口均返回脱敏格式（如 `sk-9xA2****kP8Q`）
> - **时间与并发**：时间字段统一按 **站点运营时区** 输出；接口受频率与并发限制保护，超限时返回 HTTP `429`

---

### 1. 获取密钥列表

获取当前账户名下的个人 API 密钥列表，包含实时周期用量、调用次数与不可用原因，与控制台「令牌管理」页面数据保持一致。

* **路径**：`GET /api/open/v1/tokens`
* **查询参数 (Query)**：

| 参数名 | 类型 | 默认值 | 说明 |
| :--- | :--- | :--- | :--- |
| `include_playground_2026` | `integer` | `1` | 是否包含系统专用密钥（`1` 包含，`0` 仅返回常规密钥） |
| `kid` | `string` | 无 | 按令牌 `kid` 精确筛选（如 `086421`） |
| `is_active` | `integer` | 全部 | 按状态筛选：`1` 启用、`0` 手动禁用、`2` 随账号禁用、`3` 超 30 天闲置自动关闭 |
| `keyword` | `string` | 无 | 按密钥名称或 `kid` 关键词模糊过滤 |

* **调用示例 (Curl)**：

```bash
curl -s "https://ai.artsmcp.com/api/open/v1/tokens?is_active=1" \
  -H "Authorization: Bearer <your_account_key>"
```

* **响应示例**：

```json
{
  "data": [
    {
      "team_id": 0,
      "kid": "086421",
      "name": "production-app",
      "token_key": "sk-9xA2****kP8Q",
      "quota_limit": 500.0,
      "quota_used": 12.45,
      "daily_quota_limit": 50.0,
      "daily_quota_used": 1.8,
      "current_daily_quota_used": 1.8,
      "weekly_quota_limit": -1.0,
      "weekly_quota_used": 6.2,
      "current_weekly_quota_used": 6.2,
      "monthly_quota_limit": 200.0,
      "monthly_quota_used": 12.45,
      "current_monthly_quota_used": 12.45,
      "usage_limit": 10000,
      "usage_used": 318,
      "rps_limit": 10,
      "rpm_limit": 300,
      "allowed_models": "[\"gpt-4o\",\"claude-sonnet-4-6\"]",
      "allowed_ips": "192.168.1.0/24",
      "only_playground": 0,
      "only_playground_2026": 0,
      "high_availability": 1,
      "is_active": 1,
      "unavailable_reason": null,
      "last_reset_day": "2026-11-15",
      "last_reset_week": "2026-46",
      "last_reset_month": "2026-11",
      "expires_at": "2026-12-31 23:59:59 (UTC+8)",
      "last_used_at": "2026-11-15 10:12:00 (UTC+8)",
      "created_at": "2026-11-10 16:00:00 (UTC+8)",
      "updated_at": "2026-11-15 09:30:00 (UTC+8)"
    }
  ],
  "total": 1
}
```

* **返回字段说明**：

| 字段名 | 类型 | 说明 |
| :--- | :--- | :--- |
| `data` | `array` | 令牌对象列表（单个令牌详情/创建/修改/启停/重置接口直接返回单个令牌对象） |
| `kid` | `string` | 令牌唯一编号（如 `086421`），用于后续单令牌路径参数 `{kid}` |
| `name` | `string` | 令牌名称 |
| `token_key` | `string` | 脱敏密钥串（仅创建接口返回一次完整明文） |
| `quota_limit` / `quota_used` | `number` | 总消费额度上限（`-1` 为不限）与累计已用总额度 |
| `daily_quota_limit` / `weekly_quota_limit` / `monthly_quota_limit` | `number` | 自然日/周/月额度上限（`-1` 为不限） |
| `current_daily_quota_used` / `current_weekly_quota_used` / `current_monthly_quota_used` | `number` | 当前自然日/周/月实时已用额度（跨周期自动归零） |
| `usage_limit` / `usage_used` | `integer` | 总调用次数上限（`-1` 为不限）与累计调用次数 |
| `rps_limit` / `rpm_limit` | `integer` | 每秒/每分钟请求速率上限（`0` 为不限） |
| `allowed_models` | `string` | 允许调用的模型 ID 白名单 JSON 数组字符串（空串表示不限） |
| `allowed_ips` | `string` | 允许来源 IP / CIDR 白名单（英文逗号分隔，空串表示不限） |
| `high_availability` | `integer` | 高可用模式开关（`1` 开启，`0` 关闭） |
| `is_active` | `integer` | 令牌状态（`1` 启用、`0` 手动禁用、`2` 随账号禁用、`3` 闲置自动关闭） |
| `unavailable_reason` | `string \| null` | 启用但当前不可调用时的原因（`arrears` / `no_funds` / `expired` / `quota_total` / `quota_daily` / `quota_weekly` / `quota_monthly` / `quota_count`），正常时为 `null` |
| `expires_at` / `last_used_at` / `created_at` / `updated_at` | `string \| null` | 过期时间、最近使用时间、创建时间与更新时间（按站点运营时区格式化） |
| `total` | `integer` | 符合筛选条件的令牌总数 |

---

### 2. 获取单个密钥详情

按令牌唯一编号 `kid` 查询单把密钥的完整配置、实时周期用量与调用次数统计（返回结构同第 1 节单条令牌对象）。

* **路径**：`GET /api/open/v1/tokens/{kid}`
* **路径参数**：`kid` — 令牌唯一编号（如 `086421`）
* **调用示例 (Curl)**：

```bash
curl -s "https://ai.artsmcp.com/api/open/v1/tokens/086421" \
  -H "Authorization: Bearer <your_account_key>"
```

---

### 3. 创建新密钥

创建新的个人 API 密钥。创建成功时响应中的 `token_key` 返回完整明文（仅返回这一次，请妥善保存）。

* **路径**：`POST /api/open/v1/tokens`
* **请求体参数 (JSON)**：

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| `name` | `string` | 否 | 密钥名称（默认 `default`，最多 24 字符，仅支持字母、数字、空格、`_` 与 `-`） |
| `quota_limit` | `number \| null` | 否 | 总消费额度上限，`-1`、`null` 或不传为不限 |
| `daily_quota_limit` | `number \| null` | 否 | 每日消费额度上限，`-1` 或 `null` 为不限 |
| `weekly_quota_limit` | `number \| null` | 否 | 每周消费额度上限，`-1` 或 `null` 为不限 |
| `monthly_quota_limit` | `number \| null` | 否 | 每月消费额度上限，`-1` 或 `null` 为不限 |
| `usage_limit` | `integer \| null` | 否 | 总调用次数上限，`-1` 或 `null` 为不限 |
| `rps_limit` | `integer \| null` | 否 | 每秒请求速率上限 (RPS)，`0` 或 `null` 为不限 |
| `rpm_limit` | `integer \| null` | 否 | 每分钟请求速率上限 (RPM)，`0` 或 `null` 为不限 |
| `allowed_models` | `string[] \| null` | 否 | 允许调用的模型 ID 列表，空或 `null` 表示不限 |
| `allowed_ips` | `string \| null` | 否 | 允许来源 IP / CIDR（英文逗号分隔） |
| `expires_at` | `string \| null` | 否 | 绝对过期时间（支持 `YYYY-MM-DD HH:mm:ss`、`YYYY-MM-DD` 或 ISO 8601）；传 `""` 为长期有效 |
| `expire_days` | `integer` | 否 | 相对当前时间的有效天数（如 `30`，不可与非空 `expires_at` 同时传入） |
| `high_availability` | `integer` | 否 | 高可用模式开关：`1` 开启（默认），`0` 关闭 |
| `is_active` | `integer` | 否 | 初始状态：`1` 启用（默认），`0` 停用 |

* **调用示例 (Curl)**：

```bash
curl -s -X POST "https://ai.artsmcp.com/api/open/v1/tokens" \
  -H "Authorization: Bearer <your_account_key>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "backend-worker-01",
    "quota_limit": 200.0,
    "daily_quota_limit": 25.0,
    "weekly_quota_limit": 100.0,
    "monthly_quota_limit": 200.0,
    "usage_limit": 5000,
    "rps_limit": 5,
    "rpm_limit": 120,
    "allowed_models": ["gpt-4o", "gemini-2.5-flash"],
    "allowed_ips": "203.0.113.10,10.0.0.0/8",
    "expire_days": 30,
    "is_active": 1
  }'
```

---

### 4. 修改已有密钥

增量更新已有密钥的配置（仅传入需更新的字段，未传字段保持不变；返回对象中 `token_key` 保持脱敏）。

* **路径**：`PUT /api/open/v1/tokens/{kid}` 或 `PATCH /api/open/v1/tokens/{kid}`
* **路径参数**：`kid` — 令牌唯一编号
* **请求体参数 (JSON)**：

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| `name` | `string` | 否 | 密钥名称（最多 24 字符） |
| `quota_limit` | `number \| null` | 否 | 总消费额度上限；传 `-1` 或 `null` 取消限制 |
| `daily_quota_limit` | `number \| null` | 否 | 每日消费额度上限；传 `-1` 或 `null` 取消限制 |
| `weekly_quota_limit` | `number \| null` | 否 | 每周消费额度上限；传 `-1` 或 `null` 取消限制 |
| `monthly_quota_limit` | `number \| null` | 否 | 每月消费额度上限；传 `-1` 或 `null` 取消限制 |
| `usage_limit` | `integer \| null` | 否 | 总调用次数上限；传 `-1` 或 `null` 取消限制 |
| `rps_limit` | `integer \| null` | 否 | 每秒请求速率上限 (RPS)；传 `0` 或 `null` 取消限制 |
| `rpm_limit` | `integer \| null` | 否 | 每分钟请求速率上限 (RPM)；传 `0` 或 `null` 取消限制 |
| `allowed_models` | `string[]` | 否 | 允许调用的模型 ID 列表；传 `[]` 取消模型限制 |
| `allowed_ips` | `string` | 否 | 允许来源 IP / CIDR（英文逗号分隔）；传 `""` 清空白名单 |
| `expires_at` | `string \| null` | 否 | 绝对过期时间；传 `""`、`null` 或 `"permanent"` 改为长期有效 |
| `expire_days` | `integer` | 否 | 从当前时刻起顺延的有效天数（`1..36500`，不可与非空 `expires_at` 同时传入） |
| `high_availability` | `integer` | 否 | 高可用模式开关：`1` 开启，`0` 关闭 |
| `is_active` | `integer` | 否 | 启停状态：`1` 启用，`0` 停用（系统锁定专用密钥仅允许修改此字段） |

* **调用示例 (Curl)**：

```bash
curl -s -X PATCH "https://ai.artsmcp.com/api/open/v1/tokens/086421" \
  -H "Authorization: Bearer <your_account_key>" \
  -H "Content-Type: application/json" \
  -d '{
    "quota_limit": 1000.0,
    "daily_quota_limit": 80.0,
    "weekly_quota_limit": 300.0,
    "monthly_quota_limit": null,
    "usage_limit": 5000,
    "expires_at": "2027-06-30 23:59:59"
  }'
```

---

### 5. 快捷切换密钥启停状态

快速启用或禁用指定密钥，无需提交完整配置。

* **路径**：`PATCH /api/open/v1/tokens/{kid}/status` 或 `POST /api/open/v1/tokens/{kid}/status`
* **路径参数**：`kid` — 令牌唯一编号
* **请求体参数 (JSON)**：

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| `is_active` | `integer` | 否 | `1` 启用，`0` 停用（与 `enabled` 二选一） |
| `enabled` | `boolean` | 否 | `true` 启用，`false` 停用（与 `is_active` 二选一） |

* **调用示例 (Curl)**：

```bash
curl -s -X PATCH "https://ai.artsmcp.com/api/open/v1/tokens/086421/status" \
  -H "Authorization: Bearer <your_account_key>" \
  -H "Content-Type: application/json" \
  -d '{"is_active": 0}'
```

---

### 6. 清空密钥已用额度与次数

按指定维度清零密钥的已用额度或调用次数计数器（不影响账户钱包真实账单）。

* **路径**：`POST /api/open/v1/tokens/{kid}/reset-usage`
* **路径参数**：`kid` — 令牌唯一编号
* **请求体参数 (JSON)**：

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| `target` | `string` | 否 | 清零维度：`all`（默认，全部清零）、`total`、`daily`、`weekly`、`monthly`、`usage_count`（也支持作为 Query 参数传入） |

* **调用示例 (Curl)**：

```bash
curl -s -X POST "https://ai.artsmcp.com/api/open/v1/tokens/086421/reset-usage" \
  -H "Authorization: Bearer <your_account_key>" \
  -H "Content-Type: application/json" \
  -d '{"target": "daily"}'
```

---

### 7. 删除密钥

永久删除指定个人密钥，删除后立即失效且无法恢复（系统锁定专用密钥除外）。

* **路径**：`DELETE /api/open/v1/tokens/{kid}`
* **路径参数**：`kid` — 令牌唯一编号
* **调用示例 (Curl)**：

```bash
curl -s -X DELETE "https://ai.artsmcp.com/api/open/v1/tokens/086421" \
  -H "Authorization: Bearer <your_account_key>"
```

* **响应示例**：

```json
{
  "success": true,
  "kid": "086421",
  "message": "Token deleted successfully"
}
```

* **返回字段说明**：

| 字段名 | 类型 | 说明 |
| :--- | :--- | :--- |
| `success` | `boolean` | 是否删除成功 |
| `kid` | `string` | 已删除令牌的编号 |
| `message` | `string` | 操作结果说明 |
