查询OpenAI账号APIKey可用余额、使用量和使用明细接口,用python实现

闪电发卡3年前ChatGPT4624

现在可以使用以下接口进行实现


Python


apikey = ""
    subscription_url = "https://api.openai.com/v1/dashboard/billing/subscription"
    headers = {
        "Authorization": "Bearer " + apikey,
        "Content-Type": "application/json"
    }
    subscription_response = requests.get(subscription_url, headers=headers)
    if subscription_response.status_code == 200:
        data = subscription_response.json()
        total = data.get("hard_limit_usd")
    else:
        return subscription_response.text
 
    # start_date设置为今天日期前99天
    start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime("%Y-%m-%d")
    # end_date设置为今天日期+1
    end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    billing_url = f"https://api.openai.com/v1/dashboard/billing/usage?start_date={start_date}&end_date={end_date}"
    billing_response = requests.get(billing_url, headers=headers)
    if billing_response.status_code == 200:
        data = billing_response.json()
        total_usage = data.get("total_usage") / 100
        daily_costs = data.get("daily_costs")
        days = min(5, len(daily_costs))
        recent = f"最近{days}天使用情况  \n"
        for i in range(days):
            cur = daily_costs[-i-1]
            date = datetime.datetime.fromtimestamp(cur.get("timestamp")).strftime("%Y-%m-%d")
            line_items = cur.get("line_items")
            cost = 0
            for item in line_items:
                cost += item.get("cost")
            recent += f"\t{date}\t{cost / 100} \n"
    else:
        return billing_response.text
 
    return f"\n总额:\t{total:.4f}  \n" \
                f"已用:\t{total_usage:.4f}  \n" \
                f"剩余:\t{total-total_usage:.4f}  \n" \
                f"\n"+recent

输出结果如下:


总额: 18.0000


已用: 0.4737


剩余: 17.5263


最近5天使用情况


2023-04-28 0.0


2023-04-27 0.0


2023-04-26 0.0


2023-04-25 0.0


2023-04-24 0.0


可访问 https://aichat.xingtupai.com 查看示例

余额查询方法可参考:https://www.chatgptzh.com/post/325.html


相关文章

ChatGPT Plus共享账号是什么?有什么优势和劣势?怎样才能购买到靠谱的ChatGPT Plus共享账号

ChatGPT Plus共享账号是指一种让用户可以共享ChatGPT Plus订阅服务的方式。ChatGPT Plus是OpenAI推出的高级订阅服务,为用户提供更多功能和优势,使他们能够更好地利用C...

详解闪电发卡所售各种的ChatGPT相关产品的基本概念和产品区别

我们在购买ChatGPT账号时由于产品众多,不能分清每个产品的不同,本篇文章就用最简单的语言让您了解ChatGPT相关产品的联系和区别。在购买ChatGPT产品之前,我们要了解ChatGPT相关的基本...

ChatGPT APIKey余额在线查询和使用Python代码查询的方法教程

ChatGPT APIKey余额在线查询和使用Python代码查询的方法教程

一、官方APIKey余额在线查询原查询ChatGPT API余额地址:https://api.openai.com/v1/dashboard/billing/subscriptionPython代码查...

ChatGPT 3.5 API Key密钥免费获取方法攻略,如何用更低的价格购买到GPT3.5和4.0API Key

首先,我们得到一个不幸的消息,自2024年4月以来,ChatGPT普通账号不再赠送5美元余额了,这就意味着,我们无法低成本甚至免费地获取到APIKey余额了。当然,如果你的ChatGPT账号还有API...

ChatGPT函数调用初体验:让ChatGPT具备抓取网页文本的能力

 OpenAI在6月13号升级了ChatGPT,推出了类似其网页版插件的功能——函数调用(Function calling),13号当天我在很多微信公众号就看到了这个消息,甚至有人将函数调用称为杀手级...

ChatGPT转发APIKey常见问题解决方案

我们在使用ChatGPT转发APIKey时会遇到各种问题,下面就来分析一下常用的解决方法。1、检查配置。检查APIKey和转发地址是否正确配置填写2、由于使用软件和编程语言的不同,API格式会有些许区...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。