> ## 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 Key を作成・管理します。

## 概要

ARouter は 2 種類のキーを使用します：

| キータイプ       | プレフィックス        | 目的                                   |
| ----------- | -------------- | ------------------------------------ |
| **管理キー**    | `lr_mgmt_xxxx` | 管理 API を通じて API Key を作成、一覧、更新、削除します。 |
| **API Key** | `lr_live_xxxx` | LLM リクエスト（チャット補完、埋め込みなど）を行います。       |

管理キーはスコープされたアクセス権を持つ API Key を作成できます：

* **許可されたプロバイダー** — 例：OpenAI と Anthropic のみ
* **許可されたモデル** — 例：`gpt-5.4` と `claude-sonnet-4.6` のみ
* **消費制限** — 日次/週次/月次の最大予算
* **有効期限** — 日付後に自動的に失効

```
管理キー: lr_mgmt_abc123
  ├── API Key 1: lr_live_def456  (OpenAI+Anthropic, $150/月, 2025年12月有効期限)
  ├── API Key 2: lr_live_ghi789  (DeepSeek のみ, $50/日)
  └── API Key 3: lr_live_jkl012  (すべてのプロバイダー, 無制限)
```

## API Key の作成

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl -X POST https://api.arouter.ai/api/v1/keys \
      -H "Authorization: Bearer lr_mgmt_xxxx" \
      -H "Content-Type: application/json" \
      -d '{
        "name": "production-backend",
        "allowed_providers": ["openai", "anthropic"],
        "allowed_models": ["gpt-5.4", "claude-sonnet-4.6"],
        "limit": 150,
        "limit_reset": "monthly",
        "expires_at": "2025-12-31T23:59:59Z"
      }'
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    import requests

    resp = requests.post(
        "https://api.arouter.ai/api/v1/keys",
        headers={"Authorization": "Bearer lr_mgmt_xxxx"},
        json={
            "name": "production-backend",
            "allowed_providers": ["openai", "anthropic"],
            "limit": 150,
            "limit_reset": "monthly",
            "expires_at": "2025-12-31T23:59:59Z",
        },
    )
    data = resp.json()
    print("API Key:", data["key"])
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    key, err := client.CreateKey(ctx, &arouter.CreateKeyRequest{
        Name:             "production-backend",
        AllowedProviders: []string{"openai", "anthropic"},
        Limit:            float64Ptr(150),
        LimitReset:       "monthly",
    })
    fmt.Println("API Key:", key.Key)
    ```
  </Tab>

  <Tab title="Node.js">
    ```typescript theme={null}
    const key = await router.createKey({
      name: "production-backend",
      allowed_providers: ["openai", "anthropic"],
      limit: 150,
      limit_reset: "monthly",
    });
    console.log("API Key:", key.key);
    ```
  </Tab>
</Tabs>

<Info>
  `key` フィールドは作成時に**一度だけ**返されます。安全に保存してください — 後から取得することはできません。
</Info>

## レスポンス

```json theme={null}
{
  "data": {
    "hash": "abc123...",
    "name": "production-backend",
    "key_type": "regular",
    "disabled": false,
    "limit": 150,
    "limit_remaining": 150,
    "limit_reset": "monthly",
    "allowed_providers": ["openai", "anthropic"],
    "allowed_models": ["gpt-5.4", "claude-sonnet-4.6"],
    "usage": 0,
    "created_at": "2025-01-15T10:30:00Z",
    "expires_at": "2025-12-31T23:59:59Z"
  },
  "key": "lr_live_xxxxxxxxxxxxxxxx"
}
```

## API Key の一覧表示

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

`page_size`、`page_token`、`offset` クエリパラメーターによるページネーションをサポートします。

## API Key の更新

```bash theme={null}
curl -X PATCH https://api.arouter.ai/api/v1/keys/KEY_HASH \
  -H "Authorization: Bearer lr_mgmt_xxxx" \
  -H "Content-Type: application/json" \
  -d '{"disabled": true}'
```

`name`、`disabled`、`limit`、`limit_reset`、`allowed_providers`、`allowed_models` を更新できます。

## API Key の削除

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

削除されたキーは直ちに無効化され、復元することはできません。

## ユースケース

| シナリオ             | 設定                                    |
| ---------------- | ------------------------------------- |
| **サービスごとの分離**    | 特定のモデルアクセスを持つマイクロサービスごとに 1 つの API Key |
| **コスト管理**        | チームまたは環境ごとの消費制限                       |
| **一時アクセス**       | 請負業者向けの `expires_at` 付き短期キー           |
| **プロバイダーロックダウン** | ステージングを安価なモデルのみに制限                    |
| **レート保護**        | ダッシュボードからキーごとに制限を調整                   |
