# 查詢額度

> 你的 API Key 還能用多少額度：/v1/balance 與 OpenAI 舊版的 subscription 端點。

查這把 API Key 現在最多還能用多少額度，以及是哪一項上限決定的。金額是美元的 JSON 數字，最多 6 位小數。`GET /v1/dashboard/billing/subscription` 以 OpenAI 舊版格式回傳同一個數字，給 Cherry Studio 等客戶端使用。

## 方法與路徑

`GET /v1/balance`

`GET /v1/dashboard/billing/subscription`

base URL 為 `https://api.relayvia.dev/v1`，Key 放在 `Authorization: Bearer sk-...`。

## 常用參數

沒有參數，依 header 裡的 Key 決定回傳誰的資料。

## 與原廠不同之處

- 只讀且免費：不扣款、不寫消費日誌，也不佔模型請求的限流；另有每把 Key 每分鐘 60 次的限制（超過回 429 `rate_limited`）。停用或過期的 Key 回 401。`Authorization: Bearer`、`x-api-key`、`x-goog-api-key` 都可以用。
- `remaining_usd` 取 Key 上限、團隊成員上限、會員花費上限與會員額度四者剩餘的最小值，所以可能是 0 或負數；`limited_by` 指出是哪一項：`key`、`member`、`budget` 或 `wallet`。
- 舊版端點的三個 limit 都是 `remaining_usd + key.used_total_usd`，所以客戶端照慣例用 hard limit 減 total usage 就得到 `remaining_usd`。`access_until` 是 Key 到期的 Unix 秒數，永不過期時為 0。

## 請求範例

**cURL**

```bash
curl https://api.relayvia.dev/v1/balance \
  -H "Authorization: Bearer sk-..."
```

**Python**

```python
import requests

resp = requests.get("https://api.relayvia.dev/v1/balance", headers={"Authorization": "Bearer sk-..."})
print(resp.json())
```

**JavaScript**

```javascript
const resp = await fetch("https://api.relayvia.dev/v1/balance", {
  headers: { Authorization: "Bearer sk-..." },
});
console.log(await resp.json());
```

## 回應範例

| 欄位 | 型別 | 說明 |
| --- | --- | --- |
| `remaining_usd` | `number` | 這把 Key 現在最多還能用的美元：下列各項剩餘的最小值，可能是 0 或負數。 |
| `limited_by` | `string` | 決定 `remaining_usd` 的是哪一項：`key`、`member`、`budget` 或 `wallet`。 |
| `key` | `object` | Key 的額度上限：`unlimited`、`period`（`none`、`daily`、`weekly` 或 `monthly`）、`limit_usd`、`used_usd`、`remaining_usd`、`resets_at`，另有累計用量 `used_total_usd` 與 `expires_at`。不限額時上限相關欄位為 null。 |
| `member` | `object` | Key 所屬團隊成員的額度上限，欄位同上；`period` 是 `total` 或 `monthly`。 |
| `budget` | `object \| null` | 會員花費上限中最緊的一項；沒有設定時為 null。 |
| `company.available_usd` | `number` | 會員目前可用的額度，可能是負數；控制台頁首顯示的是同一個數字。 |

**回應**

```json
{
  "object": "balance",
  "remaining_usd": 42.5,
  "limited_by": "key",
  "key": {
    "unlimited": false,
    "period": "monthly",
    "limit_usd": 50,
    "used_usd": 7.5,
    "remaining_usd": 42.5,
    "resets_at": "2026-12-01T00:00:00Z",
    "used_total_usd": 31.2,
    "expires_at": null
  },
  "member": {
    "unlimited": true,
    "period": "total",
    "limit_usd": null,
    "used_usd": 31.2,
    "remaining_usd": null,
    "resets_at": null
  },
  "budget": null,
  "company": {
    "available_usd": 812.345678
  }
}
```

**OpenAI 舊版格式**

```json
{
  "object": "billing_subscription",
  "has_payment_method": true,
  "soft_limit_usd": 73.7,
  "hard_limit_usd": 73.7,
  "system_hard_limit_usd": 73.7,
  "access_until": 0
}
```

## 這個端點會出現的錯誤

| HTTP | code | 情境 |
| --- | --- | --- |
| `401` | `invalid_api_key` | API Key 不存在、已停用或已過期。 |
| `403` | `forbidden` | 這把 Key 不能用該模型、來源 IP 不在白名單，或會員已停用。 |
| `429` | `rate_limited` | 請求太頻繁：每 IP、每把 Key、每個會員，或同時進行中的請求太多。 |

每個錯誤碼的固定訊息與處理方式見[錯誤碼](https://relayvia.dev/docs/errors)頁。
