查询余额与用量
查询账户额度和使用情况,兼容 OpenAI Billing API 格式。
接口地址
GET https://foxapi.chat/v1/dashboard/billing/subscription
GET https://foxapi.chat/v1/dashboard/billing/usage前置条件
拥有一个 FoxAPI Key(获取教程)
查询账户信息
curl
bash
curl https://foxapi.chat/v1/dashboard/billing/subscription \
-H "Authorization: Bearer sk-你的Key"Python
python
from openai import OpenAI
client = OpenAI(
api_key="sk-你的Key",
base_url="https://foxapi.chat/v1"
)
subscription = client.billing.subscription()
print(subscription)返回示例
json
{
"object": "billing_subscription",
"has_payment_method": true,
"soft_limit_usd": 100000000,
"hard_limit_usd": 100000000,
"system_hard_limit_usd": 100000000,
"access_until": 0
}| 字段 | 说明 |
|---|---|
| has_payment_method | 账户是否有效(true = 正常可用) |
| soft_limit_usd | 软限额(系统默认值,无需关注) |
| hard_limit_usd | 硬限额(系统默认值,无需关注) |
| access_until | 会员到期时间(0 = 无限制) |
查询已用额度
curl
bash
curl "https://foxapi.chat/v1/dashboard/billing/usage?start_date=2026-06-01&end_date=2026-06-07" \
-H "Authorization: Bearer sk-你的Key"Python
python
from openai import OpenAI
from datetime import datetime, timedelta
client = OpenAI(
api_key="sk-你的Key",
base_url="https://foxapi.chat/v1"
)
# 查询最近 7 天用量
end = datetime.now()
start = end - timedelta(days=7)
usage = client.billing.usage(
start_date=start.strftime("%Y-%m-%d"),
end_date=end.strftime("%Y-%m-%d")
)
print(f"已用额度: ${usage.total_usage}")请求参数
| 参数 | 类型 | 必填 | 格式 | 说明 |
|---|---|---|---|---|
| start_date | string | ✅ | YYYY-MM-DD | 查询起始日期 |
| end_date | string | ✅ | YYYY-MM-DD | 查询结束日期 |
返回示例
json
{
"object": "list",
"total_usage": 0.199
}| 字段 | 说明 |
|---|---|
| total_usage | 该时间段内的总消耗(美元) |
计算剩余可用额度
接口本身不直接返回剩余可用额度,需要客户端自行计算:
剩余可用 = 充值总额 - total_usage💡 查看充值总额
登录 FoxAPI 控制台 → 个人中心 → 余额
Python 完整示例
python
from openai import OpenAI
from datetime import datetime, timedelta
client = OpenAI(
api_key="sk-你的Key",
base_url="https://foxapi.chat/v1"
)
# 查询本月用量
now = datetime.now()
start = now.replace(day=1)
usage = client.billing.usage(
start_date=start.strftime("%Y-%m-%d"),
end_date=now.strftime("%Y-%m-%d")
)
print(f"本月已用: ${usage.total_usage:.4f}")
# 剩余可用 = 你的充值总额 - usage.total_usage常见问题
返回 401 / Invalid token
Key 不正确或已失效,到控制台检查 Key 状态。
total_usage 返回 0
- 检查日期范围是否正确
- 确认 Key 在该时间段内有使用记录
- 日期格式必须是
YYYY-MM-DD
和 OpenAI 官方接口的区别
soft_limit_usd/hard_limit_usd返回的是系统默认值,不是你的实际额度total_usage是所有模型的总消耗,不按模型分类- 如需详细用量明细,请登录 FoxAPI 控制台 查看