LoginSignup
2
1

More than 1 year has passed since last update.

【Python】OpenAIのAPI/ChatGPTのAPIから残クレジットを取得する

Last updated at Posted at 2023-03-16

以下のようにする

import requests
OPENAI_API_KEY = "sk-*****"
headers = {
    'Authorization': f'Bearer {OPENAI_API_KEY}',
}
response = requests.get('https://api.openai.com/dashboard/billing/credit_grants', headers=headers)
response_json = response.json()

取得できるのは以下のイメージ

{'object': 'credit_summary', 'total_granted': 18.0, 'total_used': 0.18462800000000001, 'total_available': 17.815372, 'grants': {'object': 'list', 'data': [{'object': 'credit_grant', 'id': '*****', 'grant_amount': 18.0, 'used_amount': 0.18462800000000001, 'effective_at': 1675641600.0, 'expires_at': 1685577600.0}]}}

この場合、無料分の18ドルがtotal_grantedにあり、そのうち、使用分0.18ドルがtotal_usedに記載されている

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1