# 查询额度

> 你的 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)页。
