- Quantity - How much to grant
- Repeat - An optional refresh interval (e.g. grant again every month), or
never - Expires - When the grant expires:
never,when-purchase-expires, orwhen-repeated
Checking item balances
quantity- The raw balance (can be negative if you’ve consumed more than granted)nonNegativeQuantity-Math.max(0, quantity)for display purposes
app/components/credits-widget.tsx
Consuming credits (server-side)
When your app needs to consume credits (e.g. when a user sends an AI request), usetryDecreaseQuantity on the server. It’s a single transactional operation - it returns false and does nothing if the balance would go negative, so concurrent requests can’t overspend.
lib/credits.ts
Always use
tryDecreaseQuantity() instead of checking the balance and then decreasing. This prevents race conditions where multiple requests could consume more credits than available.credits.increaseQuantity(amount) or decrease without the safety check using credits.decreaseQuantity(amount). These consumption methods are server-only - client-side useItem returns a read-only balance.
Adjusting balances from the dashboard
In Payments -> Customers you can view item balances per customer and manually adjust quantities (plus or minus, with an optional description) - useful for support credits, comps, or corrections. The dashboard dialog does not set an expiration on those adjustments.Related
- Products & Pricing - attach items to a product
- Granting Products - grant a one-off bundle of items