Home / Market / platform oauth linked billing

platform oauth linked billing

Use when adding Claude Platform OAuth (linked billing) to an app, so that customers link their own Claude Console organization and the Claude calls they start are billed to them. Invoke it before writing any code for "connect your Claude org", "bill Claude usage to the customer", user profiles, auth.claude.com, PAR/PKCE with Anthropic, workload identity federation or billing_policy.

skill★ 348040 dlgithubMITInstall in CosmosUpstream Source

Documentation & Preview

--- name: platform-oauth-linked-billing description: Use when adding Claude Platform OAuth (linked billing) to an app, so that customers link their own Claude Console organization and the Claude calls they start are billed to them. Invoke it before writing any code for "connect your Claude org", "bill Claude usage to the customer", user profiles, auth.claude.com, PAR/PKCE with Anthropic, workload identity federation or billing_policy.

---

Claude Platform OAuth (linked billing)

Set up Claude Platform OAuth in the project you are running in, end to end: Console, signing key, backend routes, token storage and a verified billed call. Work in three phases and do not skip ahead: inspect, plan, execute.

<skill-dir> below is the folder that holds this file (usually .claude/skills/platform-oauth-linked-billing). The scripts need only Node 18+ on the machine running them, whatever language the backend is written in.

The feature is in beta. Tested with anthropic-version: 2026-10-21 and the beta header user-profiles-billing-2026-10-01. If the user has newer docs, they win: https://platform.claude.com/docs/en/manage-claude/oauth-apps/overview

What it is (tell the user if they are unsure)

If the user wants something this does not cover (end users paying with a Claude.ai plan, batches, billable code execution), say so before doing any work.

Phase 1: Inspect the project

Read, do not change anything. Find and note:

| Question | Where to look | |----------|---------------| | Language and framework of the server code | package.json, pyproject.toml, requirements.txt, go.mod, Gemfile, composer.json, Cargo.toml | | Where server code runs | wrangler.toml (Cloudflare Workers), vercel.json, netlify.toml, Dockerfile, fly.toml, serverless.yml, app.yaml | | Is there a server at all | A static site or a mobile/desktop app with no backend cannot hold the key: the plan must add one (an edge function or small service) | | Existing users and sessions | Auth libraries (Clerk, Auth.js/NextAuth, Supabase Auth, Lucia, Devise, Django auth, Passport) and the user table or id | | Database for per-user state | ORM schema, migrations folder, DATABASE_URL-style env names | | Secret store | .env.example, .dev.vars, wrangler secret, Vercel/Netlify env, Doppler, AWS/GCP secret managers. Read names only | | Existing Anthropic usage | @anthropic-ai/sdk, anthropic (Python), raw calls to api.anthropic.com, current API key handling | | Public URL and local dev URL | Deploy config, README, dev server port. The callback URL is built from these |

Rules while inspecting:

Phase 2: Write the plan

Write CLAUDE_PLATFORM_OAUTH_PLAN.md at the project root (ask before committing it) with:

  1. Summary of what was found in Phase 1: stack, runtime, user model, database, secret store, public and local URLs.
  2. Console checklist (the user does these; see references/console-setup.md). Fill in the real values you can already know: app name, redirect URIs, subjects (<app>:backend:client, <app>:backend:operator), audiences.
  3. Secrets the backend will need, with the exact names you will use and where each one is stored: signing private key (PEM), key id (kid), issuer URL, client subject, operator subject, client ID, organization ID, operator service account ID, operator federation rule ID, and a key to encrypt refresh tokens at rest.
  4. Data model: a per-user record with user_profile_id, connection_id, encrypted refresh token and its expiry, link status; a short-lived table or cache for pending link attempts (state, PKCE verifier, browser binding hash, user id, expiry 10 to 15 minutes).
  5. Backend work, as files to create or change for this stack (see references/backend.md): signer, operator token cache, user profile creation, link/start, link/callback, unlink/status, and the billed call wrapper.
  6. Verification steps (see the end of this file).
  7. Open questions for the user, only the ones you cannot answer from the code.

Show the plan to the user and wait for an explicit go before Phase 3. If they change scope, update the file.

Phase 3: Execute

Go in this order. Stop at each step that needs the user and say exactly what they must do.

3.1 Signing key

Run scripts/genkey.mjs with an --out path outside the repository (it refuses a path git would track):

node <skill-dir>/scripts/genkey.mjs --prefix <app>-backend --out <path-outside-repo>/priv.pem

It writes the private key with mode 0600 and prints only the public JWKS array and the kid. Never print, cat or paste the private key into the chat. Tell the user to load it into the secret store themselves (for example wrangler secret put SIGNING_KEY_PEM < priv.pem) and delete the file after.

For production use a fresh key, never one that was used in a test run.

3.2 Console setup (user)

Walk the user through references/console-setup.md one step at a time and wait for confirmation after each. Check the gotchas with them as you go: they are each a failed request otherwise. If you have browser tools and the user is signed in to Console, you may navigate and read the pages for them, but the user clicks Create/Save, accepts terms and handles sign-in. Never type secrets into forms.

Collect the public values they give you (client ID, organization ID, service account ID, operator rule ID, issuer URL) and put them in the secret store or config, not in code.

3.3 Backend

Implement what the plan lists, following references/backend.md and the project's own conventions (router, ORM, error handling, test framework). Requirements that do not change with the stack:

3.4 Verify

Run scripts/verify.mjs with the same values the backend uses. The user exports them in their own shell (the variable names are at the top of the script); never write them to a file you create. It prints no key or token; between link and exchange it keeps the PKCE verifier in ~/.cache/platform-oauth-linked-billing/ (mode 600, deleted by exchange). If the default model id is rejected, set CPO_MODEL.

node <skill-dir>/scripts/verify.mjs operator            # key, issuer, operator rule and service account match
node <skill-dir>/scripts/verify.mjs profile "Test user" # creates a user profile, prints uprof_…
node <skill-dir>/scripts/verify.mjs bill uprof_…        # 403 user_profile_billing_not_authorized before linking
node <skill-dir>/scripts/verify.mjs link uprof_…        # client rule + PAR; prints the URL for the test customer
node <skill-dir>/scripts/verify.mjs exchange <code>     # code exchange (within 5 minutes of approval)
node <skill-dir>/scripts/verify.mjs bill uprof_…        # 200 after linking

link/exchange let you prove the Console setup before the backend exists (use a localhost redirect URI with nothing listening and copy code from the address bar).

Then the end-to-end check with the app running:

  1. The test user (a second Claude account, added as a test user) starts the link from the app, sees the three screens (how billing works, monthly spend limit, link and authorize) and approves.
  2. The callback stores the connection; the app's Overview tab in Console shows the linked customer.
  3. verify.mjs bill <uprof> (or the app's own action) returns 200, and the usage appears in the customer's organization.
  4. Negative checks: opening the authorize URL in another browser fails the callback; a profile that never linked gets 403 user_profile_billing_not_authorized.

Report the results with the exact responses. If something fails, use references/troubleshooting.md before changing code.

Before going live