Skip to main content

Usage-Based Billing

Track API usage per customer for metered billing with Unkey. Use the remaining credits feature to count requests and enforce usage caps.
1 min read

Charge customers based on how much they use your API. This recipe shows how to track usage with Unkey's credits system and integrate with billing providers like Stripe.

The pattern#

Full implementation#

Setting up usage tracking#

API route with usage tracking#

Stripe integration for overages#

Usage dashboard endpoint#

Plan upgrades#

When a customer upgrades, update their key:

Multi-resource tracking#

Track different types of usage separately:

Best practices#

Use refill wisely

Monthly refills keep usage tracking simple. For different billing cycles, adjust the interval.

Handle overages gracefully

Don't just cut users off. Offer overage billing or soft limits with warnings.

Show usage prominently

Let users see their usage in your dashboard. Nobody likes surprise bills.

Sync with your billing provider

Use webhooks to keep Stripe/Paddle usage records in sync with Unkey.

Next steps#