Python 88.3%
TypeScript 7.6%
Shell 4.1%
1/**2 * Admin API read-only tour with the official Node SDK (openai@7.18): users, projects, audit logs, usage, costs.3 * STATUS: ACCOUNT_RESTRICTED — run 2026-09-18 with our project key: every call rejected with4 * PermissionDeniedError 403 "Missing scopes: api.management.read"/"api.usage.read" (audit logs: AuthenticationError 401).5 * With OPENAI_ADMIN_KEY set the same code prints real data. Never mutates anything.6 * Run: node --env-file=.env examples/openai/admin/admin_readonly.ts7 */8import OpenAI from "openai";9import { appendFileSync } from "node:fs";1011const client = new OpenAI({ adminAPIKey: process.env.OPENAI_ADMIN_KEY ?? process.env.OPENAI_API_KEY, maxRetries: 0 });12const start = Math.floor(Date.now() / 1000) - 7 * 86400;13const mask = (s: string) => s.replace(/sk-(ant-)?[A-Za-z0-9_-]{8,}/g, "sk-***REDACTED***");14const log = (method: string, path: string, status: number, note: string) =>15 appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "openai", method, path, status, est_cost_usd: 0, note: mask(note) }) + "\n");1617async function attempt(label: string, path: string, fn: () => Promise<unknown>) {18 try {19 const out = await fn();20 log("GET", path, 200, `admin example ${label}`);21 console.log(200, label, JSON.stringify(out).slice(0, 200));22 } catch (e) {23 if (e instanceof OpenAI.APIError) {24 log("GET", path, e.status ?? 0, `admin example ${label}: ${e.constructor.name}`);25 console.log(e.status, label, e.constructor.name, `code=${e.code}`, `requestID=${e.requestID}`, mask(String(e.message)).slice(0, 160));26 } else throw e;27 }28}2930await attempt("users", "/v1/organization/users", async () => (await client.admin.organization.users.list({ limit: 5 })).data.map((u) => u.email));31await attempt("projects", "/v1/organization/projects", async () => (await client.admin.organization.projects.list({ limit: 5 })).data.map((p) => [p.id, p.name, p.status]));32await attempt("audit_logs", "/v1/organization/audit_logs", async () => (await client.admin.organization.auditLogs.list({ limit: 5 })).data.map((a) => a.type));33await attempt("usage.completions", "/v1/organization/usage/completions", async () =>34 (await client.admin.organization.usage.completions({ start_time: start, bucket_width: "1d", group_by: ["model"], limit: 7 })).data.map((b) => [b.start_time, b.results.length]));35await attempt("costs", "/v1/organization/costs", async () =>36 (await client.admin.organization.usage.costs({ start_time: start, bucket_width: "1d", group_by: ["line_item"], limit: 7 })).data.map((b) => [b.start_time, b.results.length]));37