> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arouter.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 管理 API 密鑰

> 管理 API 密鑰是專門用於 ARouter 管理員操作的獨立憑證 — 用於建立、列出和刪除 API 密鑰。不能用於 LLM 推理。

ARouter 使用兩種具有不同權限的 API 密鑰類型：

| 密鑰類型          | 前綴             | 用途                          |
| ------------- | -------------- | --------------------------- |
| **標準 API 密鑰** | `lr_live_...`  | LLM 推理，所有 `/v1/*` 端點        |
| **管理 API 密鑰** | `lr_admin_...` | 密鑰管理，僅限 `/api/v1/keys/*` 端點 |

管理密鑰不能呼叫 `/v1/chat/completions` 或其他推理端點。標準密鑰不能管理其他密鑰。

## 建立管理 API 密鑰

1. 前往 [ARouter 控制台](https://arouter.ai/keys)
2. 點擊**新增密鑰**
3. 選擇**管理密鑰**類型
4. 複製密鑰 — 僅顯示一次

## 使用場景

管理密鑰專為以下場景設計：

* **CI/CD 流水線** — 在部署時輪換應用密鑰
* **密鑰配置服務** — 為每個使用者或租戶建立密鑰
* **管理指令碼** — 稽核或清理未使用的密鑰
* **Terraform / 基礎設施即程式碼** — 管理密鑰生命週期

## 密鑰管理端點

所有管理端點都需要管理 API 密鑰：

### 列出密鑰

```bash theme={null}
curl https://api.arouter.ai/api/v1/keys \
  -H "Authorization: Bearer lr_admin_xxxx"
```

```json theme={null}
{
  "data": [
    {
      "hash": "abc123...",
      "name": "production-app",
      "label": "Production",
      "created_at": "2026-01-01T00:00:00Z",
      "last_used_at": "2026-04-01T12:00:00Z",
      "usage": {"prompt_tokens": 1500000, "completion_tokens": 450000, "cost": 12.50},
      "limit_usd": 100.0,
      "rate_limit": {"requests_per_minute": 100}
    }
  ]
}
```

### 建立密鑰

```bash theme={null}
curl -X POST https://api.arouter.ai/api/v1/keys \
  -H "Authorization: Bearer lr_admin_xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "staging-app",
    "label": "Staging",
    "limit_usd": 25.0,
    "allowed_models": ["openai/gpt-5.4", "anthropic/claude-sonnet-4.6"]
  }'
```

**回應：**

```json theme={null}
{
  "key": "lr_live_xxxx...",
  "hash": "def456...",
  "name": "staging-app"
}
```

<Note>
  `key` 值**僅在建立時返回一次**。請安全儲存 — 之後無法再次取得。
</Note>

### 更新密鑰

```bash theme={null}
curl -X PATCH https://api.arouter.ai/api/v1/keys/{key_hash} \
  -H "Authorization: Bearer lr_admin_xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "label": "Staging (v2)",
    "limit_usd": 50.0
  }'
```

### 刪除密鑰

```bash theme={null}
curl -X DELETE https://api.arouter.ai/api/v1/keys/{key_hash} \
  -H "Authorization: Bearer lr_admin_xxxx"
```

```json theme={null}
{"deleted": true, "hash": "def456..."}
```

***

## 密鑰物件參考

| 欄位                               | 類型               | 說明                      |
| -------------------------------- | ---------------- | ----------------------- |
| `hash`                           | `string`         | 唯一密鑰識別符（可安全儲存，不是密鑰本身）   |
| `name`                           | `string`         | 供您參考的內部名稱               |
| `label`                          | `string`         | 控制台中顯示的標籤               |
| `created_at`                     | `string`         | ISO 8601 建立時間戳          |
| `last_used_at`                   | `string`         | ISO 8601 最後使用時間戳        |
| `usage.prompt_tokens`            | `integer`        | 累計提示 token              |
| `usage.completion_tokens`        | `integer`        | 累計補全 token              |
| `usage.cost`                     | `number`         | 累計費用（美元）                |
| `limit_usd`                      | `number\|null`   | 消費限額。`null` = 不限制       |
| `rate_limit.requests_per_minute` | `integer\|null`  | 每分鐘請求數限制。`null` = 不限制   |
| `allowed_models`                 | `string[]\|null` | 模型白名單。`null` = 允許所有模型   |
| `allowed_providers`              | `string[]\|null` | 服務商白名單。`null` = 允許所有服務商 |

***

## 自動密鑰輪換

請參閱 [API 密鑰輪換](/zh-Hant/guides/administration/api-key-rotation) 取得使用管理密鑰進行零停機密鑰輪換的完整指南。

## 相關文件

* [密鑰管理](/zh-Hant/guides/key-management) — 從控制台管理密鑰
* [API 密鑰輪換](/zh-Hant/guides/administration/api-key-rotation) — 自動輪換策略
* [護欄](/zh-Hant/guides/features/guardrails) — 為密鑰設定限制和策略
