View the complete source code on GitHub.
Prerequisites
- Flowglad account with API key
- Next.js 15+ with App Router
- PostgreSQL database
Project Structure
Key Concepts
This pricing model has no recurring charges. Customers start on a free plan and purchase credit packs when they need them. Credits never expire and are consumed one-by-one as the customer uses your product. This model works well for products with unpredictable usage patterns or where customers prefer not to commit to a subscription. Common examples include API platforms, AI tools, and marketplace credits.Implementation
Pricing Configuration
Thepricing.yaml file defines the free plan, usage meter, and credit top-up product. See the full configuration in the repository.
renewalFrequency: "every_billing_period". All credits use renewalFrequency: "once", meaning they persist until consumed.
Checking Credit Balance
UsecheckUsageBalance to display remaining credits and determine if the customer can perform actions.
src/app/home-client.tsx
null if the customer has never purchased credits. Once they purchase a top-up, it returns an object that includes availableBalance.
Recording Usage
When a customer uses a credit, record the usage event. This decrements their balance. The SDK’screateUsageEvent method on the useBilling() hook handles the server communication automatically.
src/app/home-client.tsx
subscriptionId from the customer’s current subscription and defaults amount to 1 if not provided.
Purchasing Credits
When customers run out of credits (or want to stock up), let them purchase a top-up.src/app/home-client.tsx
billing.reload() to refresh the UI.
Gating Actions on Credit Balance
Block actions when the customer has no credits remaining.src/app/home-client.tsx