用量统计
查询当前账号的 API 调用量、配额使用情况和各项服务的用量明细。
快速概览
用量统计 API 提供两个核心端点:/v1/usage 用于查询账号级别的汇总用量与配额,/v1/usage/details 用于按服务维度查看每日/每小时的消耗明细。支持自定义日期范围、按服务过滤、以及按天/小时粒度的统计分析。可通过配置 配额告警 Webhook 在用量达到阈值时自动推送通知。
认证方式
Authentication
所有 API 请求均需要在 HTTP 请求头中携带有效的 API Token 进行身份认证。请在 控制台 获取您的 Access Token。
Authorization: Bearer {access_token}请求头
Request Headers
| Header | 类型 | 必填 | 说明 |
|---|---|---|---|
Authorization | string | required | Bearer Token 认证信息 |
服务名称参考
Service Name Reference
| 服务标识 | 服务名称 | 计费单位 |
|---|---|---|
text_translation | 文本翻译 | 字符数/请求数 |
document_translation | 文档翻译 | 字符数/文件数 |
image_translation | 图片翻译 | 图片张数 |
speech_translation | 语音翻译 | 秒数/请求数 |
speech_recognition | 语音识别 | 秒数/请求数 |
video_translation | 视频翻译 | 秒数/请求数 |
tts | 语音合成 | 字符数/请求数 |
subtitle | 字幕翻译 | 字符数 |
website_translation | 网站翻译 | 字符数/页面数 |
code_translation | 代码翻译 | 字符数 |
ai_polishing | 语言润色 | 字符数/请求数 |
grammar_check | 语法纠错 | 字符数/请求数 |
language_detect | 语言识别 | 请求数 |
总览
Overview / Summary
GET/v1/usage
查询账号在当前周期内的汇总用量、配额状态及按服务分类的用量明细。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| start_date | string | optional | 起始日期 (YYYY-MM-DD),默认本月 1 日 |
| end_date | string | optional | 结束日期 (YYYY-MM-DD),默认今天 |
| service | string | optional | 按服务过滤,如 text_translation;不传则返回所有服务 |
响应字段
Response Fields
| 字段 | 类型 | 说明 |
|---|---|---|
| code | integer | 状态码,0 表示成功 |
| message | string | 操作结果描述 |
| data.total_characters | integer | 周期内累计消耗的总字符数 |
| data.total_seconds | integer | 周期内语音/视频类服务累计消耗的秒数 |
| data.total_images | integer | 周期内图片翻译累计消耗的图片数 |
| data.total_requests | integer | 周期内所有 API 累计请求次数 |
| data.quota_total | integer | 账号套餐的字符配额总数 |
| data.quota_used | integer | 已使用的字符配额 |
| data.quota_used_percent | float | 配额使用百分比,范围 0~100 |
| data.quota_status | string | 配额状态:normal / warning / exceeded |
| data.breakdown | array | 按服务拆分的用量明细列表 |
| data.breakdown[].service | string | 服务标识,如 text_translation |
| data.breakdown[].characters | integer | 该服务消耗的字符数(文本类服务) |
| data.breakdown[].seconds | integer | 该服务消耗的秒数(语音/视频类服务) |
| data.breakdown[].images | integer | 该服务消耗的图片数(图片翻译) |
| data.breakdown[].requests | integer | 该服务的请求次数 |
| data.period | object | 统计周期,包含 start 和 end 日期 |
响应示例
Response Examples
示例 1:正常用量(配额充足)
Example 1: Normal Usage
{
"code": 0,
"message": "success",
"data": {
"total_characters": 1250000,
"total_seconds": 18000,
"total_images": 500,
"total_requests": 3865,
"quota_total": 5000000,
"quota_used": 1250000,
"quota_used_percent": 25.0,
"quota_status": "normal",
"breakdown": [
{ "service": "text_translation", "characters": 800000, "requests": 3200 },
{ "service": "document_translation", "characters": 350000, "requests": 45 },
{ "service": "speech_translation", "seconds": 18000, "requests": 120 },
{ "service": "image_translation", "images": 500, "requests": 500 }
],
"period": { "start": "2026-07-01", "end": "2026-07-20" }
}
}示例 2:配额预警(即将用尽)
Example 2: Quota Warning
{
"code": 0,
"message": "success",
"data": {
"total_characters": 4500000,
"total_requests": 15800,
"quota_total": 5000000,
"quota_used": 4500000,
"quota_used_percent": 90.0,
"quota_status": "warning",
"quota_reset_at": "2026-08-01T00:00:00Z",
"breakdown": [
{ "service": "text_translation", "characters": 3200000, "requests": 12500 },
{ "service": "document_translation", "characters": 800000, "requests": 110 },
{ "service": "ai_polishing", "characters": 500000, "requests": 3190 }
],
"period": { "start": "2026-07-01", "end": "2026-07-22" }
}
}示例 3:配额超限
Example 3: Quota Exceeded
{
"code": 0,
"message": "success",
"data": {
"total_characters": 5120000,
"total_requests": 20100,
"quota_total": 5000000,
"quota_used": 5120000,
"quota_used_percent": 102.4,
"quota_status": "exceeded",
"quota_reset_at": "2026-08-01T00:00:00Z",
"overtraffic_enabled": true,
"overtraffic_used": 120000,
"breakdown": [
{ "service": "text_translation", "characters": 3800000, "requests": 16500 },
{ "service": "document_translation", "characters": 950000, "requests": 150 },
{ "service": "image_translation", "images": 1200, "requests": 1200 },
{ "service": "ai_polishing", "characters": 370000, "requests": 2250 }
],
"period": { "start": "2026-07-01", "end": "2026-07-22" }
}
}示例 4:按指定服务过滤
Example 4: Filtered by Service
{
"code": 0,
"message": "success",
"data": {
"total_characters": 800000,
"total_requests": 3200,
"quota_total": 5000000,
"quota_used": 1250000,
"quota_used_percent": 25.0,
"quota_status": "normal",
"breakdown": [
{ "service": "text_translation", "characters": 800000, "requests": 3200 }
],
"period": { "start": "2026-07-01", "end": "2026-07-20" }
}
}请求示例
Request Examples
基础汇总查询
Basic Summary Query
# 查询本月汇总用量 curl "https://api.itranslator.cc/v1/usage" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" # 查询指定日期范围汇总 curl "https://api.itranslator.cc/v1/usage?start_date=2026-07-01&end_date=2026-07-22" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" # 仅查询文本翻译服务的用量 curl "https://api.itranslator.cc/v1/usage?service=text_translation" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
按服务查询明细
Per-Service Details
GET/v1/usage/details
查询指定服务在时间范围内的逐日或逐时用量消耗明细,适合用于生成报表或分析用量高峰。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| service | string | required | 服务名称,如 text_translation、ai_polishing |
| start_date | string | required | 起始日期 (YYYY-MM-DD) |
| end_date | string | required | 结束日期 (YYYY-MM-DD) |
| granularity | string | optional | daily(默认)/ hourly;hourly 仅在 7 天内可用 |
明细响应字段
Details Response Fields
| 字段 | 类型 | 说明 |
|---|---|---|
| code | integer | 状态码,0 表示成功 |
| message | string | 操作结果描述 |
| data.service | string | 查询的服务标识 |
| data.granularity | string | 统计粒度:daily / hourly |
| data.total_characters | integer | 周期内该服务消耗的总字符数 |
| data.total_requests | integer | 周期内该服务的总请求次数 |
| data.datapoints | array | 时间序列数据点列表 |
| data.datapoints[].date | string | 日期 (YYYY-MM-DD) 或时间 (YYYY-MM-DD HH:00) |
| data.datapoints[].characters | integer | 该时间点消耗的字符数 |
| data.datapoints[].requests | integer | 该时间点的请求次数 |
明细响应示例
Details Response Example
按天统计
Daily Breakdown
{
"code": 0,
"message": "success",
"data": {
"service": "text_translation",
"granularity": "daily",
"total_characters": 800000,
"total_requests": 3200,
"datapoints": [
{ "date": "2026-07-01", "characters": 42000, "requests": 168 },
{ "date": "2026-07-02", "characters": 38500, "requests": 152 },
{ "date": "2026-07-03", "characters": 51000, "requests": 205 },
...
],
"period": { "start": "2026-07-01", "end": "2026-07-20" }
}
}按小时统计
Hourly Breakdown
{
"code": 0,
"message": "success",
"data": {
"service": "text_translation",
"granularity": "hourly",
"total_characters": 56000,
"total_requests": 225,
"datapoints": [
{ "date": "2026-07-22 09:00", "characters": 12000, "requests": 48 },
{ "date": "2026-07-22 10:00", "characters": 18500, "requests": 73 },
{ "date": "2026-07-22 11:00", "characters": 15500, "requests": 62 },
{ "date": "2026-07-22 12:00", "characters": 10000, "requests": 42 }
],
"period": { "start": "2026-07-22", "end": "2026-07-22" }
}
}明细查询代码示例
Details Query Code Examples
# 按天查询文本翻译用量 curl "https://api.itranslator.cc/v1/usage/details?service=text_translation&start_date=2026-07-01&end_date=2026-07-20&granularity=daily" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" # 按小时查询语音翻译用量 curl "https://api.itranslator.cc/v1/usage/details?service=speech_translation&start_date=2026-07-22&end_date=2026-07-22&granularity=hourly" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
配额告警 Webhook
Quota Alert Webhook
可在 控制台 中配置 Webhook 地址,当配额使用率达到设定阈值时自动推送告警。当前支持 80%、90%、95%、100% 四个预警级别。
{
"event": "quota_warning",
"account_id": "acc_xxxxx",
"plan": "pro",
"quota_total": 5000000,
"quota_used": 4500000,
"quota_used_percent": 90.0,
"threshold": 90,
"quota_reset_at": "2026-08-01T00:00:00Z",
"top_services": [
{ "service": "text_translation", "characters": 3200000, "percent": 71.1 },
{ "service": "document_translation", "characters": 800000, "percent": 17.8 },
{ "service": "ai_polishing", "characters": 500000, "percent": 11.1 }
],
"timestamp": "2026-07-22T14:30:00Z"
}使用限制
Usage Limits
| 限制项 | 上限 | 说明 |
|---|---|---|
| 日期查询范围 | 365 天 | start_date 到 end_date 最大跨度 |
| 小时粒度范围 | 7 天 | granularity=hourly 时最大查询范围 |
| 单次返回记录数 | 744 | 按小时查询 31 天 × 24 小时 |
| 请求频率 | 30 次/分钟 | 用量统计类接口独立限频 |
配额状态说明
Quota Status Reference
| 状态 | 含义 | API 调用影响 |
|---|---|---|
normal | 正常 | 配额使用率 ≤ 80%,一切正常 |
warning | 预警 | 配额使用率 > 80%,建议升级套餐或预充值 |
exceeded | 超限 | 配额已用尽,取决于是否开启超量使用;未开启时请求返回 403 |
最佳实践
Best Practices
| 建议 | 说明 |
|---|---|
| 定期拉取用量做监控 | 建议每日定时调用 /v1/usage 接口,将数据存入监控系统,提前发现用量异常增长 |
| 配置多重告警阈值 | 依次配置 80%/90%/95% 三档 Webhook 告警,在配额用完前有充足的升级和充值时间 |
| 分服务追踪精细管控 | 通过 /v1/usage/details 对主要服务分别设置监控,快速定位高消耗服务并优化调用逻辑 |
| 利用小时粒度分析高峰 | 在业务高峰期时段使用 granularity=hourly 定位流量峰值,按需启用限流或横向扩容 |
| 缓存用量数据减少 API 调用 | 用量数据有约 5 分钟的更新延迟,客户端可按 5~15 分钟间隔缓存,避免高频轮询 |
| 将用量集成到业务后台 | 在管理后台嵌入用量仪表盘,方便运营和财务团队实时掌握 API 消费成本 |
错误码
Error Codes
| HTTP Code | 错误码 | 说明 |
|---|---|---|
| 200 | 0 | 成功 |
| 400 | 1001 | 参数错误,请检查必填参数和参数格式 |
| 400 | 1020 | 日期格式无效,须为 YYYY-MM-DD 格式 |
| 400 | 1021 | start_date 晚于 end_date |
| 400 | 1022 | 查询日期范围超出最大跨度(365 天) |
| 400 | 1023 | 无效的 granularity,仅支持 daily 和 hourly |
| 400 | 1024 | hourly 粒度仅支持 7 天内的查询 |
| 400 | 1025 | 未知的 service 参数,请查阅服务名称参考表 |
| 401 | 2001 | 认证失败,Token 无效或已过期 |
| 403 | 2003 | 无权限访问该资源,或配额已用尽且未开启超量使用 |
| 404 | 2004 | 资源不存在 |
| 429 | 4001 | 请求频率超限,请稍后重试 |
| 500 | 5001 | 服务器内部错误,请重试或联系技术支持 |
使用说明
- 所有用量统计 API 均需使用 Bearer Token 鉴权,请在 控制台 获取。
- 用量数据有约 5 分钟更新延迟,短时间内的高频轮询将返回相同结果。
- 建议将用量查询与 配额告警 Webhook 结合使用,实现主动推送 + 按需拉取的双重监控。
- 如配额即将耗尽,可访问控制台自助升级套餐或预充值额度。
iTranslator