余额查询接口
余额查询接口用于获取账户及 API Key 的可用余额,支持企业运维监控、单 Key 排查和个人开发者自助查询。Base URL 为 https://tokenplus.net。
余额接口共 3 个,按调用凭据区分权限范围:
| 接口 | 调用凭据 | 能力 | 速率限制 |
|---|---|---|---|
GET /v1/balance | Management Key | 查询账户余额和全部 Regular Key 余额 | 60 次/分钟 |
GET /v1/keys/{key_id}/balance | Management Key | 查询同账户指定 Regular Key 的余额详情 | 60 次/分钟 |
GET /v1/me/balance | Regular Key | 查询所属账户余额和当前 Key 的余额信息 | 120 次/分钟 |
权限边界:
- Management Key 只能调用前两个接口,不可调用
/v1/me/balance。 - Regular Key 只能调用
/v1/me/balance,不可调用 Management 余额接口。 /v1/balance只返回 Regular Key,不支持查询或枚举 Management Key。- 指定 Key 不存在、已删除或属于其他账户时,统一返回
404 key_not_found。
通用请求约定
| 项目 | 约定 |
|---|---|
| Base URL | https://tokenplus.net |
| 请求方法 | GET |
| 认证请求头 | Authorization: Bearer {api-key} |
| 响 应类型 | Content-Type: application/json |
| 时间格式 | RFC 3339 UTC,例如 2026-08-06T08:00:00Z |
金额与空值语义
- 所有金额单位均为 USD,并在响应顶层返回
currency: "USD"。 - 所有金额和百分比均为十进制字符串,例如
"850"、"85.5"、"42.75"。 null表示该字段不适用、无限制或当前无法提供可信事实,具体含义以字段说明为准。- 调用方不得将
null自动转换为0。
快照与缓存
- 每个成功响应包含
as_of,表示本次已提交数据快照的时间。 - 查询与消费并发时,不保证响应后余额不会继续变化。
- 所有成功和错误响应必须包含
Cache-Control: private, no-store。
字段定义
- 顶层与账户
- Key 字段
- 消费明细
| 字段 | 类型 | 可能为 null | 说明 |
|---|---|---|---|
object | string | 否 | balance、key_balance 或 self_balance |
currency | string | 否 | 固定为 USD |
as_of | string | 否 | 响应数据快照时间,RFC 3339 UTC |
account.total_balance | decimal string | 否 | 账户当前在线可消费钱包余额,负值对外按 "0" 展示 |
account.total_consumed | decimal string | 否 | 账户累计消费,仅 Management 列表接口返回 |
account.paid_balance | null | 是 | 本期不可用,固定返回 null |
account.free_balance | null | 是 | 本期不可用,固定返回 null |
account.paid_consumed | null | 是 | 本期不可用,仅 Management 列表接口出现 |
account.free_consumed | null | 是 | 本期不可用,仅 Management 列表接口出现 |
account.breakdown_status | string | 否 | 本期固定为 unavailable |
total_balance 是在线消费余额,不等同于可退款余额。客服或财务不得仅根据本接口计算退款金额。
| 字段 | 类型 | 可能为 null | 说明 |
|---|---|---|---|
id | string | 否 | Regular Key 公开 ID,不是数据库内部 ID |
name | string | 否 | Key 名称 |
key_type | string | 否 | 固定为 regular |
disabled | boolean | 否 | 当前投影状态是否不可用于推理 |
manually_disabled | boolean | 否 | 用户是否手工禁用,用于保留用户意图 |
limit | decimal string | 是 | Key 额度上限;无限 Key 为 null |
limit_reset | string | 否 | daily、weekly、monthly 或 never |
quota_scope | string | 是 | usage、subscription、all 或 null,表示额度约束适用的资金来源 |
period | object | 是 | 当前滚动周期;never 或无周期时为 null |
consumed_this_period | decimal string | 是 | 当前周期消费;数据无法可信确定时为 null |
consumed_today | decimal string | 是 | 今日消费;日志不完整时为 null |
key_limit_remaining | decimal string | 是 | Key 独立额度剩余;无限 Key 为 null |
remaining | decimal string | 是 | key_limit_remaining 的兼容字段,语义和值保持一致 |
effective_remaining | decimal string | 是 | 综合账户钱包和 Key 上限后,当前实际可用额度 |
usage_pct | decimal string | 是 | Key 额度使用百分比,范围 0 到 100;无限 Key 为 null |
status | string | 否 | active 或 disabled |
status_reason | string | 是 | active 时为 null;disabled 时返回具体原因 |
status_reason 可能值及优先级:
user_disabled:账户被禁用;pending_subscription:订阅 Key 等待订阅生效;expired:Key 已过期;limit_exhausted:Key 额度已耗尽;manual:Key 被用户手工禁用;- active 状态不返回
active字符串,对外为null。
consumed 仅在单 Key Management 查询中返回。
| 字段 | 类型 | 可能为 null | 说明 |
|---|---|---|---|
consumed.all_time | decimal string | 否 | Key 历史累计消费 |
consumed.this_period | decimal string | 是 | 当前周期消费 |
consumed.today | decimal string | 是 | 今日消费 |
consumed.by_model | array | 是 | 当前可验证范围内按模型拆分的消费 |
consumed.complete | boolean | 否 | 消费日志是否完整覆盖本次结果 |
consumed.retention_start | string | 是 | 当前可用日志的最早留存时间 |
当日志不可用、留存不足或查询快照发生变化时:
today和by_model返回null;complete=false;- 不得返回
0或空数组来暗示"确认没有消费"。
余额与周期计算规则
账户余额
total_balance只读取账户在线权威余额。- 账户余额为负时,对外按
"0"展示并记录异常,不返回负的可用余额。
Key 余额
| Key 情况 | key_limit_remaining | effective_remaining |
|---|---|---|
| 有独立上限 | Key 剩余额度,最低为 "0" | 钱包余额和 Key 剩余额度中实际能使用的额度 |
| 无限 Key | null | 受账户余额或对应资金来源约束 |
| 已禁用、过期或耗尽 | 仍按额度状态返回 | 仍返回查询时的实际额度投影,查询接口本身为 200 |
周期
daily、weekly、monthly使用 Key 保存时刻形成滚动周期,不按自然日、自然周或自然月切分。- 周期统一表达为
[start, end_exclusive)。 - 不提供
period.end和period.days_remaining,避免边界歧义和客户端时区差异。 limit_reset=never或无周期策略时,period=null。
错误响应
- 错误格式
- 主要错误码
固定格式:
{
"error": {
"code": "key_not_found",
"message": "API key not found.",
"param": "key_id",
"request_id": "req_..."
}
}
客户端必须按 HTTP 状态码和 error.code 分支,不得匹配 message 文本。
| HTTP | 错误码 | 适用场景 |
|---|---|---|
| 400 | invalid_parameter | 页码、分页大小、状态或其他参数不合法 |
| 400 | unsupported_key_type | /v1/balance 请求 key_type=management |
| 401 | invalid_management_key | Management 端点缺少、伪造、无效、禁用或已删除的 Management Key |
| 401 | invalid_api_key | 自助端点缺少、伪造、无效或已删除的 Regular Key |
| 403 | forbidden_scope | 有效 Regular Key 调 Management 端点,或 Management Key 调自助端点 |
| 403 | account_disabled | 自助查询所属账户已被禁用 |
| 404 | key_not_found | 指定 Key 不存在、已删除或不属于当前账户 |
| 422 | usage_query_too_large | 用量按模型聚合结果超过系统保护范围 |
| 429 | rate_limit_exceeded | 余额查询接口自身触发限流 |
| 500 | internal_error | 服务暂时无法完成查询;包括启用 Redis 限流但 Redis 不可用 |
| 504 | usage_query_timeout | 用量查询超时 |
Key 额度耗尽不是余额查询错误,不返回 429。
限流、安全与隐私
- Management 两个端点:按 Management Key、用户和来源 IP 分别限制 60 次/分钟。
- Regular 自助端点:按 Regular Key、用户和来源 IP 分别限制 120 次/分钟。
- 任一维度超限均返回
429 rate_limit_exceeded。
安全与隐私:
- 不返回 Regular Key 明文、Key hash、完整预览或内部自增 ID。
- 不返回或枚举 Management Key。
- 跨账户 Key 与不存在 Key 使用相同 404,防止资源探测。
- 所有成功和错误响应均禁止浏览器或中间代理存储。