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

# 用量查询 API

> 用 API key 查询组织的用量、每个 key 的花费和调用明细。

用量查询 API 让**组织管理员**用 API key 读取整个组织的用量，数字和控制台「用量」「调用记录」页面一致：按成员、按 key、按标签、按模型、按天的用量，带费用拆分的逐条调用明细，以及 CSV 导出。

> [!NOTE]
>
> 用量查询 API 按组织开通。需要开通请联系[客服](https://routerlink.ai/support)，并提供组织名称。

## 鉴权

和调用模型一样，在 `Authorization` 头里带 API key：

```bash theme={null}
-H "Authorization: Bearer <ROUTERLINK_API_KEY>"
```

| 接口 | 用哪种 key |
| - | - |
| `GET /key` | 已开通组织的任意 key（返回这把 key 自己的数据） |
| `GET /org/usage`、`GET /org/requests`、`GET /org/requests/export` | **组织 key**，且 key 的所有者当前是该组织的**管理员** |

Base URL：`https://router-link.world3.ai/api/v1`

key 自身的限制在这里同样生效：已禁用、已过期的 key，或者不在 key 的 IP 白名单内发起的请求，和调用模型时一样会被拒绝。

## 时间范围

* `start`、`end` 是 ISO 8601 时刻，例如 `2026-11-01T00:00:00Z`；不带时区按 UTC。
* 区间是 `[start, end)`。`end` 默认现在，`start` 默认 `end` 前 24 小时，最长 **31 天**。
* 按天、按周分组（`group_by=day|week`）按\*\*北京时间（UTC+8）\*\*切分，和控制台图表一致；每周从周一开始。

## 查询当前 key

**GET /key** `endpoint`

调用这把 key 的设置、预算和近期用量。

```bash theme={null}
curl https://router-link.world3.ai/api/v1/key \
  -H "Authorization: Bearer <ROUTERLINK_API_KEY>"
```

```json theme={null}
{
  "api_key_id": "7f0c…",
  "key_name": "prod",
  "key_masked": "sk-or-v1-abc***xyz",
  "scope": "team",
  "org_id": "3c3f…",
  "is_active": true,
  "created_at": "2026-10-01T08:00:00+00:00",
  "expires_at": null,
  "model_allowlist": null,
  "budget_usd": 100.0,
  "budget_used_usd": 12.5,
  "budget_remaining_usd": 87.5,
  "budget_reset": "monthly",
  "resets_at": "2026-12-01T00:00:00+00:00",
  "usage": {
    "today":    { "requests": 3, "total_tokens": 4100, "cost_usd": 0.012, "cost_credits": 12.0, "cache_read_tokens": 0, "cache_write_tokens": 0, "cache_read_credits": 0.0, "cache_write_credits": 0.0 },
    "month":    { "…": "字段同上" },
    "last_30d": { "…": "字段同上" }
  }
}
```

* key 没设预算时 `budget_*` 为 `null`；`resets_at` 是当前预算周期的结束时刻。
* `today`、`month` 按 UTC 日和 UTC 自然月起算；`last_30d` 是从现在往前 30 天。

## 组织用量汇总

**GET /org/usage** `endpoint`

整个组织的用量，可分组、可筛选。仅限组织管理员的 key。

| 参数 | 说明 |
| - | - |
| `start`、`end` | 时间范围（见上） |
| `group_by` | `member`（默认）、`key`、`tag`、`model`、`day`、`week` |
| `member` | 只看这个成员（user id）名下的 key |
| `api_key_id` | 只看这把 key |
| `tag` | 只看带这个标签的 key |
| `model` | 只看这个模型（展示名或模型 id，不区分大小写） |

筛选条件可以组合。`group_by=member` 时只接受 `member` 和 `model` 两种筛选。

> [tab] 按 key 看花费
>
> ```bash theme={null}
> curl "https://router-link.world3.ai/api/v1/org/usage?group_by=key&start=2026-11-01T00:00:00Z&end=2026-11-24T00:00:00Z" \
>   -H "Authorization: Bearer <ROUTERLINK_API_KEY>"
> ```

> [tab] 某个成员按天
>
> ```bash theme={null}
> curl "https://router-link.world3.ai/api/v1/org/usage?group_by=day&member=<USER_ID>&start=2026-11-17T00:00:00Z" \
>   -H "Authorization: Bearer <ROUTERLINK_API_KEY>"
> ```

> [tab] Python
>
> ```python theme={null}
> import httpx
>
> r = httpx.get(
>     "https://router-link.world3.ai/api/v1/org/usage",
>     headers={"Authorization": "Bearer <ROUTERLINK_API_KEY>"},
>     params={"group_by": "member", "start": "2026-11-01T00:00:00Z"},
>     timeout=60,
> )
> r.raise_for_status()
> for row in r.json()["rows"]:
>     print(row["user_email"], row["requests"], row["cost_usd"])
> ```

```json theme={null}
{
  "org_id": "3c3f…",
  "start": "2026-11-01T00:00:00+00:00",
  "end": "2026-11-24T00:00:00+00:00",
  "group_by": "key",
  "timezone": "Asia/Shanghai",
  "totals": { "requests": 120, "total_tokens": 98000, "cost_usd": 1.23 },
  "rows": [
    {
      "api_key_id": "7f0c…", "key_name": "prod", "key_masked": "sk-or-v1-abc***xyz", "status": "active",
      "user_id": "u_…", "user_email": "a@example.com", "role": "admin", "last_used": "2026-11-23T09:12:00+00:00",
      "requests": 100, "total_tokens": 90000, "cost_usd": 1.0,
      "cache_read_tokens": 0, "cache_write_tokens": 0, "cache_read_credits": 0.0, "cache_write_credits": 0.0
    }
  ]
}
```

各分组的行：

| `group_by` | 每行是 | 主要字段 |
| - | - | - |
| `member` | 一个成员（离开后又加入的成员，每段在组织的时间各一行） | `user_id`、`user_email`、`role`、`status`（`Active` / `Removed`）、`keys` |
| `key` | 一把未删除的 key（已禁用的 key `status` 为 `revoked`） | `api_key_id`、`key_name`、`key_masked`、`status`、所有者信息、`last_used` |
| `tag` | 一个标签（`null` 表示无标签） | `tag`、`keys` |
| `model` | 一个模型 | `model`、`display_name` |
| `day` / `week` | 区间内的每一天或每一周，没有调用也有一行 | `date`、`start`、`end`、`partial` |

每行都有 `requests`、`total_tokens`、`cost_usd`；成员、key、标签三种分组的行另有缓存 token 和缓存费用字段。

> [!NOTE]
>
> `totals` 包含组织所有的 key，含已删除的 key；而 `key`、`tag` 分组只列出还存在的 key，并且一把 key 有多个标签时会在每个标签下各算一次，所以各行加起来不一定等于 `totals`。

## 调用明细

**GET /org/requests** `endpoint`

组织的调用记录，按时间倒序，游标分页。仅限组织管理员的 key。

| 参数 | 说明 |
| - | - |
| `start`、`end` | 时间范围（见上） |
| `member`、`api_key_id`、`tag`、`model` | 筛选，同 `/org/usage` |
| `status` | `success`（HTTP \< 400）、`error`（HTTP ≥ 400），或具体状态码，例如 `429` |
| `limit` | 每页条数，1–100，默认 50 |
| `cursor` | 上一页返回的 `next_cursor` |

```bash theme={null}
curl "https://router-link.world3.ai/api/v1/org/requests?api_key_id=<KEY_ID>&status=error&limit=100" \
  -H "Authorization: Bearer <ROUTERLINK_API_KEY>"
```

```json theme={null}
{
  "org_id": "3c3f…",
  "start": "2026-11-23T10:00:00+00:00",
  "end": "2026-11-24T10:00:00+00:00",
  "items": [
    {
      "request_id": "d4339886-78b6-…",
      "created_at": "2026-11-24T01:02:03",
      "model": "Claude Sonnet 4.5",
      "endpoint": "/v1/chat/completions",
      "status_code": 200,
      "duration_ms": 812,
      "input_tokens": 1000, "output_tokens": 200, "total_tokens": 1200,
      "cache_read_tokens": 0, "cache_write_tokens": 0,
      "cost_usd": 0.0042, "cost_credits": 4.2,
      "cost_breakdown": { "…": "见下文" },
      "api_key_id": "7f0c…", "api_key_name": "prod",
      "user_id": "u_…", "user_email": "a@example.com"
    }
  ],
  "next_cursor": "MjAyNi0xMS0yNFQwMTowMjowM3w0Mg"
}
```

> [!WARNING]
>
> **翻页**：之后的每一页都要带上相同的筛选条件，**以及第一页返回的 `start` / `end`**，再加 `cursor`。不传 `start` / `end` 时，默认窗口会随当前时间后移，翻页时间一长，最后会返回 `400 invalid_cursor` 或漏掉部分调用。`next_cursor` 为 `null` 表示已经是最后一页。

* 调用明细里的 `created_at` 是**不带时区后缀的 UTC 时间**（例如 `2026-11-24T01:02:03`），不要按本地时间解析。
* 明细包含已删除 key 的调用和已离开成员的调用；已删除 key 的 `api_key_name` 为 `null`。

### 费用拆分

`cost_breakdown` 把一次调用的费用按计费项拆开。开始记录拆分之前计费的调用，以及少数受限渠道的调用，这一项为 `null`。

```json theme={null}
{
  "v": 1,
  "items": {
    "input":       { "unit": "token", "quantity": 21,   "unit_price_usd": "8.0000000000",  "price_source": "org", "list_credits": "0.1680000000",  "cost_credits": "0.1680000000" },
    "output":      { "unit": "token", "quantity": 97,   "unit_price_usd": "40.0000000000", "price_source": "org", "list_credits": "3.8800000000",  "cost_credits": "3.8800000000" },
    "cache_read":  { "unit": "token", "quantity": 5054, "unit_price_usd": "0.2000000000",  "price_source": "org", "list_credits": "1.0108000000",  "cost_credits": "1.0108000000" },
    "cache_write": { "unit": "token", "quantity": 4010, "unit_price_usd": "10.0000000000", "price_source": "org", "list_credits": "40.1000000000", "cost_credits": "40.1000000000" }
  },
  "totals": { "list_credits": "45.1588000000", "discount_credits": "0.0000000000" },
  "time_window": null,
  "credits_per_usd": "1000.00"
}
```

| 字段 | 含义 |
| - | - |
| `items` | 每个计费项一条（`input`、`output`、`cache_read`、`cache_write`、联网搜索次数、视频秒数等） |
| `unit` | `token`、`request` 或 `second` |
| `quantity` | 计费数量 |
| `unit_price_usd` | 折扣前单价：token 类按**每百万 token**，其余按每次或每秒 |
| `price_source` | `list`（公开价）或 `org`（组织专属价） |
| `list_credits` / `cost_credits` | 时段折扣前 / 后的积分 |
| `totals.discount_credits` | 这次调用的折扣合计 |
| `time_window` | 命中的闲时时段（`id`、`multiplier`），没有命中为 `null` |
| `credits_per_usd` | 这次调用使用的积分与美元换算比例 |

各项 `cost_credits` 相加恰好等于该行的 `cost_credits`。金额是十进制**字符串**，以保留完整精度。

## 导出 CSV

**GET /org/requests/export** `endpoint`

和控制台组织调用记录页「导出」完全相同的 CSV（每个计费项一行）。仅限组织管理员的 key。

| 参数 | 说明 |
| - | - |
| `start`、`end` | 北京时间日期 `YYYY-MM-DD`，`end` 包含当天 |
| `member`、`api_key_id`、`tag` | 筛选 |

```bash theme={null}
curl -o usage.csv "https://router-link.world3.ai/api/v1/org/requests/export?start=2026-11-01&end=2026-11-23" \
  -H "Authorization: Bearer <ROUTERLINK_API_KEY>"
```

## 金额说明

* `cost_usd` 由积分换算，1,000 积分 = 1 美元，和控制台一致。
* 调用明细行上的 `cost_usd` 是控制台显示的价格估算；如果和积分有出入，以积分为准。

## 错误

key 校验失败时的错误格式和调用模型时相同；其他错误返回 `{"detail": {"code": "…", "message": "…"}}`。

| 状态码 | `code` | 含义 |
| - | - | - |
| 400 | `invalid_range` | `start` / `end` 不是 ISO 8601、`start` ≥ `end`，或跨度超过 31 天；导出日期不合法 |
| 400 | `invalid_group_by` | 不支持的 `group_by` |
| 400 | `invalid_filter` | `status` 不合法、`tag` 为空，或 `group_by=member` 时用了不支持的筛选 |
| 400 | `invalid_cursor` | 游标不是本接口返回的，或不在 `[start, end)` 内 |
| 401 | `api_key_expired` | key 已过期 |
| 403 | `usage_api_not_enabled` | 该组织未开通用量查询 API |
| 403 | `org_key_required` | 用的是个人 key，请改用组织 key |
| 403 | `org_admin_required` | key 的所有者不是该组织的管理员 |
| 403 | `ip_not_allowed` | 请求不在 key 的 IP 白名单内 |
| 404 | `not_found` | `api_key_id` 或 `member` 不属于该组织 |
| 429 | `rate_limited` | 请求过于频繁，按 `Retry-After` 等待后重试 |
| 503 | `usage_timeout` / `busy` | 查询超时或该组织的查询在排队，按 `Retry-After` 重试 |

## 限制

* 每个接口每个用户、每个组织每分钟最多 **20 次**（和控制台用量页面共用）。
* 传了 `end` 时，`/org/usage` 的结果缓存 45 秒；调用明细不缓存。
* 组织规模较大时，建议缩短时间范围；批量下载用 CSV 导出。按 `status` 筛选需要读取更多数据，会更慢。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.