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 provides a base subscription with included credits that reset each billing cycle. When customers exceed their included credits, additional usage is billed at a per-unit rate (overage). The model works well for:- API platforms where most users stay within limits but power users need flexibility
- Products that want to offer a free tier with limited usage
Implementation
Pricing Configuration
Thepricing.yaml defines subscription tiers with included credits and an overage product for additional usage. See the full configuration in the repository.
pricing.yaml is renewalFrequency: "every_billing_period" for subscription credits that reset monthly. The overage product uses type: "usage" to bill per-request when credits are exhausted.
Checking Usage Balance
UsecheckUsageBalance to display remaining credits and determine if the customer can perform metered actions.
src/app/home-client.tsx
Recording Usage Events
When a customer uses a metered resource, record the event. This decrements their balance and, if credits are exhausted, triggers overage billing. 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. It defaults amount to 1 if not provided.
Checking Feature Access
Toggle features gate non-metered capabilities by subscription tier. UsecheckFeatureAccess to verify access.
src/app/home-client.tsx
true if the customer’s subscription includes the feature.
Creating Checkout Sessions
When customers want to subscribe or upgrade, create a checkout session that redirects to Flowglad’s hosted checkout.src/components/pricing-card.tsx