# Gestion des Tokens (API Avancée)

Use your **Account Permission Key** to programmatically manage your personal API tokens: list and inspect tokens, create new tokens, update quota/call count/expiration/model/IP limits, toggle status, reset used quota, and delete tokens.

> [!IMPORTANT]
> - **Base URL**: `https://ai.artsmcp.com/api/open/v1`
> - **Auth Header (Choose One)**: `Authorization: Bearer <your_account_key>`, `X-Account-Api-Key: <your_account_key>`, or `X-Api-Key: <your_account_key>`
> - **Token Identifier & Masking**: Every token is addressed by its unique `kid` (e.g., `086421`); the full plaintext `token_key` is returned once upon creation, while all other endpoints return a masked value (e.g., `sk-9xA2****kP8Q`)
> - **Time & Rate Limits**: Timestamps use the **site operating timezone**; requests exceeding rate/concurrency limits return HTTP `429`

---

### 1. List Tokens

Retrieve all personal API tokens under the current account, including real-time period usage, call counts, and unavailable reasons, matching Console **Token Management**.

* **Path**: `GET /api/open/v1/tokens`
* **Query Parameters**:

| Parameter | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `include_playground_2026` | `integer` | `1` | Include system-dedicated tokens (`1` include, `0` regular tokens only) |
| `kid` | `string` | None | Exact filter by token `kid` (e.g., `086421`) |
| `is_active` | `integer` | All | Filter by status: `1` active, `0` disabled, `2` disabled with account, `3` auto-disabled after 30 days idle |
| `keyword` | `string` | None | Fuzzy filter by token name or `kid` |

* **Example (Curl)**:

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

* **Response Example**:

```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
}
```

* **Response Fields**:

| Field | Type | Description |
| :--- | :--- | :--- |
| `data` | `array` | List of token objects (detail/create/update/status/reset endpoints return a single token object) |
| `kid` | `string` | Unique token identifier (e.g., `086421`), used as `{kid}` in single-token routes |
| `name` | `string` | Token name |
| `token_key` | `string` | Masked key string (full plaintext returned only once on creation) |
| `quota_limit` / `quota_used` | `number` | Total quota limit (`-1` unlimited) and cumulative quota used |
| `daily_quota_limit` / `weekly_quota_limit` / `monthly_quota_limit` | `number` | Daily/weekly/monthly quota limits (`-1` unlimited) |
| `current_daily_quota_used` / `current_weekly_quota_used` / `current_monthly_quota_used` | `number` | Real-time usage in the current calendar day/week/month (auto-resets each period) |
| `usage_limit` / `usage_used` | `integer` | Total call count limit (`-1` unlimited) and cumulative calls used |
| `rps_limit` / `rpm_limit` | `integer` | Requests-per-second / requests-per-minute limits (`0` unlimited) |
| `allowed_models` | `string` | Allowed model IDs as a JSON array string (empty string means unlimited) |
| `allowed_ips` | `string` | Allowed source IPs / CIDRs (comma-separated; empty string means unlimited) |
| `high_availability` | `integer` | High-availability toggle (`1` enabled, `0` disabled) |
| `is_active` | `integer` | Token status (`1` active, `0` disabled, `2` disabled with account, `3` auto-disabled idle) |
| `unavailable_reason` | `string \| null` | Reason if enabled but currently blocked (`arrears` / `no_funds` / `expired` / `quota_total` / `quota_daily` / `quota_weekly` / `quota_monthly` / `quota_count`), or `null` when usable |
| `expires_at` / `last_used_at` / `created_at` / `updated_at` | `string \| null` | Expiration, last used, created, and updated timestamps in site operating timezone |
| `total` | `integer` | Total matching tokens |

---

### 2. Get Single Token Detail

Retrieve configuration, real-time period usage, and call counts for a single token by `kid` (response matches a single token object from Section 1).

* **Path**: `GET /api/open/v1/tokens/{kid}`
* **Path Parameter**: `kid` — Unique token identifier (e.g., `086421`)
* **Example (Curl)**:

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

---

### 3. Create Token

Create a new personal API token. On success, `token_key` contains the full plaintext key (shown only once—store it securely).

* **Path**: `POST /api/open/v1/tokens`
* **Request Body (JSON)**:

| Field | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `name` | `string` | No | Token name (defaults to `default`, up to 24 chars: letters, digits, spaces, `_`, `-`) |
| `quota_limit` | `number \| null` | No | Total quota limit (`-1`, `null`, or omitted = unlimited) |
| `daily_quota_limit` | `number \| null` | No | Daily quota limit (`-1` or `null` = unlimited) |
| `weekly_quota_limit` | `number \| null` | No | Weekly quota limit (`-1` or `null` = unlimited) |
| `monthly_quota_limit` | `number \| null` | No | Monthly quota limit (`-1` or `null` = unlimited) |
| `usage_limit` | `integer \| null` | No | Total call count limit (`-1` or `null` = unlimited) |
| `rps_limit` | `integer \| null` | No | Requests-per-second limit (`0` or `null` = unlimited) |
| `rpm_limit` | `integer \| null` | No | Requests-per-minute limit (`0` or `null` = unlimited) |
| `allowed_models` | `string[] \| null` | No | Allowed model IDs (empty or `null` = unlimited) |
| `allowed_ips` | `string \| null` | No | Allowed source IPs / CIDRs (comma-separated) |
| `expires_at` | `string \| null` | No | Absolute expiration (`YYYY-MM-DD HH:mm:ss`, `YYYY-MM-DD`, or ISO 8601; `""` = never expires) |
| `expire_days` | `integer` | No | Valid days from now (e.g., `30`; do not combine with non-empty `expires_at`) |
| `high_availability` | `integer` | No | High-availability toggle: `1` enabled (default), `0` disabled |
| `is_active` | `integer` | No | Initial status: `1` active (default), `0` disabled |

* **Example (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. Update Token

Incrementally update an existing token (omit fields to leave them unchanged; `token_key` in the response remains masked).

* **Path**: `PUT /api/open/v1/tokens/{kid}` or `PATCH /api/open/v1/tokens/{kid}`
* **Path Parameter**: `kid` — Unique token identifier
* **Request Body (JSON)**:

| Field | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `name` | `string` | No | Token name (up to 24 chars) |
| `quota_limit` | `number \| null` | No | Total quota limit; pass `-1` or `null` to remove limit |
| `daily_quota_limit` | `number \| null` | No | Daily quota limit; pass `-1` or `null` to remove limit |
| `weekly_quota_limit` | `number \| null` | No | Weekly quota limit; pass `-1` or `null` to remove limit |
| `monthly_quota_limit` | `number \| null` | No | Monthly quota limit; pass `-1` or `null` to remove limit |
| `usage_limit` | `integer \| null` | No | Call count limit; pass `-1` or `null` to remove limit |
| `rps_limit` | `integer \| null` | No | RPS limit; pass `0` or `null` to remove limit |
| `rpm_limit` | `integer \| null` | No | RPM limit; pass `0` or `null` to remove limit |
| `allowed_models` | `string[]` | No | Allowed model IDs; pass `[]` to allow all models |
| `allowed_ips` | `string` | No | Allowed IPs / CIDRs (comma-separated); pass `""` to clear |
| `expires_at` | `string \| null` | No | Absolute expiration; pass `""`, `null`, or `"permanent"` for no expiration |
| `expire_days` | `integer` | No | Valid days from now (`1..36500`; do not combine with non-empty `expires_at`) |
| `high_availability` | `integer` | No | High-availability toggle: `1` enabled, `0` disabled |
| `is_active` | `integer` | No | Active status: `1` active, `0` disabled (the only editable field on system-locked tokens) |

* **Example (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. Toggle Token Status

Quickly enable or disable a token without sending a full configuration payload.

* **Path**: `PATCH /api/open/v1/tokens/{kid}/status` or `POST /api/open/v1/tokens/{kid}/status`
* **Path Parameter**: `kid` — Unique token identifier
* **Request Body (JSON)**:

| Field | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `is_active` | `integer` | No | `1` enable, `0` disable (provide either `is_active` or `enabled`) |
| `enabled` | `boolean` | No | `true` enable, `false` disable (provide either `is_active` or `enabled`) |

* **Example (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. Reset Token Usage

Reset the token's used quota or call count counters along the specified scope (does not affect actual wallet billing records).

* **Path**: `POST /api/open/v1/tokens/{kid}/reset-usage`
* **Path Parameter**: `kid` — Unique token identifier
* **Request Body (JSON)**:

| Field | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `target` | `string` | No | Scope to reset: `all` (default), `total`, `daily`, `weekly`, `monthly`, `usage_count` (also accepted as a query parameter) |

* **Example (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 Token

Permanently delete a personal API token (immediately invalidated and irreversible; system-locked tokens cannot be deleted).

* **Path**: `DELETE /api/open/v1/tokens/{kid}`
* **Path Parameter**: `kid` — Unique token identifier
* **Example (Curl)**:

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

* **Response Example**:

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

* **Response Fields**:

| Field | Type | Description |
| :--- | :--- | :--- |
| `success` | `boolean` | Whether deletion succeeded |
| `kid` | `string` | Identifier of the deleted token |
| `message` | `string` | Result message |
