From 2df31048405b861d46e7c4f9ea9bf25e670190b1 Mon Sep 17 00:00:00 2001 From: 666ghj <670939375@qq.com> Date: Thu, 4 Dec 2025 11:04:46 +0800 Subject: [PATCH] Add balance check script for OpenRouter API - Introduced a new Python script to check and display the balance of credits using the OpenRouter API. - Implemented error handling for API requests and included informative print statements for total credits, total usage, and remaining balance. - Added a placeholder for the API key to facilitate user customization. --- 余额查询.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 余额查询.py diff --git a/余额查询.py b/余额查询.py new file mode 100644 index 0000000..78f2874 --- /dev/null +++ b/余额查询.py @@ -0,0 +1,36 @@ +import requests + +def check_openrouter_balance(api_key): + url = "https://openrouter.ai/api/v1/credits" + + headers = { + "Authorization": f"Bearer {api_key}", + "Referer": "https://your-site.com", + "X-Title": "Balance Check Script" + } + + try: + response = requests.get(url, headers=headers) + response.raise_for_status() # 检查请求是否成功 + + data = response.json().get("data", {}) + + total_credits = data.get("total_credits", 0) # 总充值/赠送金额 + total_usage = data.get("total_usage", 0) # 总消耗金额 + + # 计算剩余余额 + balance = total_credits - total_usage + + print(f"💰 总额度 (Total Credits): ${total_credits}") + print(f"📉 已使用 (Total Usage): ${total_usage}") + print(f"💵 剩余余额 (Balance): ${balance:.4f}") + + return balance + + except requests.exceptions.RequestException as e: + print(f"查询失败: {e}") + return None + +# 替换你的 API Key +MY_API_KEY = "" +check_openrouter_balance(MY_API_KEY) \ No newline at end of file