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:
- Create an API key from the Lago developer settings.
- Create a count-based billable metric with code
api_requests. - Create a plan that charges the metric.
- 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
- Admin API
- ADC
- Ingress Controller
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
services:
- name: lago-service
labels:
docs-example: lago
routes:
- name: lago-route
uris:
- /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:
- host: httpbin.org
port: 80
weight: 1
The backslash preserves the gateway variable so ADC does not replace it with an environment variable.
Preview the change:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
Set up the Ingress Controller and gateway in the aic namespace before continuing. Store the Lago API key through the deployment's secret-delivery workflow.
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: lago-route
spec:
plugins:
- name: lago
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
batch_max_size: 1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: lago-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything/lago
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: lago-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
plugins:
- name: lago
enable: true
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-lago-api-key
event_transaction_id: request-${request_id}
event_subscription_id: docs-subscription
event_code: api_requests
batch_max_size: 1
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
http:
- name: lago-route
match:
paths:
- /anything/lago
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: lago-route
Apply the manifest:
kubectl apply -f lago-ic.yaml
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.
- Admin API
- ADC
- Ingress Controller
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
In adc.yaml, replace the plugin configuration on the existing route:
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
event_properties:
tier: premium
status: \${status}
batch_max_size: 1
The backslashes preserve the gateway variables for request-time resolution.
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=lago
Update the existing plugin configuration with the Kubernetes API used by the deployment:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: lago-route
spec:
plugins:
- name: lago
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-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
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: lago-route
spec:
ingressClassName: apisix
plugins:
- name: lago
enable: true
config:
endpoint_addrs:
- https://api.getlago.com
token: replace-with-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
Apply the updated plugin configuration:
kubectl apply -f lago-ic.yaml
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"
}
}