Build
Billing and credits
Stripe or Polar, plans, one-time products and a credit ledger.
01Choose a provider
One provider per deployment. Without keys, BILLING_PROVIDER resolves to none: plans and credits still work and checkout explains payments are off.
# Polar
BILLING_PROVIDER=polar
POLAR_SERVER=sandbox
POLAR_ACCESS_TOKEN=...
POLAR_WEBHOOK_SECRET=...
# or Stripe
BILLING_PROVIDER=stripe
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...02Pricing models
| Model | Set up with |
|---|---|
| Flat subscription | A plan in config/plans.ts with billingType flat and a monthly (and yearly) price |
| Per-seat | billingType per_seat; quantity follows member count |
| Free trial | trialDays on the plan, offered once per workspace; plan credits start with the first paid invoice |
| One-time purchase | An entry in products with a one-time price |
| Credits | monthlyCredits per paid invoice (times seats on per-seat plans, times 12 on yearly), plus credit packs |
03Webhooks
Put each plan's price or product ID in config/plans.ts and run npm run db:seed, then register the webhook: /api/webhooks/polar or /api/webhooks/stripe. For Stripe, subscribe to customer.subscription.*, checkout.session.completed, invoice.paid and charge.refunded. Locally:
stripe listen --forward-to localhost:3000/api/webhooks/stripeWebhooks re-read state from the provider and are idempotent, so retries are safe.
04Credits and plan gates
Credits only move through grantCredits and spendCredits, each with an idempotency key; never write the ledger directly. Gate paid features with requirePlan from features/billing/server/subscriptions.ts.