Skip to main content
Build one billable flow from your application and prove the amount. Your first check is complete when Lago’s usage quantity and amount on the Contract match your own calculation, and retrying the same event changes neither. You can do the repository work and local tests without a Lago account. Use the Lago CLI lago v2 commands for terminal work and the v2 OpenAPI contract for exact schemas.
API v2 is in beta. Endpoints return the X-Lago-Endpoint-Status: beta header.
The v2 setup follows one chain: billable metric → Product → Rate Card and Rates → Plan with applied Rate Cards → customer → Contract → events. Read How v2 differs from v1 if your agent knows the v1 model. v2 has no plan charges.

1. Map billing in your application

Tell your agent your pricing rules. Have it inspect the repository and identify:
  • Who pays, what action creates usage, and which saved record proves it happened.
  • Which system owns contracts, calculates prices, issues invoices, and collects payments today.
  • The customer, contract, and usage IDs that link that action to its bill.
  • One example bill and any risk of charging a customer twice.
Agree on the price and which existing customers, if any, will move to Lago before editing code. Prompt. Replace [pricing rules] with your billing period, currency, base fee, included usage, and overage rate:

2. Build and test locally

Once you agree on the billing plan, have your agent:
  • Map the app’s customer and contract IDs to a Lago customer external_id and a Contract external_id.
  • Send usage server-side to POST /api/v2/events or POST /api/v2/events/batch, addressed with external_contract_id. v2 rejects the v1 identifier field with a 422.
  • Test the example bill and a retry offline, without contacting Lago.
  • Keep current billing in place unless you agreed to change it. Review the diff and tests before any live write.
On retries, reuse the saved record’s original transaction_id and timestamp. On the ClickHouse event store, the timestamp is part of the deduplication key, so a retry without it is billed again. Prompt. Replace [approved billing plan] with the metric, billing period, price, and who will own invoices and payments:

3. Connect to the right Lago organization

First, get access to a Lago Cloud organization or self-hosted instance and its API key. The organization needs the product catalog enabled. Set LAGO_API_KEY in your terminal or secret manager without putting it in a prompt or source control. Install CLI 1.3.0 or later where you run your agent:
On other systems, use the CLI release archives. Connect to your target:
For self-hosted Lago, --api-url is the API service’s base URL, without /api/v1 or /api/v2. lago v2 commands send requests to /api/v2 on that host. Confirm the resolved organization and host before changing billing configuration:
A CLI dry run sends nothing. --mode test is a CLI label, not a sandbox inside your organization. Use a separate test organization or an isolated self-hosted instance for live tests. Keep demo customers and plans out of shared production billing. Inspect the existing catalog before proposing changes:
Describe the catalog as YAML and review it with lago v2 apply. Each object takes the fields of its v2 create request and is matched by code. For example, a $100 monthly fee plus $0.01 per completed job:
billing/catalog.yml
The dry run prints what it would create or update and sends nothing. Check the currency, billing timing, rate models, amounts, billing intervals and effective_from dates. A Rate’s effective_from cannot be before today in your organization’s timezone. A fixed Product needs units on its applied Rate Card. Apply the Rate Cards to the Plan before any Contract uses it: a Plan attached to a Contract is locked. apply records the objects it manages in billing/.lago-state/. Commit that folder with the YAML. See the pricing models and Plans. Before sending usage, identify or create the Lago customer and a Contract on that Plan. Use external IDs from your application. Review both requests as dry runs:
billing/contract.json wraps its fields in contract: external_id, contract_code, external_customer_id, plan_code, and optionally started_at and billing_anchor_date. A future started_at creates a pending Contract. Without it, the Contract starts now. Rate Cards with billing_timing: advance bill at the start of a period, so test this sequence in your isolated organization first. Prompt. Replace [target organization] with the organization name and API host, and [approved pricing] with your reviewed price and metric:

4. Check what Lago recorded

Choose one customer and billing period. Calculate the expected usage quantity and amount from saved app records, then read Lago’s current usage for that Contract:
Current usage covers the Contract’s open billing windows. It is not a preview of one invoice. Each entry in rate_cards carries its rate_card_code, product_code, units and amount_cents. Add --rate-card-code or --product-code to narrow the result. Check:
  • Units and amount_cents of the metered Rate Card against your calculation. If usage is zero, wait for processing and read again before investigating.
  • In an isolated test environment, retry one event with its original transaction_id and timestamp. The units should not increase.
  • For the full bill, check the fixed Rate Card entry and any taxes. When the period closes, compare each fee on the invoice with your calculation. v2 has no invoice preview.
Prompt. Replace [customer ID], [contract ID], and [source record ID] with IDs from the same isolated test organization:

If the result differs

Prompt. Replace [contract ID], [billing period], [expected amount], and [observed amount] with the values you are investigating: