大模型Token预估账单查询
Base URL: https://api.qnaigc.com
概述
查询apikey的当日、本周、本月预估费用(原价费用,单位人民币)
认证方式
支持两种鉴权方式:
-
AK/SK 签名认证:使用七牛云标准的 AK/SK 签名认证
Authorization: Qiniu <AccessKey>:<EncodedSign> -
APIKey 认证:使用 APIKey 进行认证
Authorization: Bearer <APIKey>
接口详情
GET /v2/stat/usage/apikey/cost
查询参数: type 可选值:day,week,month。分别为当天,本周,本月
请求示例:
curl --location "https://api.qnaigc.com/v2/stat/usage/apikey/cost?type=month" \
--header "Authorization: Bearer sk-xx"
响应示例(200 OK)
{"status":true,"data":{"api_keys":[{"api_key":"sk-7c***fbe19","models":[{"model_id":"deepseek-v3","items":[{"name":"deepseek-v3输入","usage":{"count":100.00,"unit":"k/tokens"},"fee":1},{"name":"deepseek-v3输出","usage":{"count":100.00,"unit":"k/tokens"},"fee":1}],"total_fee":2}],"total_fee":2}]}}
AK/SK 签名算法
概述
七牛云 API 使用 HMAC-SHA1 签名算法进行身份验证。签名结果附加在请求头的 Authorization 字段中,格式为:
Authorization: Qiniu <AccessKey>:<EncodedSign>
签名生成步骤
1. 生成待签名字符串
待签名字符串由以下部分组成(每部分用换行符分隔):
<Method> <Path>[?Query]
Host: <Host>
Content-Type: <Content-Type>
[X-Qiniu-<HeaderKey>: <HeaderValue>]
<Body>
注意:
Method为 HTTP 方法(如 GET、POST),大写Path为请求路径,不包含域名和查询参数Query为查询参数(不包含开头的?),仅在有时才添加Host为主机名(如api.qnaigc.com)Content-Type为请求内容类型(如application/json)X-Qiniu-*为可选的自定义头,按 key 的 ASCII 顺序排列,格式化为X-Qiniu-<Key>(每个单词首字母大写)- 最后有两个连续换行符
\n\n Body为请求体(仅当 Content-Type 不是application/octet-stream时才添加)
2. 计算 HMAC-SHA1 签名
使用 SecretKey 作为密钥,对待签名字符串进行 HMAC-SHA1 运算:
const hmac = crypto.createHmac('sha1', secretKey);
hmac.update(signingStr);
const sign = hmac.digest();
3. Base64 编码
对签名结果进行 URL 安全的 Base64 编码(+ 替换为 -,/ 替换为 _):
function urlSafeBase64Encode(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}
const encodedSign = urlSafeBase64Encode(sign);
4. 生成最终凭证
将 AccessKey 和 encodedSign 用冒号连接:
<AccessKey>:<EncodedSign>
完整示例代码
const crypto = require('crypto')
/**
* URL 安全的 Base64 编码
*/
function urlSafeBase64Encode(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
}
/**
* 生成待签名的原始字符串
*/
function generateSigningString(options) {
const { method, path, query, host, contentType, headers, body } = options
let signingStr = method.toUpperCase()
signingStr += ' ' + path
if (query) {
signingStr += '?' + query
}
signingStr += '\nHost: ' + host
if (contentType) {
signingStr += '\nContent-Type: ' + contentType
}
if (headers) {
const sortedKeys = Object.keys(headers).sort()
sortedKeys.forEach(key => {
if (key.toLowerCase().startsWith('x-qiniu-')) {
const formattedKey = key.split('-')
.map((part, index) => {
return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()
})
.join('-')
signingStr += '\n' + formattedKey + ': ' + headers[key]
}
})
}
signingStr += '\n\n'
if (body && contentType && contentType !== 'application/octet-stream') {
signingStr += body
}
return signingStr
}
/**
* 生成七牛云管理凭证(Access Token)
*/
function generateAccessToken(accessKey, secretKey, requestOptions) {
const signingStr = generateSigningString(requestOptions)
const hmac = crypto.createHmac('sha1', secretKey)
hmac.update(signingStr)
const sign = hmac.digest()
const encodedSign = urlSafeBase64Encode(sign)
const accessToken = accessKey + ':' + encodedSign
return accessToken
}
// 示例:调用用量查询接口
const accessKey = '你的 AccessKey'
const secretKey = '你的 SecretKey'
const requestOptions = {
method: 'GET',
path: '/v2/stat/usage/apikey/cost',
query: 'type=month',
host: 'api.qnaigc.com'
}
const accessToken = generateAccessToken(accessKey, secretKey, requestOptions)
console.log('Access Token:', accessToken)
// 实际请求
fetch('https://api.qnaigc.com/v2/stat/usage/apikey/cost?type=month', {
headers: {
'Authorization': 'Qiniu ' + accessToken
}
}).then(res => res.json()).then(console.log)
文档反馈
(如有产品使用问题,请 提交工单)