Get the remaining credits via the API

Hello,

I’m using the completion API for a personal project, and I’d really like to know what are the remaining (free) credits on my account ?

Is such endpoint exists ?

Thanks in advance

2 Likes

To be clear, you’re looking to be able to check your account usage stats via an api call instead of manually checking through the “Manage Account > Usage” page, correct?

Yeah I think that’s what he meant, I would also like to know the answer to this question.

There is current no documented OpenAI API endpoint to access account information.

Would be great if there was :slight_smile:

There are a couple of API endpoints which expose your usage & credits

https://api.openai.com/v1/usage
https://api.openai.com/dashboard/billing/credit_grants

I have been working on an #unofficial PowerShell Module to expose them. https://www.powershellgallery.com/packages/PS-OpenAI/

It’s basic, but it shows the endpoints I have found and what’s required to get a valid API response.

This is the output for my credit_grants. (I have redacted my id to publish here)

{
    "object":  "credit_summary",
    "total_granted":  18.0,
    "total_used":  0.6284545,
    "total_available":  17.3715455,
    "grants":  {
                   "object":  "list",
                   "data":  [
                                {
                                    "object":  "credit_grant",
                                    "id":  "bdb804***********************ff0132",
                                    "grant_amount":  18.0,
                                    "used_amount":  0.6284545,
                                    "effective_at":  1673740800.0,
                                    "expires_at":  1682899200.0
                                }
                            ]
               }
}

Hope this helps.

Enjoy.
M

3 Likes

Hi @mc1903

Thank you very much for those two undocumented API endpoints! I heard rumors someone had reversed engineered these.

Do you have other undocumented API endpoints, or are those the only two?

Thanks!

1 Like

Take a look through the GitHub repo https://github.com/mc1903/PS-OpenAI and you will find the url in each of the functions under /Src/Public/

They use the [uri]$url = "https://api.openai.com/*" variable.

Just be aware that some of the totals return a value 100x the actual usage.

For instance my billing for yesterday was 0.05 (5 cents), but the https://api.openai.com/dashboard/billing/usage endpoint returns my total_usage as 4.562

{
	"object": "list",
	"daily_costs": [{
		"timestamp": 1674691200.0,
		"line_items": [{
				"name": "Base",
				"cost": 4.562
			},
			{
				"name": "FT Training",
				"cost": 0.0
			},
			{
				"name": "FT Inference",
				"cost": 0.0
			},
			{
				"name": "Embeddings",
				"cost": 0.0
			},
			{
				"name": "DALL-E API",
				"cost": 0.0
			}
		]
	}],
	"total_usage": 4.562
}

Where as the webpage shows it as:

image

Enjoy.
M

3 Likes

This looks cool. Quick question. My guess is that OpenAI has changed some things since you built your git repo but when I call the “https://api.openai.com/dashboard/billing/usage” endpoint, I get the response:
Your request to GET /dashboard/billing/credit_grants must be made with a session key (that is, it can only be made from the browser). You made it with the following key type: secret.
Would love to know if it is still possible to access this usage info via API.
Thanks in advance

1 Like

Yes, discovery of, and direct use of, the API that the usage page accesses has had authentication by API key disabled, so it can only be polled from the authentication of you being in the web page.

More secure: people stealing your API keys can’t interrogate the value of the account or plan the best time to attack.

Yes, OpenAI changed the authentication for that API endpoint, as well as a couple others, soon after I built the PS-OpenAI PowerShell module. Hence it has not been updated in months.

To be fair to OpenAI, I was calling unofficial API endpoints that I had discovered; so it was only a matter of time before something was changed/updated on the back-end that broke the access.

I am frustrated at OpenAI as they steadfastly refuse to give any meaningful comment on if/when a supported/official set of account management API endpoints will be made available.

You can extract your session key from your browser session using the F12 developer options and use it in place of the API key; it works for a while; but does expire.

I had a brief look the Okta Auth0 authentication, used for the web browser access; but I don’t have much experience with it and I am fairly sure any serious attempt at reverse engineering account access will get me banned from the platform.

M

1 Like

Thanks for both of your answers. Much appreciated.

1 Like

Can you show me how you made the request to this endpoint to obtain the output?

Look at the GitHub URL I posted in this thread back in Jan 2023.

You cannot use your API key for this any longer, so you need to extract your session bearer token from a browser session logged in to your OpenAI account.

I use F12 to open a Dev Tools window and then select a new link in my account (Usage/etc). In the Dev Tools window, select one of the GET’s to api.openai.com and then in the headers you will find the Authorization Bearer.

Use it to override the apiKey with the Get-OpenAICreditGrants command like this:


Get-OpenAICreditGrants -apiKey "sess-2vUoce******************************Ci6h" -jsonOut
{
    "object":  "credit_summary",
    "total_granted":  23.0,
    "total_used":  3.0700000000000003,
    "total_available":  4.9,
    "total_paid_available":  4.9,
    "grants":  {
                   "object":  "list",
                   "data":  [
                                {
                                    "object":  "credit_grant",
                                    "id":  "bcc7771e-13af-4676-****-************",
                                    "grant_amount":  5.0,
                                    "used_amount":  0.1,
                                    "effective_at":  1709337600.0,
                                    "expires_at":  1743465600.0
                                },
                                {
                                    "object":  "credit_grant",
                                    "id":  "bdb804a7-7529-4b14-****-************",
                                    "grant_amount":  18.0,
                                    "used_amount":  2.97,
                                    "effective_at":  1673740800.0,
                                    "expires_at":  1682899200.0
                                }
                            ]
               }
}

I have redacted part of my bearer and credit grant ID’s to post here, you will see them as clear text.

Be aware that the bearer key will expire in a few days.

Hope that helps.

M