> ## Documentation Index
> Fetch the complete documentation index at: https://getlago.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Integration testing

> Set up a test organization and run a full Contracts flow, from a billable metric to an invoice.

Test your integration end to end before you bill real customers. This page covers the test setup and the order of operations. For a complete API walkthrough with every payload and the expected amounts, see [Your first invoice](https://getlago.com/docs/v2/guide/introduction/your-first-invoice.md).

## Set up a test environment

Each Lago organization is a single environment. Lago has no test mode and no separate test API keys. To test without touching production data, use a separate organization, for example "Company Staging" next to "Company Production".

* Replicate the same configuration in both organizations: billable metrics, Products, Rate Cards, Plans and webhook endpoints. Codes are scoped to an organization, so you can reuse the same codes in both.
* Contracts require the Product catalog. Make sure it is enabled on both organizations.
* Each organization has its own [API keys](https://getlago.com/docs/v2/guide/security/api-keys.md). Point your test environment at the test organization's key and keep keys out of source code.
* Your organization lives in one region. Send requests to `https://api.getlago.com/api/v2` (US) or `https://api.eu.getlago.com/api/v2` (EU). See [Lago Cloud](https://getlago.com/docs/v2/guide/lago-cloud.md#regions-and-endpoints).

> [!TIP]
>
> Set a [grace period](https://getlago.com/docs/v2/guide/invoicing/invoicing-settings/grace-period.md) on the test organization's billing entity. Invoices then stay in draft, so you can add late usage, refresh them and inspect fees before they are finalized. The grace period requires a premium license.

## Run the flow

The Contracts flow is: billable metric, Product, Rate Card and Rates, Plan with applied Rate Cards, customer, Contract, events, invoice.

**1. Define a billable metric**

The billable metric defines how Lago aggregates usage events. Fixed Products do not need one.

1. Access the **Billable metrics** section in the side menu.
2. Create a new billable metric.
3. Enter its name, the `code` your events will send and its aggregation type.
4. Save the billable metric.

Learn more in [Create billable metrics](https://getlago.com/docs/v2/guide/billable-metrics/create-billable-metrics.md). API: [Create a billable metric](https://getlago.com/docs/v2/api-reference/billable-metrics/create.md).

**2. Create the Products**

A Product is what you sell. A metered Product references a billable metric. A fixed Product has no metric and bills a quantity set on the Plan or Contract.

Create each Product with its `Name`, `Code`, `Type` (fixed or metered), its `Billable metric` for a metered Product, and an optional `Product category`.

Learn more in [Products](https://getlago.com/docs/v2/guide/product-catalog/products). API: [Create a Product](https://getlago.com/docs/v2/api-reference/products/create.md).

**3. Create the Rate Cards and Rates**

A Rate Card sets the currency, billing timing and proration for one Product. Its Rates set the price, the pricing model, the billing interval and when the price takes effect.

Create one Rate Card per Product and currency, with at least one Rate. A Rate's `effective_from` cannot be before today in your organization timezone.

Learn more in [Rate Cards](https://getlago.com/docs/v2/guide/rate-cards/overview.md) and [Rates](https://getlago.com/docs/v2/guide/rate-cards/rates.md). API: [Create a Rate Card](https://getlago.com/docs/v2/api-reference/rate-cards/create.md).

**4. Create the Plan and apply Rate Cards**

1. Open **Plans** and click **Create plan**.
2. Enter the Plan's `Name`, `Code` and `Plan currency`.
3. Click **Create plan**.
4. Open the Plan and select **Rate cards** under **Overview**.
5. Open the **Apply a rate card** form, select the `Product` and its Rate Card, then click **Add rate card**. Repeat for each Product.

Apply every Rate Card before a Contract uses the Plan. A Plan attached to a Contract is locked.

Learn more in [Plans](https://getlago.com/docs/v2/guide/plans/overview.md) and [Plan phases](https://getlago.com/docs/v2/guide/plans/plan-phases.md). API: [Create a Plan](https://getlago.com/docs/v2/api-reference/plans/create.md), [Apply a Rate Card to a Plan](https://getlago.com/docs/v2/api-reference/plan-applied-rate-cards/create.md).

**5. Create a customer**

1. Open **Customers**.
2. Click **Create a customer**.
3. Enter the customer's name and external ID.
4. Click **Create customer**.

Learn more in [Customer management](https://getlago.com/docs/v2/guide/customers/customer-management.md). API: [Create a customer](https://getlago.com/docs/v2/api-reference/customers/create.md).

**6. Create a Contract**

The Contract applies the Plan to the customer and sets when billing starts.

1. From the customer's page or the Contract list, click **Create contract**.
2. Check the `Customer` and select the `Plan`.
3. Under **Contract settings**, click **Add an external id** and enter your Contract identifier.
4. Enter the required `Code`.
5. Set the start date and the `Billing anchor date`.
6. Click **Create contract**.

A future start date creates a `pending` Contract that becomes `active` at the start. To test right away, set the start date to today. In the API, omit `started_at` to start the Contract now.

Learn more in [Contracts](https://getlago.com/docs/v2/guide/contracts/overview.md) and [Billing timeline](https://getlago.com/docs/v2/guide/contracts/billing-timeline.md). API: [Create a Contract](https://getlago.com/docs/v2/api-reference/contracts/create.md).

**7. Send usage events**

Your backend sends events to the Contract with `external_contract_id`, which matches the Contract's `external_id`, not its `contract_code`. Each event needs a unique `transaction_id`. The `code` must match the billable metric code.

```bash Send an event theme={"dark"}
LAGO_URL="https://api.getlago.com"
API_KEY="__YOUR_TEST_API_KEY__"

curl --location --request POST "$LAGO_URL/api/v2/events" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "event": {
      "transaction_id": "__UNIQUE_ID__",
      "code": "api_calls",
      "external_contract_id": "contract_example",
      "timestamp": 1800007200
    }
  }'
```

`timestamp` is in Unix seconds and must fall inside the billing period you want to test. Keep the same `transaction_id` when you retry a request. Lago uses it for [deduplication](https://getlago.com/docs/v2/guide/events/ingesting-usage.md#idempotency-and-deduplication). To check what Lago received, open **Developers** and the **Events** tab.

Learn more in [Ingest usage](https://getlago.com/docs/v2/guide/events/ingesting-usage.md). API: [Ingest an event](https://getlago.com/docs/v2/api-reference/events/create.md), [Ingest a batch of events](https://getlago.com/docs/v2/api-reference/events/batch.md).

**8. Check current usage**

Current usage shows the provisional amounts for the Contract's open billing windows, before any invoice exists. Use it to confirm that events reach the right Product and price.

```bash Retrieve current usage theme={"dark"}
curl --location --request GET "$LAGO_URL/api/v2/contracts/contract_example/current_usage" \
  --header "Authorization: Bearer $API_KEY"
```

In the Dashboard, open the Contract and click the **Usage** tab. Newly accepted events can take a moment to appear.

Learn more in [Retrieve usage](https://getlago.com/docs/v2/guide/events/retrieve-usage.md#retrieve-current-usage). API: [Retrieve Contract current usage](https://getlago.com/docs/v2/api-reference/contracts/retrieve-current-usage.md).

**9. Receive webhooks**

Lago notifies your application with webhooks, for example `contract.started` when a Contract starts and `invoice.created` when an invoice is finalized. With a grace period, `invoice.drafted` fires when the draft is created.

1. Open **Developers**.
2. Open the **Webhooks** tab.
3. Click **Add an endpoint**.
4. Enter a name and a public endpoint URL.
5. Select **Standard Webhooks** as the signature and the events to receive.
6. Click **Create endpoint**.

Verify the signature of every request in your test environment as you would in production. Learn more in [Webhooks](https://getlago.com/docs/v2/guide/webhooks.md). API: [Create a webhook endpoint](https://getlago.com/docs/v2/api-reference/webhook-endpoints/create.md).

**10. Check the invoice**

Lago generates invoices on the Contract's billing timeline. Rate Cards billed in arrears are invoiced when the billing period ends. Rate Cards billed in advance are invoiced at the start of the period.

With a grace period, the invoice is created as a draft. While it is a draft, you can:

* [Refresh it](https://getlago.com/docs/v2/guide/invoicing/draft-invoices.md#refresh-a-draft-invoice) to include events sent after the period ended. API: `PUT /api/v2/invoices/{lago_id}/refresh`.
* [Finalize it](https://getlago.com/docs/v2/guide/invoicing/draft-invoices.md#finalize-a-draft-invoice) to test the rest of your flow without waiting for the grace period to end. API: `PUT /api/v2/invoices/{lago_id}/finalize`. Finalization cannot be undone.

Inspect each fee's `product_code`, `rate_card_code`, `rate_code`, `external_contract_id`, `units`, `from_datetime` and `to_datetime` to trace it back to your pricing.

Learn more in [Invoices](https://getlago.com/docs/v2/guide/invoicing/overview.md) and [Fees](https://getlago.com/docs/v2/guide/invoicing/fees.md). API: [List invoices](https://getlago.com/docs/v2/api-reference/invoices/list.md), [Retrieve an invoice](https://getlago.com/docs/v2/api-reference/invoices/retrieve.md).

## Test final billing

To test what happens when a customer leaves, end the Contract with `DELETE /api/v2/contracts/{external_id}?contract_code={contract_code}`. An active Contract becomes `terminated` and stops billing from that point, which can produce final fees. A pending Contract becomes `canceled` and never bills.

> [!WARNING]
>
> Ending a Contract is irreversible. Run this only on test Contracts. See [End or cancel a Contract](https://getlago.com/docs/v2/guide/contracts/end-or-cancel.md).

## Limits of a test organization

* Lago does not simulate time. Billing runs on real dates, so an arrears invoice for a monthly Rate appears when the real billing period ends. To see invoices sooner, use a daily Rate (`billing_interval_unit: day`) on a test Rate Card, set a near `billing_anchor_date`, or end the Contract.
* An invoice records what the customer owes. It does not confirm payment. Check `payment_status` separately from `status`, and use your payment provider's test mode in the test organization.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.