Code kit
Stripe usage-based billing, done correctly, in an afternoon
A drop-in, event-sourced credit ledger plus a correct 2026 Stripe Billing Meter integration for Next.js AI/SaaS products. Zero runtime dependencies, one-time $99, you own the code — no hosted ledger, no monthly fee.
Zero runtime dependencies. 18/18 tests passing. Instant download after purchase.
What's in the box
lib/credit-ledger.js
Event-sourced credit ledger. Balance is derived by replaying grant/spend events, not stored as a mutable counter, so retried webhooks and retried requests (same idempotency key) are no-ops instead of double-grants or double-spends.
lib/stripe-meter.js
Reports usage to Stripe's Meter Events API with the correct 2026 request shape (POST /v1/billing/meter_events), plus a reconcileToStripe() helper to drain unreported ledger spends in a batch from a cron or queue.
lib/low-balance-email.js
Resend-based low-balance alert with a cooldown so a customer running low doesn't get paged five times in an hour.
examples/webhook-route.js
A Next.js webhook handler that also fixes a separate, common bug: handling customer.subscription.updated (not just .deleted) for access revocation, since a card decline often moves status via .updated without ever firing .deleted.
Frequently asked
Why not just use Stripe's usage_type: "metered" price?
That approach was retired as of API version 2025-03-31.basil. Every metered price now needs a backing Meter object, and usage is reported through the separate, asynchronous Meter Events API — most tutorials still online show the old, broken flow.
Why do I need my own ledger if Stripe already has a Meter?
Stripe's Meter Events API doesn't confirm synchronously that an event was accepted. If you gate feature access on that confirmation, you either block users on a round-trip that isn't meaningful, or let usage through before you know it will bill correctly. Keeping your own source-of-truth ledger for access control (instant, in your own database) and reporting to Stripe separately for invoicing is the fix — that split is the whole design of this kit.
Is this a hosted service?
No. It's source code you drop into your own Next.js app and own outright. Nothing calls home, no vendor lock-in, no per-wallet fee like the hosted alternatives.
What database does it use?
None built in — the ledger is storage-agnostic. Implement getEvents/appendEvent/hasEvent against Postgres, Supabase, Redis, or anything else. A working in-memory reference store is included for prototyping and tests.
What license is this under?
Commercial, single-project use. You can use it in as many features of your own product as you like; you can't resell the kit itself.