Created
          March 11, 2023 21:03 
        
      - 
      
 - 
        
Save jbn/0903c8b053eaf5ef9753abb52a68b2e2 to your computer and use it in GitHub Desktop.  
    A python module to get how much you owe Sam Altman 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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