Skip to main content
Every PymtHouse integration is some combination of three primitives: machine auth (your backend talks to PymtHouse), user provisioning (you register end-users), and JWT issuance (you get signed tokens that gate AI requests). What varies is how users get into the system and how you charge them.

Setup: install the SDK and configure env vars

All TypeScript examples below use the official Builder SDK. Install it first:
Set these four environment variables in your backend. Never expose the M2M credentials client-side.
See the Builder SDK reference for the full API, subpath exports, and method signatures.
Then construct the client once per request lifecycle (server-side only):
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

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 a 404:
The lazy pattern means you never need a separate sign-up hook — users are provisioned on their first request. If you prefer eager provisioning (e.g. to pre-allocate balance), call 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, use mintUserSignerToken from @pymthouse/builder-sdk/signer/server. This mints a short-lived signer JWT and returns the user’s balance in one call:
See Signer routing for the DMZ URL, webhook config, and identity webhook setup.

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 run my-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_…) has device_code grant enabled
  • device_third_party_initiate_login enabled on the public client
  • initiate_login_uri registered to your login handler’s HTTPS endpoint
  • M2M client has users:token or device:approve scope

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

Billing is async and metering-based — you do not need to hook into each individual request. The signer records usage automatically.

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.
Publishing with status: active syncs the plan to OpenMeter automatically.

Step 3: Connect Stripe for payment collection

To collect payment from users, connect a Stripe account:
Complete the Stripe onboarding at the returned URL. Once 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:
Redirect the user to 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. Set ACTIVATION_GATE_MODE in your environment:
  • 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