# 查询余额与用量

> 用 API 密钥本身查询这个密钥的总额度、已用额度、过期时间和调用记录，适合在程序或监控里使用。

下面 4 个接口都用 API 密钥本身鉴权，查询的是**这个密钥**的情况。密钥只能放在 `Authorization: Bearer` 请求头里，放在 URL 参数里会返回 401。

| 接口 | 返回 |
|---|---|
| `GET /v1/dashboard/billing/subscription` | 密钥的总额度（美元）和过期时间 |
| `GET /v1/dashboard/billing/usage` | 密钥已经用掉的金额（美分） |
| `GET /api/usage/token` | 密钥的名称、总额度、已用、剩余（额度单位）、过期时间、模型限制 |
| `GET /api/log/token` | 密钥最近的调用记录 |

完整地址是 `https://api.goodapi.tech` 加上表里的路径。

## 总额度和过期时间

```bash
curl https://api.goodapi.tech/v1/dashboard/billing/subscription \
  -H "Authorization: Bearer $GOODAPI_API_KEY"
```

```json
{
  "object": "billing_subscription",
  "has_payment_method": true,
  "soft_limit_usd": 0.2,
  "hard_limit_usd": 0.2,
  "system_hard_limit_usd": 0.2,
  "access_until": 1795235356
}
```

- `hard_limit_usd`：这个密钥的总额度，单位美元。
- `access_until`：过期时间，Unix 时间戳（秒）。

## 已用金额

```bash
curl https://api.goodapi.tech/v1/dashboard/billing/usage \
  -H "Authorization: Bearer $GOODAPI_API_KEY"
```

```json
{"object": "list", "total_usage": 0.3}
```

`total_usage` 是这个密钥已经用掉的金额，单位是**美分**（与 OpenAI 旧版接口一致），上例是 $0.003。

## 额度明细

```bash
curl https://api.goodapi.tech/api/usage/token \
  -H "Authorization: Bearer $GOODAPI_API_KEY"
```

```json
{
  "code": true,
  "message": "ok",
  "data": {
    "object": "token_usage",
    "name": "my-project",
    "total_granted": 100000,
    "total_used": 1500,
    "total_available": 98500,
    "unlimited_quota": false,
    "expires_at": 1795235356,
    "model_limits_enabled": true,
    "model_limits": {"claude-sonnet-5-5": true}
  }
}
```

- `total_granted`、`total_used`、`total_available`：总额度、已用、剩余，单位是额度，**500,000 额度 = 1 美元**。
- `unlimited_quota` 为 `true` 时，这个密钥不限额度，只受账户余额限制。
- `expires_at`：过期时间，Unix 时间戳（秒）。
- `model_limits`：开启了模型限制时，列出允许的模型。

创建密钥时开启了「允许查询用户额度」，返回里还会多一个 `user_quota`，是**账户**的余额：

```json
"user_quota": {"total_available": 99976314, "total_used": 23686}
```

## 调用记录

```bash
curl https://api.goodapi.tech/api/log/token \
  -H "Authorization: Bearer $GOODAPI_API_KEY"
```

返回这个密钥最近的调用记录（最新的在前，最多 1,000 条）：

```json
{
  "success": true,
  "message": "",
  "data": [
    {
      "created_at": 1795247120,
      "token_name": "my-project",
      "model_name": "deepseek-v4-flash",
      "quota": 28,
      "prompt_tokens": 87,
      "completion_tokens": 12,
      "use_time": 1,
      "is_stream": false,
      "request_id": "..."
    }
  ]
}
```

| 字段 | 说明 |
|---|---|
| `created_at` | 调用时间，Unix 时间戳（秒） |
| `model_name` | 模型 |
| `prompt_tokens`、`completion_tokens` | 输入、输出 tokens |
| `quota` | 本次扣费，单位是额度（500,000 额度 = 1 美元） |
| `use_time` | 耗时，秒 |
| `is_stream` | 是否流式 |
| `request_id` | 请求 ID，与响应头 `x-oneapi-request-id` 相同 |

更完整的记录和筛选，在控制台的「[使用日志](https://api.goodapi.tech/usage-logs)」页查看。
