按插件用量汇总 API
GET /my/billing/usage-by-app:按 OAuth 插件汇总的模型用量(净消耗、调用次数、最近使用时间与事件级明细),服务于 Driver 桌面壳「按插件用量」卡片与网页端积分页「按插件」视图(issue #1966)。
源码:apps/backend/src/routes/my-billing.ts、apps/backend/src/services/billing-usage-by-app.ts、RPC read_billing_usage_by_app
认证
仅两种身份可访问,其余(插件 token、API Key 等)一律 403 forbidden:
- JWT 用户会话(网页端):默认统计本人默认个人团队;可传
teamId指定本人为accepted成员的团队,非本人团队返回404 team_not_found。 - 官方 Host 委托 OAuth token(Driver 桌面壳,scope
oauth:delegate):强制统计 token 用户的默认个人团队,客户端白名单(OAUTH_DELEGATION_HOST_CLIENT_IDS)fail-closed。
响应设置 Cache-Control: no-store。
请求
GET /my/billing/usage-by-app?from=<RFC3339>&to=<RFC3339>&appId=<uuid>&cursor=<cursor>&limit=50&teamId=<uuid>| 参数 | 必填 | 说明 |
|---|---|---|
from / to | 否 | RFC3339 时间窗,左闭右开;缺省为最近 30 天(to=now、from=to-30d) |
appId | 否 | 只统计指定插件(uuid 小写) |
cursor | 否 | 下一页游标(响应 nextCursor 原样回传;base64url) |
limit | 否 | 明细每页条数,1-100,默认 50 |
teamId | 否 | 仅 JWT 可用:指定统计团队(须为本人 accepted 成员) |
窗口超过 366 天返回 422 usage_range_too_large;参数非法返回 400 invalid_query。
响应
json
{
"success": true,
"data": {
"teamId": "…",
"from": "2026-09-24T00:00:00Z",
"to": "2026-10-24T00:00:00Z",
"apps": [
{
"appId": "…", "appName": "示例插件", "appMode": "development",
"chargedMilliCredits": 4, "refundedMilliCredits": 0,
"netMilliCredits": 4, "callCount": 1, "lastUsedAt": "2026-10-23T12:00:00+00:00"
}
],
"totals": { "chargedMilliCredits": 4, "refundedMilliCredits": 0, "netMilliCredits": 4, "callCount": 1 },
"items": [
{
"transactionId": "…", "appId": "…", "createdAt": "2026-10-23T12:00:00+00:00",
"transactionType": "model_usage", "sourceType": "billing_usage_event",
"chargedMilliCredits": 4, "refundedMilliCredits": 0,
"modelId": "glm-4.7", "executionPath": "model-gateway:/v1/chat/completions",
"eventId": "…"
}
],
"nextCursor": null
}
}- 金额单位毫积分(1 credit = 1000 milli-credit);
netMilliCredits = charged - refunded,可为负。 apps按oauthAppId聚合一行;appMode:development(窗口内全部为开发态 token 调用)/mixed/public。appMode、eventId为网页端扩展键,Driver 客户端忽略。totals与apps同口径(受appId筛选影响);items为事件级明细,createdAt降序游标分页。
错误
| 状态码 | code | 场景 |
|---|---|---|
| 400 | invalid_query | 时间/UUID/limit/cursor 非法,或含未知查询参数 |
| 403 | forbidden | 插件 token、API Key 等非 JWT/OAuth 身份;OAuth 非官方 Host 白名单 |
| 404 | team_not_found | 无默认个人团队,或 JWT 指定的 teamId 非本人成员 |
| 422 | usage_range_too_large | 时间窗超过 366 天 |
| 503 | unavailable | 数据面暂时不可用 |
错误体统一为 { "error": { "code": "…" } }。
相关
- 积分明细(来源插件列/模型/token 展示)与结算字段补记见
docs/features/billing/usage-attribution.md。