Setup: install the SDK and configure env vars
All TypeScript examples below use the official Builder SDK. Install it first:createPmtHouseClientFromEnv from @pymthouse/builder-sdk/env reads these same variables automatically but may not handle PYMTHOUSE_ALLOW_INSECURE_HTTP. Use the explicit constructor above for local development to ensure allowInsecureHttp is set correctly.Pick the pattern that matches your architecture:
- Pattern 1: SaaS app — per-user sessions — Users log into your web app; each user gets their own metered allowance.
- Pattern 2: CLI tool — device flow — Users authenticate your CLI with their browser; no password prompts.
- Pattern 3: Metered billing — charge users — Collect payment, set usage limits, and sell plan upgrades.
Pattern 1: SaaS app — per-user sessions
When to use: Your app has logged-in users. You want to issue a fresh JWT for each user session and track usage per user for billing. What you’ll have after: Every API request carries a user-scoped JWT. Usage is attributed to individual users. You can show per-user billing dashboards.Architecture
Step 1: Mint a user JWT (lazy-provision pattern)
The most robust pattern — used by the reference dashboard — is to attempt the token mint first and only provision the user on a404:
upsertAppUser explicitly on sign-up:
Step 2: Gate requests on balance
Check the user’s balance before allowing the request. This prevents over-billing and gives you a clean error surface.Step 3: Show usage in your dashboard
Next.js example: API route
Signer sessions (Option A — clearinghouse direct mint)
If your integration talks directly to the remote signer DMZ rather than a gateway, usemintUserSignerToken from @pymthouse/builder-sdk/signer/server. This mints a short-lived signer JWT and returns the user’s balance in one call:
Pattern 2: CLI tool — device flow
When to use: You’re building a CLI, daemon, or any tool that runs in a terminal. Users should authenticate with their browser, not type in API keys. What you’ll have after: Users runmy-cli login, open a URL in their browser, and the CLI receives a signed session token without ever touching credentials.
Architecture
Step 1: CLI requests a device code
Step 2: CLI polls for the result
Start polling immediately and wait for the user to complete login:Step 3: Your backend binds the device grant (Option B)
When a user authenticates at your login page, your backend binds the pending device grant so the CLI poll receives the token. This is a two-step operation: mint a user JWT, then exchange it to bind the grant:The
user_code (e.g. ABCD-EFGH) is what the CLI receives and shows to the user. The device_code is a server-side opaque token — never display it. Pass user_code in the resource parameter when binding the grant.Requirements checklist
Before this works end-to-end:- Public client (
app_…) hasdevice_codegrant enabled -
device_third_party_initiate_loginenabled on the public client -
initiate_login_uriregistered to your login handler’s HTTPS endpoint - M2M client has
users:tokenordevice:approvescope
Pattern 3: Metered billing — charge users
When to use: You want to charge users based on their AI usage. You offer a free tier, paid plans, and want to collect payment via Stripe. What you’ll have after: Users auto-enroll in a free Starter plan. When they hit the limit, you can offer a paid plan and take payment via Stripe Connect.How billing works
Step 1: Every new user gets a free Starter plan
No action needed. PymtHouse auto-subscribes new users to the Starter plan on first provision. The Starter allowance is$5.00 by default.
To change the Starter allowance for all new users:
Step 2: Create a paid plan
Create a plan with a monthly subscription fee and included usage allowance. Done from the dashboard or via session-authenticated API.status: active syncs the plan to OpenMeter automatically.
Step 3: Connect Stripe for payment collection
To collect payment from users, connect a Stripe account:charges_enabled and details_submitted are true, paid plan checkout is unlocked.
Point a Stripe webhook at POST /webhooks/stripe with STRIPE_WEBHOOK_SECRET to keep Connect status in sync automatically.
Step 4: Let users upgrade and pay
Trigger a checkout session when a user wants to upgrade:checkoutUrl. After payment, their subscription in OpenMeter is updated automatically.
Step 5: Manual credit top-ups
Grant additional balance outside of the plan subscription — useful for support credits, trials, or promotions:Step 6: Show users their billing summary
Pull the full billing snapshot for your billing dashboard page:Activation gate modes
PymtHouse can automatically block new user provisioning or paid plan checkouts until Stripe Connect is ready. SetACTIVATION_GATE_MODE in your environment:
Related guides
- User management — full CRUD for provisioned users
- Allowances — balance reads, top-ups, and grant history
- Billing summary — response field reference and USD micro handling
- Plans — plan creation, OpenMeter sync, and phase-out
- Device flow — full device authorization reference
- Token exchange — RFC 8693 device binding and signer session exchange