How to implement usage based billing on Stripe
Usage-based billing on Stripe requires creating metered products with usage records that track consumption. You'll set up subscription items with metered billing, report usage via API calls, and configure automated invoicing.
Prerequisites
- Active Stripe account with API keys
- Basic understanding of webhooks
- Development environment with API access
- Product catalog already created
Step-by-Step Instructions
Create a metered product and price
Set up the subscription with metered items
Implement usage tracking in your application
stripe.subscriptionItems.createUsageRecord(
'si_subscription_item_id',
{
quantity: 100,
timestamp: Math.floor(Date.now() / 1000),
action: 'increment'
}
);Track usage events in real-time or batch them for periodic reporting.Configure usage aggregation and billing
Set up webhook endpoints for usage events
invoice.created, invoice.payment_succeeded, invoice.payment_failed, and customer.subscription.updated. Click Add endpoint and note your webhook signing secret for verification.Test usage reporting and billing
stripe usage-records create \
--subscription-item=si_test_item \
--quantity=50 \
--timestamp=$(date +%s)Check Billing > Usage records to verify data appears correctly. Create a test invoice preview to confirm usage calculations.Monitor and optimize usage billing
Common Issues & Troubleshooting
Usage records not appearing on invoices
Verify the timestamp falls within the current billing period and the subscription item ID is correct. Check that usage aggregation is set to sum not last_during_period for cumulative billing.
Duplicate usage charges appearing
Implement idempotency keys when creating usage records: idempotency_key: 'usage_' + unique_event_id. Check your webhook handling doesn't create duplicate records from retry events.
Customer invoices showing zero usage
Confirm usage records are submitted before the billing period ends. Check the subscription item status is active and verify API authentication has correct permissions for usage record creation.
Usage billing amounts incorrect
Review your price aggregation method in the product settings. Verify unit prices match your pricing model and check for timezone issues in timestamp calculations that might assign usage to wrong billing periods.