Skip to content

LinLin00000000/openai-billing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

查询 openai 余额

Install

npm i openai-billing

Usage

import { fetchBilling } from "openai-billing"

// for CommonJS
// const { fetchBilling } = require("openai-billing")

const apikey = 'YOUR OPENAI KEY'
const result = await fetchBilling(apikey, { baseURL: 'https://api.openai.com' })

if (result.error) {
    const { code, message } = result.error
    console.error(`Error: ${code} - ${message}`)
}
else {
    const { total, used, remain, expiresTime } = result
    console.log(`
额度总量: 💵 $ ${total}
已用额度: 💵 $ ${used}
剩余额度: 💵 $ ${remain}
过期时间:🪫  ${new Date(expiresTime).toLocaleDateString()}
    `)
}

Use in browser

<script src="https://cdn.jsdelivr.net/npm/openai-billing@2">
const apikey = 'YOUR OPENAI KEY'
const result = await OpenaiBilling.fetchBilling(apikey, { baseURL: 'https://api.openai.com' })

// ...