This guide shows how to create a complete billing flow programmatically: define pricing, create a plan, embed checkout, and route billable traffic per customer.Documentation Index
Fetch the complete documentation index at: https://lava.so/docs/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites: Start with Authenticate an Agent, then follow Route Traffic for the gateway path you chose.
MCP flow
The Lava MCP server handles gateway traffic routing. Billing setup (meters, plans, checkout) requires the SDK. A typical billing workflow:- SDK: Create a meter, plan, and checkout session (Steps 1–3 below)
- MCP:
loginto authenticate the gateway session - MCP:
promptorcallto route billable traffic through the gateway
SDK flow
Overview
The billing flow has four parts:- Meter - Defines how usage is priced (e.g., $3 per million tokens)
- Plan - A subscription that includes an optional monthly fee, credit allowance, and linked meters
- Checkout - An embedded flow where your customer subscribes and pays
- Forward tokens - Per-customer auth tokens that route traffic through the gateway with billing
Step 1: Create a Meter
A meter defines the pricing rules for a type of usage. For example, charging per token for AI model calls:Meter options
| Parameter | Options | Description |
|---|---|---|
rate_type | fixed, percentage | Fixed price per unit, or percentage markup on base cost |
tier_type | tokens_1m, characters_1m, minutes, requests | What unit is being measured |
Step 2: Create a Plan
A plan ties together an optional recurring price, included credits, and one or more meters (product features included in your plan):The
meter_ids field links meters to the plan. You can link multiple meters to a single plan.Plan options
| Parameter | Options | Description |
|---|---|---|
billing_interval | day, week, month, year | How often the subscription renews |
rollover_type | full, none | Whether unused included credit carries over |
included_credit | Any decimal string | Credit included with each billing cycle |
credit_bundles | Array of bundles | Optional add-on credit packs customers can purchase |
Step 3: Embed Checkout
Checkout is a two-step flow: your backend creates a session, your frontend opens it with the@lavapayments/checkout SDK.
Backend: Create a session
Frontend: Open checkout
onSuccess fires with the customerId.
The
checkout_session_token is an opaque token — do not try to construct URLs from it. Always use the SDK to open checkout.Step 4: Route Billable Traffic
Once a customer has completed checkout, generate forward tokens scoped to their customer ID and meter. Every request made with this token is tracked and billed against their subscription:Step 5: Monitor Customer Usage
Track usage and costs per customer:What’s Next?
Meters
Advanced meter configuration and pricing strategies
Checkout Guide
Checkout modes, completion handling, and webhooks
Webhooks
Get notified when customers subscribe, run low on balance, or cancel
SDK Reference
Full API for customers, subscriptions, and usage