View the complete source code on GitHub.
Prerequisites
- Flowglad account with API key
- TanStack Start project with authentication (this example uses Better Auth)
- Node.js 21+ or Bun
Project Structure
Key Concepts
This pricing model combines three billing mechanisms:- Subscription tiers - Monthly plans at fixed prices ($10, $30, $60/mo)
- Usage credit grants - Each tier includes credits that renew each billing period (200, 360, 750 generations)
- One-time top-ups - Customers can purchase additional credits when they run out
Implementation
Pricing Configuration
Thepricing.yaml file defines subscription tiers, credit allocations, and top-up products. See the full configuration in the repository.
pricing.yaml is renewalFrequency: use "every_billing_period" for subscription credits that reset monthly, and "once" for top-up credits that are consumed permanently.
Checking Usage Balance
UsecheckUsageBalance to display remaining credits and gate access to generation features.
src/routes/index.tsx
Recording Usage
When a customer generates content, record the usage event. This decrements their credit balance. The SDK’screateUsageEvent method on the useBilling() hook handles the server communication automatically.
src/routes/index.tsx
subscriptionId from the customer’s current subscription.
Checking Feature Access
Toggle features let you differentiate plans beyond just credit amounts. UsecheckFeatureAccess to check if a user has access to gated premium features.
src/routes/index.tsx
true if the customer’s current subscription includes the feature, false otherwise.
Purchasing Top-Up Credits
When customers run low on credits, let them purchase additional credits without changing their subscription.src/routes/index.tsx