你可以在 Console 直接看余额。
你也可以用 API 做自动化监控。
你应该优先用什么方式
方式 1:在 Console 查看
你打开 Console 就能看到余额与用量。
方式 2:用 API 定时查询
如果你要做告警,你可以用脚本定时请求余额接口。
你要把凭据放在服务端。
你不要把任何 key 写进前端或公开仓库。
示例:用 curl 查询
下面是一个可复制的模板。
你把 YOUR_ACCESS_TOKEN 换成你的凭据。
curl --compressed 'https://api.yelinai.com/api/user/self' \
-H 'Accept: application/json' \
-H 'Authorization: YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json'
你需要加 --compressed。
这个接口会返回 gzip 压缩内容。
示例:用 Python 查询
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 小于阈值时,你就发通知。