查询 OpenAI KEY 余额一直不方便。花了些时间写了一行代码,可以直接输出余额和有效期。大家可以试试
curl -s -H "Authorization: Bearer [OpenAI KEY]" https://api.openai.com/dashboard/billing/credit_grants | python3 -c "import sys, json, datetime; data = json.load(sys.stdin); print(data if 'total_granted' not in data else ('总 金 额:%s\n 已 使 用:%s\n 剩余金额:%s\n 截至日期:%s' % (data['total_granted'],data['total_used'],data['total_available'],datetime.datetime.fromtimestamp(data['grants']['data'][0]['expires_at']))));"
总 金 额:18.0
已 使 用:2.28
剩余金额:15.72
截至日期:2023-06-01 08:00:00
运行前可以先 curl https://api.openai.com/cdn-cgi/trace 检查 IP 是否符合 OpenAI 要求
先检查访问 openai api 的 IP 地址
curl --socks5-hostname [socks5-ip]:[port] https://api.openai.com/cdn-cgi/trace
再运行查询命令:
curl -s --socks5-hostname [socks5-ip]:[port] -H "Authorization: Bearer [OpenAI KEY]" https://api.openai.com/dashboard/billing/credit_grants | python3 -c "import sys, json, datetime; data = json.load(sys.stdin); print(data if 'total_granted' not in data else ('总 金 额:%s\n 已 使 用:%s\n 剩余金额:%s\n 截至日期:%s' % (data['total_granted'],data['total_used'],data['total_available'],datetime.datetime.fromtimestamp(data['grants']['data'][0]['expires_at']))));"
如果 windows 安装了 python3 ,这行命令也是适用的。
1
yinmin OP 写了一个 sh 文件,可以方便查询:
sudo nano /usr/bin/openai_credit.sh ===== curl -s -H "Authorization: Bearer $1" https://api.openai.com/dashboard/billing/credit_grants | python3 -c "import sys, json, datetime; data = json.load(sys.stdin); print(data if 'total_granted' not in data else ('总 金 额:%s\n 已 使 用:%s\n 剩余金额:%s\n 截至日期:%s' % (data['total_granted'],data['total_used'],data['total_available'],datetime.datetime.fromtimestamp(data['grants']['data'][0]['expires_at']))));" ====== sudo chmod +x /usr/bin/openai_credit.sh 查询时使用:openai_credit.sh [openai key] |
2
zictos 2023-04-02 06:41:34 +08:00
是不是你也发现之前的方法不行了,凌晨 1 点半开始,我以前在 python 中使用的方法不行了。
提示: { "error": { "message": "Your request to GET /dashboard/billing/credit_grants must be made with a session key (that is, it can only be made from the browser). You made it with the following key type: secret.", "type": "server_error", "param": null, "code": null } } |
3
zictos 2023-04-02 06:46:06 +08:00
你真的还可以吗?我用 curl 试了,还是跟上面报错是一样的,怎么那么巧,你刚一写就不行了。
curl -s -H "Authorization: Bearer sk-abcdefghijklmn" https://api.openai.com/dashboard/billing/credit_grants |
4
T3rryy 2023-04-02 08:45:20 +08:00
难蚌.. 楼主刚发完贴 OPEN 改了 api ..
|
6
chinaqq 2023-04-02 09:27:20 +08:00
@zictos 同样,我的 WP 插件 [OpenAI Tools for WordPress & WooCommerce]( https://wordpress.org/plugins/openai-tools-for-wp-wc/)和 curl 都这样报错了。
|
8
miraku 2023-04-02 14:04:45 +08:00
今天查余额, 接口解析失败了没返回结果给前端, 我还以为我账号被封了😂
|
9
rootx 2023-04-02 16:00:39 +08:00 via iPhone
有最新的方案吗?
|