Skip to main content

What you’ll build

This tutorial builds metered billing end to end for an API product that charges per API call. By the end you’ll have a plan with a base fee plus metered usage, a stream of recorded usage events, a windowed usage query for charts, and a per-subscription usage view your dashboard can show to customers. Everything here uses real endpoints. Follow along against a local make demo stack (http://localhost:8080, key sk_test_12345) or substitute https://api.recurso.dev and your own key.
Recurso stores and aggregates usage; the rating (turning quantity into a charge) happens at invoice time from the plan’s tiers. Enforcing limits in real time is your application’s job — see feature gating.

Step 1: Create a metered plan

A metered plan is three calls: the plan (flat base fee), a billable metric (the meter), and the charges that price the metric on the plan — the most common SaaS model: predictable revenue plus a usage upside.
1

Create the base plan

2

Define the billable metric

The metric’s code doubles as the usage event dimension it aggregates:
3

Attach graduated charges to the plan

First 10k calls included, overage at ₹0.05 then ₹0.01 — rates are decimal strings, so sub-paise pricing is exact.
graduated tiers charge each tier’s rate across its own range; volume tiers apply the final tier’s rate to all units. See Usage-Based Billing for all four models priced against the same month.
Then create a customer and subscribe them to this plan — see the end-to-end flow if you need the exact calls. Keep the resulting subscription_id and customer_id; you need both to record usage.

Step 2: Record usage events

Report usage against the subscription’s dimension throughout the period. All four fields are required, and the record call is ownership-checked: the subscription must belong to your tenant and customer_id must match the subscription’s customer.
A successful call returns { "status": "recorded", "event_id": "..." }. The event timestamp is set server-side at ingestion.
Report usage frequently (e.g. hourly batches) rather than one giant call at period end — it keeps the customer-facing usage view accurate and spreads load. If a 404 comes back, the subscription isn’t in your tenant; a 400 means customer_id doesn’t match the subscription’s customer.

Step 3: Query windowed usage

GET /v1/usage aggregates events into day or month buckets over an arbitrary window — this is the endpoint behind a usage chart. The window defaults to the last 30 days.
To discover which meters your services have reported, list the tenant’s dimension catalog with recurso.usage.dimensions() (GET /v1/usage/dimensions).

Step 4: Show customers their usage

GET /v1/subscriptions/{id}/usage answers “you’ve used 4,231 of 10,000 API calls this month.” It returns each dimension’s current-period and lifetime totals — and if you attach an entitlement limit whose feature_key equals the dimension name, the limit and remaining headroom are joined in automatically.
Node

Step 5: Bill on usage

You don’t call anything to invoice. At the end of each billing period Recurso aggregates the recorded events and rates them against the plan’s usage tiers, producing invoice line items automatically: You’ll receive the invoice.created webhook when the invoice is generated and invoice.paid once it’s collected. See the end-to-end flow for the full webhook sequence.
Always attach an Idempotency-Key to any request your client might retry, so a network blip can’t double-record usage or double-charge. See Idempotency.

Next Steps

Usage-Based Billing

Full pricing-model reference and the metering + entitlements pattern

Feature Gating

Enforce plan limits and boolean features at request time

Record Usage Event API

Endpoint reference with ownership rules

Analytics

MRR and usage analytics