#Getting Usage Programatically

1 messages · Page 1 of 1 (latest)

real tree
#

I don't know if this is allowed, but you can reverse engineer the dashboard page, and then you get this endpoint:
https://api.openai.com/dashboard/billing/usage?start_date=YYYY-MM-DD&end_date=YYYY-MM-DD
You can adjust the dates in the query parameters to your needs in the following format: YYYY-MM-DD, e.g.
https://api.openai.com/dashboard/billing/usage?end_date=2024-01-01&start_date=2023-12-01

In the headers you will need to include your token and organization, here's how the headers object looks like:

{ 
  "Openai-Organization": "YOUR_ORGANIZATION_ID",
  "Authorization": "Bearer YOUR_TOKEN"
}
  • To get your organization ID, go to the organization page and copy your organization ID, this looks something like org-abc123
  • To get your token:
  1. Go to a random page on the openai platform, e.g. the playgrond
  2. Open up developer tools by clicking F12 or right clicking on the page and selecting inspect
  3. Navigate to the network tab, this is the fourth tab.
  4. Reload the page
  5. Hit ctrl+f
  6. Type Bearer sess in the search bar
  7. Hit enter, and you'll see a couple results, click on one of them, and your token should be highlighted on the screen.
#

I don't really know, like a month or something like that

#

Yeah probably, but it could be longer, haven't tried it out yet

#

What's cron?

#

It generally shouldn't be too hard to code that, you might want to try it out and see how long the key lasts, you can log out and in again to get a new SSID

#

That might also work, but it isn't ideal

#

Yeah I don't know why OpenAI didn't add that yet, I've tried putting an API key into the bearer, but then it errored

#

The sess key does in fact reset when you log in and out, so it'll probably also expire when your login expires, which would probably be something like 1-2 months

#
{
    "error": {
        "message": "Incorrect API key provided: sess-NWA*********************************3DFl. You can find your API key at https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}