Skip to main content
Here is a typical use case of Lago and n8n for alerting automation.

Overconsumption alerting example (with n8n)

When a customer overconsumes during a period, you might need to warn them. This happens with cloud or API products. Automatic billing should not leave your customers with a bad surprise when they open their invoices. Here is a full workflow to create an alerting system based on your customers’ current usage, using n8n, an automation tool for developers.
n8n workflow with a Cron node, an HTTP Request node, an IF node and a Slack node

n8n alerting workflow

In this example, you build an alert that fires whenever a usage threshold is exceeded. Here is a summary of this workflow:
  1. Use a Cron expression to call the Lago API every X minutes/hours/days;
  2. Call the Current usage endpoint available in Lago to fetch your customers’ current usage;
  3. Create an IF statement to condition the trigger (here, messages are triggered above a specific overconsumption); and
  4. Send a message whenever this threshold is reached. You could use an emailing tool, Slack or a CRM. This example uses Slack.

1st node - CRON expression to repeat tasks at a defined interval

The first node triggers the automation automatically at a defined interval.
  1. Add a new Node;
  2. Select CRON as a new application node;
  3. Set the Mode to Every X; and
  4. Set the Value to 10 and the Unit to Minutes.
The flow then runs every 10 minutes. Change the value and the unit to your preferred interval.
n8n Cron node set to the Every X mode, with a value of 10 and a unit of Minutes

Cron expression

2nd node - Catch customers’ current usage with an HTTP request

This node fetches current usage from the Lago API with an HTTP request.
  1. Add a new Node;
  2. Select HTTP Request as a new application node;
  3. Fetch customers’ current usage from Lago API, with external_subscription_id as a query parameter;
  4. Set the Authorization header to Bearer __YOUR_API_KEY__ and the Content-Type header to application/json; and
  5. Execute the node to fetch the payload from the Lago API.
The node calls the same request as this one:
The response wraps the usage in a customer_usage object, so the next node reads its fields with the customer_usage prefix.
n8n HTTP Request node with the Authorization and Content-Type headers, and the customer_usage response

Customer current usage

3rd node - IF conditional statement to trigger messages under conditions

This node triggers the alert only when your customers exceed a usage threshold. Those limits depend on your product and your paid features. In this example, the alert fires when the total consumption of usage-based features goes over $200. You could also use the units of a charge or another useful value from the payload.
  1. Add a new Node;
  2. Select IF as a new application node;
  3. Create a condition for the TRUE branch (when conditions are met);
  4. Value 1 is the parameter of your condition, here customer_usage.total_amount_cents;
  5. Operation is the comparison you want to apply. Select Larger or Equal and set Value 2 to 20000: total_amount_cents is expressed in cents, so a $200 threshold is 20000.
You can add as many conditions as you need, and you can add an action for the FALSE branch.
n8n IF node comparing customer_usage.total_amount_cents with 20000

Customer current usage with if statement

4th node - Send an alert message to Slack

This last node sends the message. This example uses a Slack channel, but you could also send an email directly to your customers when they pass their usage limits.
  1. Add a new Node;
  2. Select Slack as a new application node;
  3. Select the targeted Slack Account & Slack Channel;
  4. Choose the option to POST a Message; and
  5. Define a message and use the variables of your payload to give context to your customers about their current usage.
After you connect your Slack account, authorize the application’s bot to post messages in the targeted channel.
n8n expression editor building the alert message from the charges_usage fields
Slack alert listing the billable metrics, the units consumed and the next billing period