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

闪电发卡2年前ChatGPT2930

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


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 4.0 APIKey权限申请开通方法以及购买渠道攻略(官方直连+转发)

ChatGPT 4.0 APIKey权限申请开通方法以及购买渠道攻略(官方直连+转发)

一、APIKey开通4.0权限方法:目前只要充值API预付费就可以直接开通GPT4.0 API权限,可以使用ChatGPT API所有模型,包括GPT-4,GPT-3.5,DALLE3等,难点是需要国...

微信快速接入ChatGPT教程,让你的微信秒变人工智能机器人

微信快速接入ChatGPT教程,让你的微信秒变人工智能机器人

前言最近ChatGPT可谓是火的一发不可收拾,从圈内火到圈外。在人工智能领域,Ai已经是一个屡见不鲜的东西了,为什么这次OpenAi推出的ChatGPT却异常的受人欢迎?其实这还得益于GPT模型。那么...

OpenAI API Key购买批发5美元余额,ChatGPT模型GPT3.5、GPT4.0直连APIKey和转发APIKey购买批发代充值,可自定义余额,安全稳定不限速

一、什么是OpenAI APIKey?OpenAI APIKey是OpenAI旗下产品ChatGPT、Dall-E 3、Sora等调用其接口服务所必须得密钥,可以验证调用权限,是一个形如sk-xxxx...

如何利用ChatGPT进行赚钱?ChatGPT可变现项目分析

GPT-3 本身并没有直接的盈利模式,因为它是由 OpenAI 开发并公开发布的一个自然语言生成模型,可以免费使用。但是,GPT-3 可以作为一个关键技术与商业应用结合,从而带来盈利。以下是 GPT-...

ChatGPT视频摘要实战:ChatGPT API应用实例教程

ChatGPT视频摘要实战:ChatGPT API应用实例教程

随着在 YouTube 上提交的大量新视频,很容易感到挑战并努力跟上我想看的一切。 我可以与我每天将视频添加到“稍后观看”列表中的经历联系起来,只是为了让列表变得越来越长,实际上并没有稍后再看。 现在...

ChatGPT API技术教程OpenAI APIKey在线对接-Chat Completion块对象

闪电发卡ChatGPT产品推荐:ChatGPT独享账号:https://www.chatgptzh.com/post/86.htmlChatGPT Plus独享共享账号购买代充:https://www...

发表评论    

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