> ## 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.

# Your first invoice

> Configure monthly and usage-based pricing, send usage events, and check the resulting invoice.

Follow this API walkthrough to configure a $100 monthly fee plus $0.01 per API call. You'll create the pricing, apply a Plan to a customer through a Contract, and send usage events.

For one full billing month with 2,000 calls, the expected fees total \$120 USD before tax, with no discounts or credits.

## Before you start

> [!NOTE]
>
> You need an API key with write access to the objects below and the Product catalog enabled on your organization. All requests use `https://api.getlago.com/api/v2` (US). For the EU cluster, use `https://api.eu.getlago.com/api/v2`.

This example covers January 1–February 1, 2027, with usage dated January 15. Both Rate Cards bill after the billing period ends. Creating the Contract does not immediately generate the invoice shown at the end of this guide.

The Contract starts on `2027-01-01T00:00:00Z` and its first billing cycle ends on `2027-02-01`.

A Rate's `effective_from` cannot be before today in your organization timezone.

If you run this later, move the Rate and Contract dates forward together and update the Unix timestamp in both event examples. A different start can make the first cycle shorter than a month, and the first invoice can then differ from \$120.

## What you will build

| Object | Code | Purpose |
| - | - | - |
| Billable metric | `api_calls` | Counts API call events (`count_agg`) |
| Product category | `api_services` | Groups the two Products |
| Fixed Product | `monthly_service` | The \$100 monthly service |
| Metered Product | `api_calls_product` | API calls, measured by `api_calls` |
| Rate Cards | `monthly_service_usd`, `api_calls_usd` | USD, `arrears`, `proration: false` |
| Rates | `monthly_standard`, `calls_standard` | $100 per month, $0.01 per call |
| Plan | `api_monthly` | The offer |
| Customer | `customer_example` | Who you bill |
| Contract | `contract_example` | Applies `api_monthly` to `customer_example` |

**1. Create the billable metric**

Create a billable metric that counts API call events. Count (`count_agg`) does not need a `field_name`. With `recurring: false`, the aggregation resets at the end of each billing cycle.

```bash Create billable metric theme={"dark"}
LAGO_URL="https://api.getlago.com"
API_KEY="__YOUR_API_KEY__"

curl --location --request POST "$LAGO_URL/api/v2/billable_metrics" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "billable_metric": {
      "name": "API calls",
      "code": "api_calls",
      "description": "Number of API calls",
      "aggregation_type": "count_agg",
      "recurring": false
    }
  }'
```

Reference: [Create a billable metric](https://getlago.com/docs/v2/api-reference/billable-metrics/create.md).

**2. Create the Product category and Products**

The category is optional. It groups both Products on usage and invoices. The fixed Product has no metric. The metered Product must reference one.

Run all three requests below: create the Product category, then the monthly service Product and API calls Product.

```bash Product category theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/product_categories" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "product_category": {
      "name": "API services",
      "code": "api_services"
    }
  }'
```

```bash Fixed product theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/products" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "product": {
      "name": "Monthly service",
      "code": "monthly_service",
      "product_type": "fixed",
      "product_category_code": "api_services"
    }
  }'
```

```bash Metered product theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/products" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "product": {
      "name": "API calls",
      "code": "api_calls_product",
      "product_type": "metered",
      "billable_metric_code": "api_calls",
      "product_category_code": "api_services"
    }
  }'
```

References: [Create a Product category](https://getlago.com/docs/v2/api-reference/product-categories/create.md), [Create a Product](https://getlago.com/docs/v2/api-reference/products/create.md).

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

Create a Rate Card for each Product. Both use USD and bill in arrears, meaning after the billing period ends. Proration is disabled, so fees are not adjusted for part of a billing period.

Run both requests to create a Rate Card and its first Rate for each Product. Include the first Rate in `rates` to define its price and monthly billing interval. If the Rate is invalid, the whole request rolls back.

```bash Monthly service rate card theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "rate_card": {
      "name": "Monthly service USD",
      "code": "monthly_service_usd",
      "product_code": "monthly_service",
      "currency": "USD",
      "billing_timing": "arrears",
      "proration": false,
      "rates": [
        {
          "code": "monthly_standard",
          "effective_from": "2027-01-01T00:00:00Z",
          "rate_model": "standard",
          "rate_properties": { "amount": "100" },
          "billing_interval_count": 1,
          "billing_interval_unit": "month"
        }
      ]
    }
  }'
```

```bash API calls rate card theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "rate_card": {
      "name": "API calls USD",
      "code": "api_calls_usd",
      "product_code": "api_calls_product",
      "currency": "USD",
      "billing_timing": "arrears",
      "proration": false,
      "rates": [
        {
          "code": "calls_standard",
          "effective_from": "2027-01-01T00:00:00Z",
          "rate_model": "standard",
          "rate_properties": { "amount": "0.01" },
          "billing_interval_count": 1,
          "billing_interval_unit": "month"
        }
      ]
    }
  }'
```

Check the result with `GET /api/v2/rate_cards/api_calls_usd?expands[]=rates`. References: [Create a Rate Card](https://getlago.com/docs/v2/api-reference/rate-cards/create.md), [Rates](https://getlago.com/docs/v2/guide/rate-cards/rates.md).

**4. Create the Plan**

Create a USD Plan to combine the monthly service and API usage pricing.

```bash Create plan theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/plans" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "plan": {
      "name": "API monthly",
      "code": "api_monthly",
      "currency": "USD"
    }
  }'
```

Reference: [Create a Plan](https://getlago.com/docs/v2/api-reference/plans/create.md).

**5. Apply Rate Cards to the Plan**

Apply both Rate Cards to the Plan. Set `units` to `1` for the monthly service. The API calls Product uses the quantity measured by the billable metric.

> [!NOTE]
>
> Apply the Rate Cards before any Contract uses the Plan: a Plan attached to a Contract is locked.

Run both requests below.

```bash Apply fixed rate card theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/plans/api_monthly/applied_rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "applied_rate_card": {
      "rate_card_code": "monthly_service_usd",
      "units": "1"
    }
  }'
```

```bash Apply API calls rate card theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/plans/api_monthly/applied_rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "applied_rate_card": {
      "rate_card_code": "api_calls_usd"
    }
  }'
```

> [!NOTE]
>
> Without `rate_phases`, each applied Rate Card gets one indefinite phase that follows the active Rate.

Reference: [Create a Plan applied Rate Card](https://getlago.com/docs/v2/api-reference/plan-applied-rate-cards/create.md).

**6. Create the customer**

Create the customer who will receive the invoices. Use `customer_example` to identify this customer when creating the Contract.

```bash Create customer theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/customers" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "customer": {
      "external_id": "customer_example",
      "name": "Acme Corp",
      "email": "billing@example.com",
      "currency": "USD"
    }
  }'
```

Reference: [Create a customer](https://getlago.com/docs/v2/api-reference/customers/create.md).

**7. Create the Contract**

The Contract applies `api_monthly` to `customer_example`. Set the required `contract_code` to identify this specific Contract, including when retrieving it after it ends. A future `started_at` creates a `pending` Contract. It becomes `active` at the start. `billing_anchor_date` sets when the first billing cycle ends.

```bash Create contract theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/contracts" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "contract": {
      "external_id": "contract_example",
      "contract_code": "contract_example_2027",
      "external_customer_id": "customer_example",
      "plan_code": "api_monthly",
      "started_at": "2027-01-01T00:00:00Z",
      "billing_anchor_date": "2027-02-01"
    }
  }'
```

Retrieve it with `GET /api/v2/contracts/contract_example?contract_code=contract_example_2027&expands[]=applied_rate_cards` to confirm the Plan, status, start and anchor. Reference: [Create a Contract](https://getlago.com/docs/v2/api-reference/contracts/create.md).

**8. Send usage events**

Send one event per API call, addressed to the Contract with `external_contract_id`. Each event needs a unique `transaction_id`. `timestamp` is in Unix seconds. `1800007200` is `2027-01-15T10:00:00Z`.

For this walkthrough, run the batch example to send all 2,000 events. The single-event request shows the payload format; you do not need to run it separately.

```bash Single-event request (reference only) theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/events" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "event": {
      "transaction_id": "api_call_0001",
      "code": "api_calls",
      "external_contract_id": "contract_example",
      "timestamp": 1800007200
    }
  }'
```

```bash 2,000 events in batches of 100 theme={"dark"}
for batch in $(seq 0 19); do
  EVENTS=$(for i in $(seq 1 100); do
    n=$((batch * 100 + i))
    printf '{"transaction_id":"api_call_%04d","code":"api_calls","external_contract_id":"contract_example","timestamp":1800007200}' "$n"
    [ "$i" -lt 100 ] && printf ','
  done)

  curl --location --request POST "$LAGO_URL/api/v2/events/batch" \
    --header "Authorization: Bearer $API_KEY" \
    --header 'Content-Type: application/json' \
    --data-raw "{\"events\":[${EVENTS}]}"
done
```

A batch holds at most 100 events by default. When you retry a request, keep the same `transaction_id` values. Deployments can limit how far a timestamp may sit from the request time. References: [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), [Usage ingestion](https://getlago.com/docs/v2/guide/events/ingesting-usage.md).

**9. Check current usage**

Current usage covers the Contract's open billing windows. It is not a preview of one invoice.

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

After all 2,000 events are processed, expect a response like this (abridged):

```json theme={"dark"}
{
  "current_usage": {
    "external_contract_id": "contract_example",
    "external_customer_id": "customer_example",
    "currency": "USD",
    "amount_cents": 12000,
    "rate_cards": [
      {
        "rate_card_code": "api_calls_usd",
        "product_code": "api_calls_product",
        "product_type": "metered",
        "rate_source": "rate",
        "rate_model": "standard",
        "units": "2000",
        "events_count": 2000,
        "amount_cents": 2000
      },
      {
        "rate_card_code": "monthly_service_usd",
        "product_code": "monthly_service",
        "product_type": "fixed",
        "rate_source": "rate",
        "rate_model": "standard",
        "units": "1",
        "amount_cents": 10000
      }
    ]
  }
}
```

Amounts ending in `_cents` are minor units: `12000` is \$120.00. Newly accepted events can take a moment to appear. Reference: [Retrieve Contract current usage](https://getlago.com/docs/v2/api-reference/contracts/retrieve-current-usage.md).

**10. Check the invoice**

Both Rate Cards bill in arrears. When the first cycle ends on February 1, 2027, Lago bills the January period. List the customer's invoices, then retrieve one with its fees.

From the invoice list, select the invoice for this Contract and billing period. Replace `__LAGO_INVOICE_ID__` with its Lago invoice ID.

```bash List invoices theme={"dark"}
curl --location --request GET "$LAGO_URL/api/v2/invoices?external_customer_id=customer_example" \
  --header "Authorization: Bearer $API_KEY"
```

```bash Retrieve invoice with fees theme={"dark"}
LAGO_INVOICE_ID="__LAGO_INVOICE_ID__"

curl --location --request GET "$LAGO_URL/api/v2/invoices/$LAGO_INVOICE_ID?expands[]=fees" \
  --header "Authorization: Bearer $API_KEY"
```

Expected invoice lines, before tax:

| Fee | `rate_card_code` | `units` | Unit price | `amount_cents` |
| - | - | - | - | - |
| Monthly service | `monthly_service_usd` | 1 | \$100.00 | 10000 |
| API calls | `api_calls_usd` | 2,000 | \$0.01 | 2000 |
| **Fees total** | | | | **12000** (\$120.00) |

Each fee carries `product_code`, `rate_card_code`, `rate_code`, `external_contract_id`, `units`, `from_datetime` and `to_datetime`, so you can trace it back to the pricing that produced it. Taxes, if any, appear in `taxes_amount_cents`. References: [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), [List fees](https://getlago.com/docs/v2/api-reference/fees/list.md).

> [!WARNING]
>
> An invoice records what the customer owes. It does not mean the customer has paid. Check `payment_status` separately from `status`.

> [card] [Product catalog](https://getlago.com/docs/v2/guide/product-catalog/overview.md)
>
> Structure Products with categories and filters.

> [card] [Pricing models](https://getlago.com/docs/v2/guide/rate-cards/pricing-models/overview.md)
>
> Graduated, volume, package, percentage and more.

> [card] [Contracts](https://getlago.com/docs/v2/guide/contracts/overview.md)
>
> Start dates, anchors, phases and overrides.

> [card] [API reference](https://getlago.com/docs/v2/api-reference/intro.md)
>
> Every endpoint, with request and response schemas.


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