OPENAI批量查询api账户余额源码,可用来查询openai api的账户类型、总额度、已使用、剩余额度、有效期。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>API-KEY信息查询</title> <link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABnxJREFUWEetV2lMlFcUPd8MMMywyiKrDIussgzKqoKgIhYFtbFWa2Lq0hhj1ZhImjRtmjRp2iamtbY1GumSGC2gFgVUsKAi+z44DPu+yi4MO8x8zXvICDgwEH0/Jpm8d+8975177r0fgwXrwq3rnhwu5zgLdjsAIQCdhWdW+H8ELJoApHE43D9+PHBUMteemf1z5uFlnrZM8AuAzwBwVhhkuccVYHFtXH/0/K8RZyeIEQUwE5yfCjBbluvpnc4xeDauO7qTgKAAouNirgI4+U5OV2jMAlcufnziNEM4Z7hM6XKe3VCgixAXD7hZCmEo0MGUfBotfT3Ib6hGWUs92JWBkLMsREx03PXLAHNGna2rxRp8EhgKTa4GWvq60TU0AC0NTaxdbQk9bT4qOlpwpygLWlwNjE1OYGxqUp1LMGAvMdFxMVIAbkudtlpljNNbIzE4NoKbeU/R1t+rPK7B5eJE8E7Ym5rPc9E+0Ie0ilJI25sXd81CylyIi5ExgO5SAE6GRMDayASXHt9D3/CQ8igBFiUKgJ2pOYbHx/CirREDI8MwEOjAw9oOBnwBiptqcbswCwpWoSqEjLzAotS5WtrAz84JblZCZNdKkViaR53o8vjY6bEBvnZONA/SK8uQWVOOabl83ssQcAEOLnhaVYZHL4pU3lElAA0OFwf8giGysYeCZcFhGNzISYekrQke1rb4yDcIPE0tlDTV4ZGkEENjo9S5haEReoYGMa14A+REcDjsV1vgh+R4DI3PnJtXiFS9wAHfIPjYOSG3vpLyTQJee/YQ9d2dOLJpO1YJdPFvcTZa+3uoLzN9Q0SKAuBkboX+ERmSywpQ3kaKHyg9p0J3IaEkB7l1leoBEF7Phe1FSXMdYvMz4G5tiyMbt80DUNvVTp3xtXgI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0Ofr4Lf0xHnAFn2B6NiYS2BwbtE3er1BqhnJjYnpKXzoswneNg4YnZxAc183ZGOjdCawMTHDxNQUbuU9QVXnTFNSt5gv4v/yULBy8XLG8rnOSLn1d3DBGiMT8DV5lB6ijufVEgy+npDUBacqID/RsTFXwODUcgze95k3n2bDghSwCHnfAdT5m/dxypMJfmJmPtG46gzf174SwKzDC7Ex7hyGPc6yTBjLQKjum+FdgfwPVAUVDIGyUFAAAAAASUVORK5CYII= "> <style> body { background-color: #497e7e; } table { margin: auto; border-collapse: collapse; } th, td { padding: 8px; text-align: center; color: #000000; border: 1px solid #000000; } th:first-child, td:first-child { width: 520px; } h1 { text-align: center; color: #000000; } form { display: flex; justify-content: center; align-items: center; margin-bottom: 20px; } textarea#api-key-input { width: 430px; font-size: 16px; padding: 10px; margin-right: 10px; border-radius: 5px; border: none; resize: vertical; rows: 8; } select#api-url-select, input#custom-url-input { width: 300px; height: 50px; font-size: 16px; background-color: #212121; border-radius: 5px; color: #ffffff; border: none; margin-right: 10px; } input#custom-url-input { display: none; } button { height: 50px; font-size: 16px; background-color: #ff8c00; color: #ffffff; border: none; border-radius: 5px; cursor: pointer; } .status-ok { color: #2d8d2d; } .status-error { color: #7e1707; } tr:nth-child(even) { /* 将偶数行背景色改为深蓝色 */ background-color: #f1d39c; } tr:nth-child(odd) { /* 将偶数行背景色改为深蓝色 */ background-color: #f1d39c; } .links-container a { text-decoration: none; display: inline-block; text-align: center; border: 1px solid black; padding: 5px 10px; margin: 10px; transition: all 0.3s ease; } .links-container a:hover { background-color: black; color: white; } .link1 { color: red; } .link2 { color: blue; } .links-container{ text-align: center; } </style> </head> <body> <h1>API-KEY信息查询</h1> <form> <textarea id="api-key-input" placeholder="请输入API KEY,每行一个"></textarea> <div style="display: flex; align-items: flex-end"> <select id="api-url-select"> <option value="https://openai.1rmb.tk">【社区反代】https://openai.1rmb.tk</option> <option value="custom">自定义API地址 ...</option> </select> <input type="text" id="custom-url-input" placeholder="请输入自定义API链接" /> </div> <button type="button" onclick="sendRequest()">发送请求</button> </form> <table id="result-table"> <thead> <tr> <th>API KEY</th> <th style="width: 120px">账号类型</th> <th style="width: 50px">总额度</th> <th style="width: 50px">已使用</th> <th style="width: 70px">剩余额度</th> <th style="width: 70px">有效期</th> </tr> </thead> <tbody></tbody> </table> <script> // 定义查询过的API-KEY列表 let queriedApiKeys = []; async function checkBilling(apiKey,apiUrl) { // 计算起始日期和结束日期 const now = new Date(); const startDate = new Date(now - 90 * 24 * 60 * 60 * 1000); const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000); // 设置API请求URL和请求头 const urlSubscription = apiUrl+'/v1/dashboard/billing/subscription'; // 查是否订阅 const urlUsage = apiUrl+`/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`; // 查使用量 const headers = { "Authorization": "Bearer " + apiKey, "Content-Type": "application/json" }; try { // 获取API限额 let response = await fetch(urlSubscription, {headers}); if (!response.ok) { const data = await response.json() console.log(apiKey + ":账户已被封禁,请登录OpenAI进行查看。"+data.error.message); return; } const subscriptionData = await response.json(); //console.log(usageData); // 有效期 const end_date = subscriptionData.access_until; // 账号类型 const plan = (subscriptionData.plan.title === "Pay-as-you-go") ? "Pay-as-you-go" : subscriptionData.plan.id; // 总额度 const totalAmount = subscriptionData.hard_limit_usd; // 获取已使用量 response = await fetch(urlUsage, {headers}); const usageData = await response.json(); const totalUsage = usageData.total_usage / 100; // 计算剩余额度 const remaining = totalAmount - totalUsage; // 输出总用量、总额及余额信息 console.log(`Total Amount: ${totalAmount.toFixed(2)}`); console.log(`Used: ${totalUsage.toFixed(2)}`); console.log(`Remaining: ${remaining.toFixed(2)}`); return [plan, totalAmount, totalUsage, remaining, formatDate(new Date(end_date * 1000))]; } catch (error) { console.error(error); return [null, null, null]; } } function formatDate(date) { const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); return `${year}-${month}-${day}`; } function sendRequest() { let apiKeyInput = document.getElementById("api-key-input"); let apiUrlSelect = document.getElementById("api-url-select"); let customUrlInput = document.getElementById("custom-url-input"); if (apiKeyInput.value.trim() === "") { alert("请填写API KEY"); return; } // 添加以下一行代码,清空之前的结果信息 document.getElementById("result-table").getElementsByTagName('tbody')[0].innerHTML = ""; let apiUrl = ""; if (apiUrlSelect.value === "custom") { if (customUrlInput.value.trim() === "") { alert("请设置API链接"); return; } else { apiUrl = customUrlInput.value.trim(); } } else { apiUrl = apiUrlSelect.value; } let apiKeys = apiKeyInput.value.trim().split("\n"); let tableBody = document.querySelector("#result-table tbody"); for (let i = 0; i < apiKeys.length; i++) { let apiKey = apiKeys[i].trim(); // 判断是否已经查询过 if (queriedApiKeys.includes(apiKey)) { console.log(`API KEY ${apiKey} 已查询过,跳过此次查询`); continue; } queriedApiKeys.push(apiKey); checkBilling(apiKey,apiUrl).then((data) => { // update table with data let row = document.createElement("tr"); // API KEY let apiKeyCell = document.createElement("td"); apiKeyCell.textContent = apiKey; row.appendChild(apiKeyCell); if (data[0] === null) { // ERROR MESSAGE let errorMessageCell = document.createElement("td"); errorMessageCell.colSpan = "3"; errorMessageCell.classList.add("status-error"); errorMessageCell.textContent = "不正确或已失效的API-KEY"; row.appendChild(errorMessageCell); } else { // Plan let totalPlanCell = document.createElement("td"); totalPlanCell.textContent = data[0]; row.appendChild(totalPlanCell); // TOTAL GRANTED let totalGrantedCell = document.createElement("td"); totalGrantedCell.textContent = data[1].toFixed(2); row.appendChild(totalGrantedCell); // TOTAL USED let totalUsedCell = document.createElement("td"); totalUsedCell.textContent = data[2].toFixed(2); row.appendChild(totalUsedCell); // TOTAL AVAILABLE let totalAvailableCell = document.createElement("td"); totalAvailableCell.textContent = data[3].toFixed(2); row.appendChild(totalAvailableCell); // 有效期 let totalEndDateCell = document.createElement("td"); totalEndDateCell.textContent = data[4]; row.appendChild(totalEndDateCell); } tableBody.appendChild(row); // 添加以下代码,查询完成后删除已查询的API-KEY if (i === apiKeys.length - 1) { queriedApiKeys = []; } }).catch((error) => { console.error(error); // update table with error message let row = document.createElement("tr"); let apiKeyCell = document.createElement("td"); apiKeyCell.textContent = apiKey; row.appendChild(apiKeyCell); let errorMessageCell = document.createElement("td"); errorMessageCell.colSpan = "4"; errorMessageCell.style.width = "90px"; errorMessageCell.classList.add("status-error"); errorMessageCell.textContent = "API请求失败,请检查其有效性或网络情况"; row.appendChild(errorMessageCell); tableBody.appendChild(row); // 添加以下代码,查询完成后删除已查询的API-KEY if (i === apiKeys.length - 1) { queriedApiKeys = []; } }); } } let apiUrlSelect = document.getElementById("api-url-select"); let customUrlInput = document.getElementById("custom-url-input"); apiUrlSelect.addEventListener("change", function () { if (apiUrlSelect.value === "custom") { customUrlInput.style.display = "inline-block"; customUrlInput.style.marginTop = "5px"; } else { customUrlInput.style.display = "none"; } }); </script> </body> </html>
发表评论