Skip to main content
The Segment integration forwards product usage tracked in Segment to Lago as billable events. Use it when Segment already captures the actions you bill on and you want Lago to rate them against a contract.

Prerequisites

In Lago:
  1. Create a Lago organization to manage your billing and invoicing.
  2. Create a billable metric to track the usage of your customers.
  3. Create a metered product on that metric, price it with a rate card, and add it to a plan.
  4. Create a customer and a contract for that customer.
In Segment:
  1. Create a Segment account.
  2. Create a data source (ideally, product usage of your customer).

Send usage from Segment to Lago

Create a function

Create a custom Function in Segment:
  1. Navigate to the Catalog in Segment.
  2. Click the Functions tab to access the custom Functions feature.
  3. Choose to create your first function and follow the prompts to set it up.
Segment catalog with the Functions tab open

Segment Functions

Use the destination function

Select the Destination function, as you want to send data from Segment to Lago.
Segment function type picker with Destination function selected

Destinations function

Post requests to Lago events

Replace the pre-written code in the editor with the function below. It catches a Track event from Segment, builds a Lago event body and posts it to POST /api/v2/events. Each event targets a contract through external_contract_id.
transaction_id and code are required. code must match the code of a billable metric. external_contract_id must match the external_id of the contract you bill. The example reads it from a contract_id property on the Segment event. Map it from wherever your tracking plan stores it. For EU-hosted organizations, use https://api.eu.getlago.com/api/v2/events.
This function can be adapted to Identify, Group or Page events. Refer to Segment’s documentation for all available actions. The properties object depends on your billable metric. Add or remove properties, and add conditions to send only specific events to Lago.

Use the test mode editor

Use a sample event in the Test panel to preview the data fetched from a Segment event and debug the function before you deploy it.

Hide sensitive data

The function reads the API key from a setting instead of hard-coding it. Store it as a sensitive variable. To create Settings variables:
  1. Go to the Settings tab.
  2. Add a new Setting.
  3. Define a Name and a Label for this Setting.
  4. Define it as Required or Optional.
  5. Mark it as Sensitive.
Segment function Settings tab with a sensitive API key variable

Hide sensitive data with settings variables

Send usage events to Lago

Run the function in Segment to send a test event to Lago. Check it in the events list or with GET /api/v2/events. Once the setup is finalized in Segment, the function triggers automatically on your defined behavior.

Segment to Lago demo video

This video was recorded with the legacy v1 events payload. Its Segment setup steps still apply. Use the V2 code above for the endpoint and the contract identifier.

Ingest usage

How events target a contract.

Ingest an event

POST /api/v2/events reference.