Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32"""Admin API read-only probes with the official Python SDK (client.beta.organization.*).34STATUS: organization.retrieve() (GET /v1/organizations/me) → LIVE_VERIFIED 2026-09-18 with the REGULAR key (200).5 usage_report / cost_report / claude_code → ACCOUNT_RESTRICTED with a regular key6 (401 "The Admin API requires an Admin API key or an organization-scoped API key."); they run for real only when7 ANTHROPIC_ADMIN_KEY is set in .env (the SDK also honours ANTHROPIC_AUTH_TOKEN for org:admin OAuth tokens).8Run: .venv/bin/python examples/anthropic/admin/org_me_and_usage_report.py9"""10from __future__ import annotations11import os, sys12from datetime import datetime, timedelta, timezone13from pathlib import Path1415ROOT = Path(__file__).resolve().parents[3]16sys.path.insert(0, str(ROOT))17from scripts.live import load_env, log_request, anthropic_request # noqa: E4021819load_env()20import anthropic # noqa: E4022122# 1. org info — any key kind23client = anthropic.Anthropic()24org = client.beta.organization.retrieve()25log_request("anthropic", "GET", "/v1/organizations/me", 200, 0, "sdk admin org me")26print("organization:", org.id, org.name)2728# 2. Admin endpoints — use the admin key if present; otherwise show the 401 honestly via the raw helper29admin_key = os.environ.get("ANTHROPIC_ADMIN_KEY")30start = (datetime.now(timezone.utc) - timedelta(days=7)).strftime("%Y-%m-%dT00:00:00Z")31day = (datetime.now(timezone.utc) - timedelta(days=1)).strftime("%Y-%m-%d")3233# NOTE: the Python SDK surface (sources/anthropic/openapi/python-sdk-api.md) exposes client.beta.organization.retrieve()34# but no usage_report / cost_report methods yet, so the reports use the raw helper (admin=True → ANTHROPIC_ADMIN_KEY).35paths = (f"/v1/organizations/usage_report/messages?starting_at={start}&bucket_width=1d&group_by[]=model&group_by[]=service_tier",36 f"/v1/organizations/cost_report?starting_at={start}&group_by[]=workspace_id",37 f"/v1/organizations/usage_report/claude_code?starting_at={day}&limit=5")38for path in paths:39 st, body, _ = anthropic_request("GET", path, admin=bool(admin_key), note="admin example report" + (" (admin key)" if admin_key else " (regular key)"))40 if st == 200:41 print(st, path.split("?")[0], "buckets/records:", len(body.get("data", [])), "next_page:", body.get("next_page"))42 for bucket in body.get("data", [])[:2]:43 print(" ", bucket.get("starting_at") or bucket.get("date"), str(bucket.get("results", bucket))[:300])44 else:45 print(st, path.split("?")[0], "->", body.get("error", {}).get("message") if isinstance(body, dict) else body)46if not admin_key:47 print("Set ANTHROPIC_ADMIN_KEY (sk-ant-admin01-…) in .env to run the reports for real (they are ACCOUNT_RESTRICTED with a regular key).")48