How to add billing and usage metering to a SaaS app
Charging for a product is two problems that look like one. Billing decides what a customer owes. Entitlement decides what they may do. Most billing bugs come from letting the second be an accident of the first.
8 min read · Last checked
In short
Key points
- Keep a clear line between billing (money owed) and entitlement (what the customer may use now).
- Check entitlement in your backend before doing metered work, and cache the answer only for the time the platform allows.
- Record usage with an idempotency key so a retry cannot count twice.
- Tell customers before they hit a limit, not after.
Separate what a customer owes from what they can do
A subscription says a customer pays a certain amount each month. An entitlement says which features are switched on and what the limits are. Billing events feed the entitlement, but your product should only ever ask one question: what may this customer do right now?
If your code reads raw billing state in many places, every pricing change becomes a code change. If it asks one entitlement source, a price or plan change is configuration.
Choose the kinds of limits you need
- On or off: a feature is part of a plan or it is not, such as reports or an API.
- Counter: an amount used in a period that resets, such as API calls or messages sent each month.
- Gauge: an amount in use at a point in time that can go down, such as seats or projects. A gauge needs a way to release usage as well as consume it.
Record usage so that retries are safe
Networks fail and workers restart, so any call that records usage will sometimes be sent twice. Attach an idempotency key to each unit of work, for example the id of the request or job. The server recognises the second call with the same key as a replay and does not count it again.
Stripe’s documentation for usage-based billing describes the same pattern of recording usage events against a meter and billing from the totals. Whatever system holds the meter, the rule is the same: one real event, one key.
var context = await te.GetRuntimeContextAsync("my-app", "Production", tenantId, ct);
if (context is null || !context.Features.Contains("reports"))
return Results.StatusCode(StatusCodes.Status403Forbidden);
await te.ConsumeUsageAsync("my-app", tenantSlug, "Production", "reports",
amount: 1, idempotencyKey: $"report-{requestId}", ct: ct);
// requestId is the id of this request or job, so a retry is counted once.Cache entitlement for a known time, not forever
Calling the platform on every request is slow and fragile. Caching without a bound means a cancelled customer keeps access. Take the lifetime from the platform’s answer rather than inventing your own.
With TenancyEngine the runtime context says how long a snapshot may be used, never more than 300 seconds, and measures that from when your server received it. The snapshot also stops being valid at the next scheduled change, such as a trial ending. If the platform cannot be reached, decide in advance whether your feature fails open or closed, and write it down.
Warn before the limit, and react to plan changes
A customer who is cut off without warning opens a support ticket. A customer who is told at 80 percent upgrades. TenancyEngine sends a usage.near_limit webhook when a customer passes 80 percent of a limit and usage.at_limit when they reach it, once for each crossing. It sends entitlement.changed when a plan changes, so your app fetches the context again instead of polling.
The webhook article linked below covers how to receive those events safely.
Where the money side lives
On Growth and above, TenancyEngine connects the plans your app checks to subscription billing: hosted checkout, invoices, payment reminders and a customer billing portal, run by TenaBill. You can combine a flat subscription with metered usage, and give customers account credits. Because the plan decides entitlement, a payment failure and a plan change flow through the same single check in your code.
Honest limits
What to know before you rely on this
- Subscription billing starts on Growth. Starter has no subscription billing.
- TenancyEngine is not a merchant of record and does not file sales tax or do accounting.
- Free offerings can be assigned from your backend. A paid plan always goes through checkout and cannot be started with an API key alone.
Questions people ask
What happens if a retry sends the same usage twice?
Send the same idempotency key with the retry. The second call is recognised as a replay and does not count again.
How do I find out when a customer changes plan?
Subscribe a webhook endpoint to entitlement.changed. When it arrives, fetch the customer’s runtime context again.
Can I bill by usage and by subscription together?
Yes. Combine a flat subscription with metered usage, and give customers account credits, on Growth and above.
Sources
Keep reading
Try it on the free plan
One app and five customers, no credit card required.