Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Admin API read-only tour with the official Python SDK (openai 3.16.2): users, projects, audit logs,2usage (completions by model, daily) and costs. Never mutates anything.3STATUS: ACCOUNT_RESTRICTED — run 2026-09-18 with our project key (no Admin key available): every call raised4openai.PermissionDeniedError (HTTP 403, "Missing scopes: api.management.read" / api.usage.read) and audit logs5raised AuthenticationError (HTTP 401, "Missing scopes: api.audit_logs.read"). With OPENAI_ADMIN_KEY set the6same code prints real data.7Run: .venv/bin/python examples/openai/admin/admin_readonly.py8"""9from __future__ import annotations1011import os12import sys13import time14from pathlib import Path1516sys.path.insert(0, str(Path(__file__).resolve().parents[3]))17from scripts import live # noqa: E402 loads .env, gives log_request1819import openai # noqa: E40220from openai import OpenAI # noqa: E4022122client = OpenAI(admin_api_key=os.environ.get("OPENAI_ADMIN_KEY") or os.environ["OPENAI_API_KEY"], max_retries=0)23start = int(time.time()) - 7 * 86400242526def attempt(label: str, method: str, path: str, fn):27 try:28 out = fn()29 live.log_request("openai", method, path, 200, 0, f"admin example {label}")30 print(f"200 {label}: {out}")31 except openai.APIStatusError as e: # PermissionDeniedError(403) / AuthenticationError(401) / NotFoundError...32 live.log_request("openai", method, path, e.status_code, 0, f"admin example {label}: {type(e).__name__}")33 print(f"{e.status_code} {label}: {type(e).__name__} code={e.code!r} request_id={e.request_id} :: {live.mask(str(e.body))[:160]}")343536attempt("users", "GET", "/v1/organization/users", lambda: [u.email for u in client.admin.organization.users.list(limit=5).data])37attempt("projects", "GET", "/v1/organization/projects",38 lambda: [(p.id, p.name, p.status) for p in client.admin.organization.projects.list(limit=5).data])39attempt("audit_logs", "GET", "/v1/organization/audit_logs",40 lambda: [(a.type, a.effective_at) for a in client.admin.organization.audit_logs.list(limit=5).data])41attempt("usage.completions", "GET", "/v1/organization/usage/completions",42 lambda: [(b.start_time, [(r.model, r.input_tokens, r.output_tokens) for r in b.results])43 for b in client.admin.organization.usage.completions(start_time=start, bucket_width="1d", group_by=["model"], limit=7).data])44attempt("costs", "GET", "/v1/organization/costs",45 lambda: [(b.start_time, [(r.line_item, r.amount.value) for r in b.results])46 for b in client.admin.organization.usage.costs(start_time=start, bucket_width="1d", group_by=["line_item"], limit=7).data])47