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

闪电发卡3年前ChatGPT4211

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


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


相关文章

OpenAI API KEY 3.5购买方式分析,如何优雅的购买ChatGPT APIKey

一、什么是Openai APIKey ?OpenAI APIKey 是用于访问 OpenAI API 的凭证,它允许开发者通过编程方式与 OpenAI 的模型进行交互和使用。通过这个 API,开发者可...

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

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

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

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

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

如何使用Python调用ChatGPT API?

如何使用Python调用ChatGPT API?

什么是ChatGPT?  ChatGPT 是由 OpenAI 开发的一个语言模型。OpenAI 是一家领先的人工智能研究机构。ChatGPT 基于变换器架构,使用深度学习生成会话风格的文本。该模型在大...

ChatGPT API教程在线对接OpenAI APIKey技术教程

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

怎样使用ChatGPT实现财务自由?国内使用ChatGPT的方法教程

相信很多人最近刷到利用ChatGPT赚钱的视频,那么到底是真的还是假的呢?我大致给大家分了两大种类型,请大家看下我分析的是否正确。ChatGPTChatGPT是一种基于自然语言处理技术的人工智能模型,...

发表评论    

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