ToAPIsToAPIs
API Key

查詢 API Key 餘額

讀取自有模型 Key 的持久化額度快照

GET
/api/v1/api-keys/{api_key_id}/balance

中國大陸用戶請注意: 中國大陸用戶請使用 https://api.toapis.cn 作為接口地址 (Base URL). 文檔示例中的 https://api.toapis.com 請替換為 https://api.toapis.cn.

使用說明

讀取指定 Key 的剩餘額度快照, 用於查看它還能消耗多少. 停用, 過期, 耗盡和已軟刪除的 Key 都可以讀取; 屬於他人或不存在的 Key 回傳 404.

回傳的是資料庫中已儲存的餘額快照, 不是即時計算的數值: 無限額度 Key 的剩餘金額為 null, 已用金額正常回傳. 日/月限額的即時已用計數不在本介面讀取範圍; 呼叫扣費按批次寫入資料庫, 批次尚未寫入時, 讀到的數值會略滯後於實際用量.

驗證

Authorizationstring請求頭必填

系統存取權杖, 格式為 Bearer YOUR_SYSTEM_ACCESS_TOKEN. 不接受模型 Key, 不需要 X-User-Id. 共用規則見接入方式.

請求參數

api_key_idstring路徑必填

自有一般模型 Key 的十進位 ID 字串, 不是 sk- 密鑰.

不接受主體或查詢參數.

請求範例

請求示例

curl --request GET \
  --url 'https://api.toapis.com/api/v1/api-keys/43/balance' \
  --header 'Authorization: Bearer YOUR_SYSTEM_ACCESS_TOKEN'
import requests

response = requests.get(
    "https://api.toapis.com/api/v1/api-keys/43/balance",
    headers={"Authorization": "Bearer YOUR_SYSTEM_ACCESS_TOKEN"},
    timeout=30,
)
response.raise_for_status()
print(response.json())
const response = await fetch("https://api.toapis.com/api/v1/api-keys/43/balance", {
  method: "GET",
  headers: { Authorization: "Bearer YOUR_SYSTEM_ACCESS_TOKEN" },
  signal: AbortSignal.timeout(30_000),
});
if (!response.ok) {
  throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
console.log(await response.json());

回應欄位

範例使用虛構資料. Key 中繼資料見取得 API Key 詳情, 共用回應與錯誤處理見接入方式.

data.api_key_idstring

Key ID.

data.statusstring

目前 Key 狀態, 包括 deleted.

data.unlimitedboolean

是否為無限額度.

data.remaining_creditsstring | null

剩餘 credits, 無限時為 null.

data.remaining_usdstring | null

剩餘 USD 等值, 無限時為 null.

data.used_creditsstring

累計已用 credits.

data.used_usdstring

累計已用 USD 等值.

data.daily_limit_creditsstring | null

每日 credits 上限, 無限制為 null.

data.monthly_limit_creditsstring | null

每月 credits 上限, 無限制為 null.

data.credits_per_usdstring

每 1 USD 對應的 credits.

回應範例

響應示例

{
  "code": 0,
  "message": "",
  "data": {
    "api_key_id": "43",
    "status": "enabled",
    "unlimited": false,
    "remaining_credits": "500",
    "remaining_usd": "5",
    "used_credits": "100",
    "used_usd": "1",
    "daily_limit_credits": null,
    "monthly_limit_credits": null,
    "credits_per_usd": "100"
  }
}