Time: about 20 minutes · Level: beginner · You’ll build: a usage-billed API with one plan, one customer, and a verified usage record.
Scenario
You run a search API. Each request costs $0.001, and customers pay monthly for what they use. By the end of this tutorial, every request your API serves will be recorded against the right customer, and you’ll be able to see the usage on their account.Prerequisites
- A sandbox workspace and a
developerAPI key. See Quickstart if you need one. - Node.js 20 or later, to run the script in step 5.
curlandjqin your terminal.
1
Define what you're measuring
A metric counts the events you care about. Start with a count of successful search requests.The
filter means failed requests (for example, 500) are not billed.2
Set a price for the metric
A unit price charges the same amount for each unit. This one is $0.001 per request, billed monthly in arrears.
3
Bundle the price into a plan
4
Create a customer and subscribe them
Use your own customer ID so your application and Orvlin share one identifier.You should see a subscription with
"status": "active", or "trialing" if the plan has a trial.5
Record usage from your application
Every billable request should produce one event, with an idempotency key tied to the request. Here is the pattern in Node.js. Save it as Run it:The
meter.mjs:eventName must match what your metric counts. Here it is search_request, and the metric counts events where statusCode is 200. The 500 request is recorded but is not billed.6
Confirm the usage
List the events for the customer:You should see
3. Next, check what the customer can use:What you built
- A metric that counts successful requests.
- A unit price of $0.001 per request.
- A plan the customer subscribes to.
- A usage pipeline that is safe to retry.
Next steps
Gate features with entitlements
Stop requests when a customer runs out of allowance.
Test billing with test clocks
Move time forward and see the first invoice.