> ## Documentation Index
> Fetch the complete documentation index at: https://docs.yelinai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何通过 API 查询账号余额

> 用脚本查询余额并做阈值告警，方便你自动化监控成本。

<Info>
  你可以在 **Console** 直接看余额。
  你也可以用 API 做自动化监控。
</Info>

## 你应该优先用什么方式

### 方式 1：在 Console 查看

你打开 **Console** 就能看到余额与用量。

### 方式 2：用 API 定时查询

如果你要做告警，你可以用脚本定时请求余额接口。

<Warning>
  你要把凭据放在服务端。
  你不要把任何 key 写进前端或公开仓库。
</Warning>

## 示例：用 curl 查询

下面是一个可复制的模板。
你把 `YOUR_ACCESS_TOKEN` 换成你的凭据。

```bash theme={"system"}
curl --compressed 'https://api.yelinai.com/api/user/self' \
  -H 'Accept: application/json' \
  -H 'Authorization: YOUR_ACCESS_TOKEN' \
  -H 'Content-Type: application/json'
```

<Note>
  你需要加 `--compressed`。
  这个接口会返回 gzip 压缩内容。
</Note>

## 示例：用 Python 查询

```python theme={"system"}
import os
import requests

url = "https://api.yelinai.com/api/user/self"
access_token = os.environ["YELIN_ACCESS_TOKEN"]

headers = {
    "Accept": "application/json",
    "Authorization": access_token,
    "Content-Type": "application/json",
}

resp = requests.get(url, headers=headers, timeout=10)
resp.raise_for_status()

data = resp.json()["data"]
quota = data.get("quota")
used_quota = data.get("used_quota")
print({"quota": quota, "used_quota": used_quota})
```

## 你可以怎么做告警

你可以每隔 1-5 分钟查询一次。
当 `quota` 小于阈值时，你就发通知。

<CardGroup cols={2}>
  <Card title="调用记录" icon="history" href="/faq/call-logs">
    排查失败请求与扣费。
  </Card>

  <Card title="定价" icon="badge-dollar-sign" href="/pricing">
    用定价估算成本。
  </Card>
</CardGroup>
