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

# Lago CLI: the v2 API from your terminal

> Install the CLI, connect to Lago Cloud, and verify a Contract usage charge from your terminal or coding agent.

> [!NOTE]
>
> API v2 is in beta. Endpoints return the `X-Lago-Endpoint-Status: beta` header.

Use the [Lago CLI](https://github.com/getlago/lago-cli) to configure the Product catalog, create Contracts, send usage and read it back from your terminal or coding agent. It works with Lago Cloud US and EU, or self-hosted Lago.

The v2 commands live under `lago v2`. They are generated from the Lago API v2 document pinned in your CLI release and send requests to `/api/v2` on your profile's host. Use an [SDK or the REST API](https://getlago.com/docs/v2/api-reference/intro.md) in your application at runtime.

> [!NOTE]
>
> `lago v2` requires CLI v1.1.0 or later. `lago v2 apply` requires v1.2.1 or later. Every v2 command requires the Product catalog to be enabled on your organization. API v2 is in beta: `lago v2` command and flag names follow the v2 spec and can change with it, so pin a CLI version before scripting against them.

## Install

Homebrew on macOS and Linux installs the binary, the man page and the shell completions:

```bash theme={"dark"}
brew install getlago/tap/lago
```

Anywhere Go 1.27 or later runs:

```bash theme={"dark"}
go install github.com/getlago/lago-cli/cmd/lago@latest
```

Release archives for macOS, Linux and Windows, with checksums signed by cosign, are on the [releases page](https://github.com/getlago/lago-cli/releases). `lago version` prints the CLI version and the two API specs it was built from: `SPEC_VERSION` for v1 and `SPEC_V2_VERSION` for v2. `lago upgrade` tells you whether a newer release exists and the command to install it.

> [!WARNING]
>
> If `lago version` prints a Docker Compose version, a shell alias named `lago` from a self-hosted Lago setup is shadowing the binary. Run `unalias lago` and remove the alias from your shell profile.

## Configure

Get an API key from **Developers > API keys** in the Lago app and confirm your region. For the walkthrough below, use a separate test organization or an isolated self-hosted instance.

Set `LAGO_API_KEY` in your shell or secret manager without putting it in a prompt or source control, then:

```bash Cloud US theme={"dark"}
lago init --api-key "$LAGO_API_KEY" --region us --mode test
```

```bash Cloud EU theme={"dark"}
lago init --api-key "$LAGO_API_KEY" --region eu --mode test
```

```bash Self-hosted theme={"dark"}
lago init --api-key "$LAGO_API_KEY" --region self-hosted --api-url https://lago.example.com --mode test
```

`init` checks the key against the API before saving anything to `~/.config/lago/config.toml`. Pass the base URL only. The profile stores the v1 base, and `lago v2` commands swap `/api/v1` for `/api/v2` on the same host. One profile serves both APIs. The dashboard URL (`app.getlago.com`) is refused, because it is not the API.

`lago whoami` shows the profile, the organization timezone and the exact host requests go to. `lago doctor` checks configuration, network and authentication.

## Verify your first usage charge

This walkthrough creates a billable metric, a metered Product, a Rate Card with one Rate, a Plan, a customer, a Contract and one event. The illustrative price is **\$1 per request**, before taxes. Run it once in an isolated organization with `jq` installed. The `cli_quickstart_*` codes must be unused.

> [!WARNING]
>
> `--mode test` labels the CLI profile. It does not create a sandbox or prevent writes. Creating a Contract starts billing it. Confirm the organization with `lago whoami` before proceeding.

### 1. Check the connection and preview a request

```bash theme={"dark"}
lago whoami --output json
lago doctor
lago v2 customers list --limit 5 --output json
lago v2 billable-metrics create --name "CLI requests" \
  --code cli_quickstart_requests --aggregation-type count_agg --dry-run
```

The dry run prints the method, URL, redacted headers and body without sending anything. The next steps make real writes to your selected organization.

### 2. Create the metric and the metered Product

```bash theme={"dark"}
lago v2 billable-metrics create --name "CLI requests" \
  --code cli_quickstart_requests --aggregation-type count_agg --recurring false
lago v2 products create --name "CLI requests" --code cli_quickstart_requests \
  --product-type metered --billable-metric-code cli_quickstart_requests
```

### 3. Price it with a Rate Card

A Rate's `effective_from` cannot be before today in your organization timezone. Set it to today's date in that timezone, shown by `lago whoami`. The command below uses today's UTC date.

```bash theme={"dark"}
effective_from=$(date -u +%Y-%m-%dT00:00:00Z)
lago v2 rate-cards create --name "CLI requests USD" --code cli_quickstart_requests_usd \
  --product-code cli_quickstart_requests --currency USD \
  --billing-timing arrears --proration false \
  --rates "[{\"code\":\"cli_quickstart_standard\",\"effective_from\":\"$effective_from\",\"rate_model\":\"standard\",\"rate_properties\":{\"amount\":\"1\"},\"billing_interval_unit\":\"month\",\"billing_interval_count\":1}]"
```

The Rate's `amount` is in currency units, so `"1"` means \$1 per request. If the Rate is invalid, the whole request rolls back.

### 4. Create the Plan and apply the Rate Card

```bash theme={"dark"}
lago v2 catalog-plans create --name "CLI quickstart" --code cli_quickstart --currency USD
lago v2 plan-applied-rate-cards create cli_quickstart \
  --rate-card-code cli_quickstart_requests_usd
```

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

### 5. Create the customer and the Contract

```bash theme={"dark"}
lago v2 customers create --external-id cli_quickstart_customer \
  --name "CLI Quickstart" --currency USD
lago v2 contracts create --input '{
  "contract": {
    "external_id": "cli_quickstart_contract",
    "code": "cli_quickstart",
    "external_customer_id": "cli_quickstart_customer",
    "plan_code": "cli_quickstart"
  }
}'
```

The Contract `code` is required by the API, and CLI v1.3.0 has no `--code` flag for it yet. `--input` sends the complete request body, so it carries the `contract` wrapper. Without `started_at`, the Contract starts now and is active.

### 6. Send one event and check usage

Save the original timestamp. Reuse both it and the transaction ID if you retry this event.

```bash theme={"dark"}
event_timestamp=$(date +%s)
lago v2 events send --external-contract-id cli_quickstart_contract \
  --code cli_quickstart_requests --transaction-id cli_quickstart_event \
  --timestamp "$event_timestamp"
lago v2 contracts current-usage cli_quickstart_contract \
  --apply-taxes false --output json
```

After event processing completes, the `cli_quickstart_requests_usd` entry in `current_usage.rate_cards` should show `units: "1"` (or an equivalent decimal) and `amount_cents: 100`: **1 request × $1 = $1**. If usage is still zero, read it again after processing. Check the metric code, the Contract and the timestamp if it remains zero.

Retry only the `lago v2 events send` command above in the same shell, keeping the saved timestamp and transaction ID. Read current usage again: the quantity and amount should remain unchanged.

This verifies the usage charge, not taxes, payment collection or the full invoice lifecycle. Continue with [your first invoice](https://getlago.com/docs/v2/guide/introduction/your-first-invoice.md) for the end-to-end billing cycle.

## How it works

### Flags are API fields

A `snake_case` field is a `--kebab-case` flag. Nested fields carry their parent as a prefix. Objects and arrays take JSON. `--input` sends a whole request body, inline or from `@file.json`.

```bash theme={"dark"}
lago v2 products create --code gpu_hours --name "GPU hours" \
  --product-type metered --billable-metric-code gpu_hours
lago v2 rate-cards create --input @rate_card.json
```

`--input` replaces every other body flag and is sent as written. Include the resource wrapper, such as `{"rate_card": {...}}`.

### Creates print identifiers

A `create` or `update` prints the identifier block. Add `--output json` for the full resource.

```console theme={"dark"}
$ lago v2 products create --code gpu_hours --name "GPU hours" --product-type metered --billable-metric-code gpu_hours
LAGO_ID  1a901a90-1a90-1a90-1a90-1a901a901a90
CODE     gpu_hours
NAME     GPU hours
```

### Responses are wrapped

v2 responses are wrapped in the resource name, like v1. A `--query` starts with that wrapper. Lists put records under the plural name and pagination under `meta`.

```bash theme={"dark"}
lago v2 products get gpu_hours --query 'product.product_type'
lago v2 products list --query 'products[].code'
```

### Lists use cursors

v2 lists are cursor-paginated. `--limit` sets the page size. `--after` takes the previous response's `meta.next_cursor`, and `--before` its `meta.prev_cursor`. `--all` follows the cursors until the last page. There is no `--page` on v2 lists.

### Destructive operations ask first

Every v2 `delete`, plus `invoices void`, `invoices finalize`, `invoices retry-payment`, `invoices retry-taxes`, `credit-notes void` and the credit note metadata deletes, prompts for the identifier. In a script, pass it with `--confirm`. `lago v2 contracts delete` ends the Contract: an active Contract is terminated, a draft or pending one is canceled.

```bash theme={"dark"}
lago v2 contracts delete cli_quickstart_contract --confirm cli_quickstart_contract
```

### Errors name the field

v2 validation errors carry a code per field. The CLI prints them as `field: code`. In `--output json`, the error also carries `error.field_errors` with the raw `{field, code, meta}` entries. A v2 404 names the missing identifier.

### Live is the default mode

`--mode test` is a safety declaration, not a sandbox. Live mode prints `[LIVE]` and gates destructive operations. Credentials passed by flag or environment run live unless you set a mode.

## Scripting

| Flag | Use |
| - | - |
| `--output json` | Full resource. Also `yaml`. Default is `table`. |
| `--query <jmespath>` | Select from the response. Implies `--output json`. |
| `--all` | Every page of a `list`, following `meta.next_cursor`. |
| `--limit`, `--after`, `--before` | One page, by cursor. |
| `--dry-run` | Print the request, send nothing. |
| `--confirm <id>` | Confirm a destructive operation non-interactively. |
| `--profile <name>` | Switch organization or region. |

```bash theme={"dark"}
lago v2 contracts list --all --output json | jq -r '.contracts[].external_id'
lago v2 rate-cards get cli_quickstart_requests_usd --expands rates --query 'rate_card.rates[].code'
lago api GET '/api/v2/products?limit=5'   # any v2 endpoint, same auth and output handling
```

Precedence is flags, then environment, then profile.

| Environment variable | Sets |
| - | - |
| `LAGO_API_KEY` | API key |
| `LAGO_API_URL` | API base URL |
| `LAGO_MODE` | `live` or `test` |
| `LAGO_PROFILE` | Profile name |

## Exit codes

| Code | Meaning |
| - | - |
| 0 | Success |
| 1 | Generic error |
| 2 | Usage error, including a missing `--confirm` |
| 3 | Authentication or authorization |
| 4 | Not found. The error names the missing identifier. |
| 5 | Validation or other 4xx |
| 6 | Rate limited |
| 7 | Server 5xx |
| 8 | Network or timeout |

Money is exact: `*_amount_cents` are integers and `units` are decimal strings. The CLI sends no telemetry.

> [card] [Command reference](https://getlago.com/docs/v2/guide/lago-cli/commands/overview.md)
>
> One page per command group, with commands and their output side by side.

> [card] [API reference](https://getlago.com/docs/v2/api-reference/intro.md)
>
> The v2 schemas the CLI is generated from.


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