Overview
The usage-based billing workflow in Retempo follows five stages:- Create a service and a USAGE_BASED plan — define your unit price and billing interval.
- Subscriber checks out — they complete a checkout session and a subscription is created.
- Record usage events — your system posts an event each time the subscriber consumes a unit.
- Create a billing invoice — at the end of the billing period, compute the total yourself and create an invoice with that amount.
- Settle the invoice — submit a settlement to record on-chain USDC payment proof.
Workflow
Create a USAGE_BASED payment plan
Before subscribers can check out, you need a service with a usage-based plan attached. Create the plan by posting to
/api/v1/services/:serviceId/plans. Set pricingType to USAGE_BASED, choose a billingInterval, and set amount to your unit price in USDC.In this example, the AI Token API charges $0.000010 USDC per token generated:Subscriber checks out
When a subscriber is ready to activate, create a checkout session for them. On successful payment, Retempo automatically creates an ACTIVE subscription tied to the plan.Redirect your subscriber to the
url returned in the response. Once they complete payment, Retempo creates the subscription and you receive a webhook notification. Store the resulting subscriptionId — you’ll need it to record usage events.You can only record usage events against a subscription in
ACTIVE status. Wait for the checkout webhook before you begin metering.Record usage events
Each time the subscriber consumes a unit of your service, post a usage event to Post as many usage events as needed across the billing period. Each event is stored independently, so you can record incremental bursts (e.g. 5,000 tokens per API response) rather than waiting for a large total to accumulate.
/api/v1/usage-events. Record events as they happen throughout the billing period — do not wait until billing time to report usage.Here’s an example recording 50,000 tokens generated by subscriber usr_b4e7f812:Create a billing invoice
At the end of the billing period, compute the total amount owed by summing the subscriber’s usage events, then create an invoice with that amount.Example calculation:Post the computed amount to
/api/v1/invoices. The paymentPlanId field is required and must reference the plan attached to this subscription:Settle the invoice
Once the subscriber pays, submit a settlement to record the on-chain USDC payment proof against the invoice. The After a successful settlement, the settlement status is
referenceHash is the bytes32 identifier for the on-chain settlement record. You must also supply wallet addresses for the payer and merchant:CONFIRMED and the invoice status updates to PAID. The on-chain transactionHash is stored as an immutable record. Your subscriber’s payment is now fully accounted for.Billing period management
Because USAGE_BASED plans bill at the end of a period rather than at checkout time, you are responsible for tracking per-period usage and triggering invoice creation at the right moment. A reliable approach for monthly billing:-
Track period boundaries. Store the subscription’s
currentPeriodStartandcurrentPeriodEnddates when the subscription is created (or renewed). These define the window for which you will bill. -
Aggregate usage within the period. Sum the
quantityvalues from all usage events recorded betweenperiodStartandperiodEndfor eacheventKey. Keep a running total in your own database so you can query it efficiently at billing time without re-fetching all events. -
Create the invoice at period end. When
currentPeriodEndarrives, compute the final total and post an invoice as shown in Step 4 above. Pass the correctpaymentPlanIdso the invoice is associated with the right plan. -
Settle immediately after payment. Once the subscriber’s USDC transfer confirms on-chain, post the settlement (Step 5). Retempo stores the
referenceHashandtransactionHashas immutable proof. -
Reset for the next period. After settling, reset your running aggregate counters for the new period starting at
currentPeriodEnd + 1.
If a subscriber cancels mid-period, pro-rate their invoice based on the number of days they were active, or bill for actual usage only — whichever matches your service terms. Retempo’s
amount field accepts any value you compute.