Check usage, balance and usage limit using API with php

Any way to Check usage, balance and usage limit using API with php? I have searched several forums without success.

Yes and no.

Officially, no. There is no public API for this use case.

Unofficially…

https://api.openai.com

dashboard

All dashboard endpoints require a session bearer token which can be retrieved from the Chrome Developer Console after logging in to https://platform.openai.com

billing

activity

GET
https://api.openai.com/dashboard/billing/activity

Parameters:

  • end_date=2024-03-01
  • start_date=2024-02-01
Response
{
  "object": "list",
  "data": [
    {
      "organization_id": "org-**************************",
      "organization_name": "****",
      "aggregation_timestamp": 1707696000,
      "n_requests": 200,
      "operation": "completion",
      "snapshot_id": "gpt-3.5-turbo-0125",
      "n_context_tokens_total": 144766,
      "n_generated_tokens_total": 21471,
      "email": "************@******.****",
      "api_key_id": "key_****************",
      "api_key_name": "changelog",
      "api_key_redacted": "sk-...***"
    },
    {
      "organization_id": "org-**************************",
      "organization_name": "****",
      "aggregation_timestamp": 1707696000,
      "n_requests": 2,
      "operation": "completion",
      "snapshot_id": "gpt-3.5-turbo-instruct",
      "n_context_tokens_total": 527,
      "n_generated_tokens_total": 246,
      "email": "************@******.****",
      "api_key_id": "Playground",
      "api_key_name": "",
      "api_key_redacted": "Playground"
    },
    {
      "organization_id": "org-**************************",
      "organization_name": "****",
      "aggregation_timestamp": 1707696000,
      "n_requests": 6,
      "operation": "completion",
      "snapshot_id": "gpt-4-0125-preview",
      "n_context_tokens_total": 12230,
      "n_generated_tokens_total": 434,
      "email": "************@******.****",
      "api_key_id": "key_****************",
      "api_key_name": "changelog",
      "api_key_redacted": "sk-...***"
    },
    {
      "organization_id": "org-**************************",
      "organization_name": "****",
      "aggregation_timestamp": 1707696000,
      "n_requests": 1,
      "operation": "completion",
      "snapshot_id": "gpt-4-0125-preview",
      "n_context_tokens_total": 298,
      "n_generated_tokens_total": 29,
      "email": "************@******.****",
      "api_key_id": "Playground",
      "api_key_name": "",
      "api_key_redacted": "Playground"
    },
		...
    {
      "organization_id": "org-**************************",
      "organization_name": "****",
      "aggregation_timestamp": 1708992000,
      "n_requests": 21,
      "operation": "completion",
      "snapshot_id": "gpt-3.5-turbo-0125",
      "n_context_tokens_total": 10204,
      "n_generated_tokens_total": 2071,
      "email": "************@******.****",
      "api_key_id": "key_****************",
      "api_key_name": "changelog",
      "api_key_redacted": "sk-...***"
    }
  ],
  "ft_data": [],
  "dalle_api_data": [],
  "whisper_api_data": [],
  "tts_api_data": [],
  "assistant_code_interpreter_data": []
}

usage

GET
https://api.openai.com/dashboard/billing/usage

Parameters:

  • end_date=2024-03-01
  • start_date=2024-02-01
Response
{
  "object": "list",
  "daily_costs": [
    {
      "timestamp": 1707955200.0,
      "line_items": [
        {
          "name": "GPT-4 Turbo",
          "cost": 0.0
        },
        {
          "name": "GPT-4",
          "cost": 0.0
        },
        {
          "name": "GPT-3.5 Turbo",
          "cost": 3.8144
        },
        {
          "name": "Assistants API",
          "cost": 0.0
        },
        {
          "name": "Fine-tuning models",
          "cost": 0.0
        },
        {
          "name": "Embedding models",
          "cost": 0.0
        },
        {
          "name": "Base models",
          "cost": 0.0
        },
        {
          "name": "Image models",
          "cost": 0.0
        },
        {
          "name": "Audio models",
          "cost": 0.0
        }
      ]
    },
    {
      "timestamp": 1708041600.0,
      "line_items": [
        {
          "name": "GPT-4 Turbo",
          "cost": 0.0
        },
        {
          "name": "GPT-4",
          "cost": 0.0
        },
        {
          "name": "GPT-3.5 Turbo",
          "cost": 4.0709
        },
        {
          "name": "Assistants API",
          "cost": 0.0
        },
        {
          "name": "Fine-tuning models",
          "cost": 0.0
        },
        {
          "name": "Embedding models",
          "cost": 0.0
        },
        {
          "name": "Base models",
          "cost": 0.0
        },
        {
          "name": "Image models",
          "cost": 0.0
        },
        {
          "name": "Audio models",
          "cost": 0.0
        }
      ]
    },
		...
    {
      "timestamp": 1709164800.0,
      "line_items": [
        {
          "name": "GPT-4 Turbo",
          "cost": 0.0
        },
        {
          "name": "GPT-4",
          "cost": 0.0
        },
        {
          "name": "GPT-3.5 Turbo",
          "cost": 0.0
        },
        {
          "name": "Assistants API",
          "cost": 0.0
        },
        {
          "name": "Fine-tuning models",
          "cost": 0.0
        },
        {
          "name": "Embedding models",
          "cost": 0.0
        },
        {
          "name": "Base models",
          "cost": 0.0
        },
        {
          "name": "Image models",
          "cost": 0.0
        },
        {
          "name": "Audio models",
          "cost": 0.0
        }
      ]
    }
  ],
  "total_usage": 2358.63770000000001
}