SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
14 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.1 KB · 31 lines typescript
Raw Blame History
1// Admin API read-only probes with the official TypeScript SDK (client.beta.organization.*).2// STATUS: organization.retrieve() → LIVE_VERIFIED 2026-09-18 with the REGULAR key (200).3//         usage/cost/claude_code reports → ACCOUNT_RESTRICTED with a regular key (401 "The Admin API requires an Admin API key4//         or an organization-scoped API key."); executed for real only when ANTHROPIC_ADMIN_KEY is set in .env.5// Run: node --env-file=.env --experimental-strip-types examples/anthropic/admin/org_me_and_usage_report.ts6import Anthropic from "@anthropic-ai/sdk";78const client = new Anthropic(); // ANTHROPIC_API_KEY (or ANTHROPIC_AUTH_TOKEN for an org:admin OAuth token)9const org = await client.beta.organization.retrieve();10console.log("organization:", org.id, org.name);1112const adminKey = process.env.ANTHROPIC_ADMIN_KEY;13const start = new Date(Date.now() - 7 * 86_400_000).toISOString().slice(0, 10) + "T00:00:00Z";14const day = new Date(Date.now() - 86_400_000).toISOString().slice(0, 10);1516if (!adminKey) {17  console.log("ANTHROPIC_ADMIN_KEY not set — reports are ACCOUNT_RESTRICTED with a regular key (401). Skipping.");18} else {19  // The Node SDK surface (sources/anthropic/openapi/node-sdk-api.md) exposes no usage/cost report methods yet → raw fetch.20  const get = async (path: string) => {21    const res = await fetch(`https://api.anthropic.com${path}`, { headers: { "x-api-key": adminKey, "anthropic-version": "2023-06-01" } });22    return { status: res.status, body: (await res.json()) as any };23  };24  const usage = await get(`/v1/organizations/usage_report/messages?starting_at=${start}&bucket_width=1d&group_by[]=model&group_by[]=service_tier`);25  console.log("usage_report/messages", usage.status, JSON.stringify(usage.body).slice(0, 400));26  const cost = await get(`/v1/organizations/cost_report?starting_at=${start}&group_by[]=workspace_id`);27  console.log("cost_report", cost.status, JSON.stringify(cost.body).slice(0, 400));28  const cc = await get(`/v1/organizations/usage_report/claude_code?starting_at=${day}&limit=5`);29  console.log("usage_report/claude_code", cc.status, JSON.stringify(cc.body).slice(0, 400));30}31