Skip to main content

Lago

The lago plugin reports gateway usage events to Lago after requests complete. Lago associates each event with a subscription and billable metric so API traffic can contribute to usage-based billing.

Delivery runs asynchronously and does not delay the proxied response. Monitor gateway error logs for rejected batches, and retain a durable usage source when billing events might need to be replayed because events are dropped after retries are exhausted.

Examples​

The examples report API calls to a count-based metric and attach a property used for differentiated pricing.

Prepare Lago Cloud or a self-hosted Lago. Review Lago's license and deployment requirements before operating a self-hosted installation:

  1. Create an API key from the Lago developer settings.
  2. Create a count-based billable metric with code api_requests.
  3. Create a plan that charges the metric.
  4. Create a customer and assign the plan with external subscription ID docs-subscription.
export LAGO_API_URL="https://api.getlago.com"
export LAGO_API_KEY="replace-with-lago-api-key"

Report API Calls​

curl "http://127.0.0.1:9180/apisix/admin/routes/lago-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/anything/lago",
"plugins": {
"lago": {
"endpoint_addrs": [
"${LAGO_API_URL}"
],
"token": "${LAGO_API_KEY}",
"event_transaction_id": "request-\${request_id}",
"event_subscription_id": "docs-subscription",
"event_code": "api_requests",
"batch_max_size": 1
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF

Send a request:

curl "http://127.0.0.1:9080/anything/lago"

After Lago processes the event, query the subscription events and select stable fields:

curl --get "${LAGO_API_URL}/api/v1/events" \
-H "Authorization: Bearer ${LAGO_API_KEY}" \
--data-urlencode "external_subscription_id=docs-subscription" \
--data-urlencode "code=api_requests" \
| jq '.events[0] | {
code,
external_subscription_id,
transaction_id
}'

The latest event should contain fields similar to:

{
"code": "api_requests",
"external_subscription_id": "docs-subscription",
"transaction_id": "request-..."
}

Add Properties for Differentiated Pricing​

Configure a matching tier filter on the Lago metric or charge, then update the existing plugin to attach the pricing tier and response status to each event.

Replace the plugin configuration on the existing route:

curl "http://127.0.0.1:9180/apisix/admin/routes/lago-route/plugins/lago" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"endpoint_addrs": ["${LAGO_API_URL}"],
"token": "${LAGO_API_KEY}",
"event_transaction_id": "request-\${request_id}",
"event_subscription_id": "docs-subscription",
"event_code": "api_requests",
"event_properties": {
"tier": "premium",
"status": "\${status}"
},
"batch_max_size": 1
}
EOF

Send another request:

curl "http://127.0.0.1:9080/anything/lago"

After Lago processes the event, query the subscription and select the latest event with the pricing tier:

curl --get "${LAGO_API_URL}/api/v1/events" \
-H "Authorization: Bearer ${LAGO_API_KEY}" \
--data-urlencode "external_subscription_id=docs-subscription" \
--data-urlencode "code=api_requests" \
| jq '[.events[] | select(.properties.tier == "premium")][0] | {
code,
properties
}'

The event should include the pricing properties:

{
"code": "api_requests",
"properties": {
"tier": "premium",
"status": "200"
}
}