Skip to content

Instantly share code, notes, and snippets.

@jbn
Created March 11, 2023 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbn/0903c8b053eaf5ef9753abb52a68b2e2 to your computer and use it in GitHub Desktop.
Save jbn/0903c8b053eaf5ef9753abb52a68b2e2 to your computer and use it in GitHub Desktop.
A python module to get how much you owe Sam Altman
from typing import Any, Dict, Optional
import openai
import requests
from datetime import date, timedelta
API_BASE = "https://api.openai.com"
USAGE_ENDPOINT = f"{API_BASE}/dashboard/billing/usage"
def get_billing_usage(
start_date: Optional[date] = None, end_date: Optional[date] = None
) -> Dict[str, Any]:
start_date = start_date or date.today() - timedelta(days=1)
end_date = end_date or date.today()
assert start_date < end_date, "start_date must be before end_date"
resp = requests.get(
url="https://api.openai.com/v1/dashboard/billing/usage",
params={"start_date": start_date, "end_date": end_date},
headers={"Authorization": f"Bearer {openai.api_key}"},
)
resp.raise_for_status()
return resp.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment