#In python, how do I use the API to get the amount of moderation endpoint requests in the past month?
1 messages · Page 1 of 1 (latest)
Hi @pulsar lava. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To get the number of moderation endpoint requests in the past month using Python, you should use the Moderations Usage API. Here’s a step-by-step example based on the official documentation:
import requests
import time
import os
# Set your OpenAI Admin API Key
OPENAI_ADMIN_KEY = os.environ.get("OPENAI_ADMIN_KEY")
# Calculate start time: 30 days ago from now (in Unix seconds)
days_ago = 30
start_time = int(time.time()) - (days_ago * 24 * 60 * 60)
# Define the Moderations Usage API endpoint and parameters
url = "<https://api.openai.com/v1/organization/usage/moderations">
params = {
"start_time": start_time,
"bucket_width": "1d", # daily buckets
"limit": 30 # 30 days
}
headers = {
"Authorization": f"Bearer {OPENAI_ADMIN_KEY}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers, params=params)
data = response.json()
# Sum up the number of moderation requests over the period
total_requests = 0
for bucket in data.get("data", []):
for result in bucket.get("results", []):
total_requests += result.get("num_model_requests", 0)
print(f"Total moderation endpoint requests in the past month: {total_requests}")
Why might the API and dashboard show different values?
The knowledge sources indicate that minor discrepancies can occur between API usage endpoints and the dashboard due to differences in how usage and spend are recorded, or due to data latency and aggregation logic. For financial reconciliation, OpenAI recommends using the Costs endpoint or the Costs tab in the dashboard, but for raw usage counts, the API should be accurate for technical reporting. If you see significant mismatches, consider possible delays (data may be delayed by up to 24 hours) or differences in how the dashboard aggregates or filters data compared to the API response. If the discrepancy persists, contacting OpenAI support is recommended for further investigation Usage API docs Usage API vs Dashboard.
References:
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@pulsar lava Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@stoic oasis Thanks, but please make it so that it doesnt get the key from .env, but a string pasted at end, as it is run purley locally, thus no security issues
Let me check the knowledge base for you again. One moment! :books:
Certainly! You can provide your OpenAI Admin API key directly as a string in your script. Here’s how you can do it, based strictly on the official API documentation and examples from the knowledge sources:
import requests
import time
# Paste your OpenAI Admin API key here
OPENAI_ADMIN_KEY = "sk-..." # <-- Replace with your actual key
# Calculate start time: 30 days ago from now (in Unix seconds)
days_ago = 30
start_time = int(time.time()) - (days_ago * 24 * 60 * 60)
# Define the Moderations Usage API endpoint and parameters
url = "<https://api.openai.com/v1/organization/usage/moderations">
params = {
"start_time": start_time,
"bucket_width": "1d", # daily buckets
"limit": 30 # 30 days
}
headers = {
"Authorization": f"Bearer {OPENAI_ADMIN_KEY}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers, params=params)
data = response.json()
# Sum up the number of moderation requests over the period
total_requests = 0
for bucket in data.get("data", []):
for result in bucket.get("results", []):
total_requests += result.get("num_model_requests", 0)
print(f"Total moderation endpoint requests in the past month: {total_requests}")
This approach does not use environment variables or .env files; you simply paste your API key as a string in the script. This is consistent with the usage patterns shown in the knowledge sources, where the API key can be set directly in code for local, non-production use Moderations Usage API Reference.