> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlago.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Zapier

Here is a typical use case of using Lago with Zapier to create powerful alerting automation.

## Invoice Alerting Example (with Zapier)

<Frame caption="invoice alerting flow">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/invoice-alerting-flow-6c86d0faab38b7740a20925797099c70.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=d5193917c1cd7b2bc9e94e123188013c" width="1444" height="322" data-path="integrations/alerting/images/invoice-alerting-flow-6c86d0faab38b7740a20925797099c70.png" />
</Frame>

In this example, we are going to **build an alert anytime a new invoice is emitted**. To create this workflow, we are using:

1. Lago's webhook when a new invoice is emitted;
2. Zapier as an automation tool, to catch, tranform and send the data; and
3. Slack as the "receiver" to alert your team anytime a new invoice is created.

<Frame caption="Zapier alerting flow">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/zapier-flow-invoice-alerting-6916f7acabd57ddc10125d0fe1b9f7e1.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=f52fbe3f8eeb04136c2b1c0a3d915bcf" width="1800" height="744" data-path="integrations/alerting/images/zapier-flow-invoice-alerting-6916f7acabd57ddc10125d0fe1b9f7e1.png" />
</Frame>

## 1. Catch a webhook when a new invoice is emitted

Lago automatically creates an invoice when a billable period is over. The invoice's payload gives you a detailed view of what has been invoiced. The first action we need to perform is to catch this invoice with a webhook:

1. In Zapier, create a new Zap;
2. Use the **Webhooks by Zapier** as the trigger of this Zap;
3. Select the **Catch Raw Hook** event trigger;
4. Copy the Zapier Webhook URL and paste it in Lago (**Developers** > **Webhooks** > **Add a webhook**); and
5. Catch your first webhook when an invoice is emitted (whenever you assign an add-on or a subscription).

## 2. Run a script to transform the webhook

In Zapier, create a second action by clicking the `+` icon. This new event action is used to format the webhook with a breakdown of fields that can be used in a message.

1. Select **Code by Zapier** as a new *Event Action*;
2. Click on **Javascript** as the event code language to run;
3. Create a field called `payload`. The value of this field is the full **Raw body** of your invoice object received);
4. Run the script (code snippet below) in the **Code** section;
5. Test the action. If valid, it returns a breakdown of fields.

```javascript theme={"dark"}
var obj = JSON.parse(inputData.payload);

if(obj.object_type == "invoice"){
  return obj
}
```

<Frame caption="Script to tranform the invoice payload">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/script-invoice-alerting-ad7c8393f51e76b0601c2e153b1ee1dc.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=a411e41f76774a60ed23919200175a96" width="1804" height="1290" data-path="integrations/alerting/images/script-invoice-alerting-ad7c8393f51e76b0601c2e153b1ee1dc.png" />
</Frame>

<Frame caption="Breakdown of the invoice payload">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/alerting-fields-breakdown-33a58492027cdcbfe5562d1c1cd96397.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=77ca2f70b3b7187ec1102d4dd7a980b8" width="1806" height="1320" data-path="integrations/alerting/images/alerting-fields-breakdown-33a58492027cdcbfe5562d1c1cd96397.png" />
</Frame>

## 3. Send a message to a Slack Channel

Once you catch the breakdown of fields returned by the invoice payload, you can easily use them to create a Slack text message.

In Zapier, create a third action by clicking the `+` icon. This new event action is used to send a message to Slack by using the fields of the invoice payload.

1. Select **Slack** as a new app action;
2. Select the **Send Channel Message** action;
3. Choose the targeted **Slack Account**;
4. Choose the targeted **Slack Channel**; and
5. Create a message by using the fields returned by the payload.

By testing and validating the entire Zap, a Slack message is sent anytime a new invoice is emitted by Lago. You can use the same message example as detailed below:

<Frame caption="Breakdown of the invoice payload">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/slack-message-example-228799226dec5760962b69c5e7816daf.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=df921968059e866e6477d6e5df5bb5c2" width="1804" height="884" data-path="integrations/alerting/images/slack-message-example-228799226dec5760962b69c5e7816daf.png" />
</Frame>

<Frame caption="Slack text message">
  <img src="https://mintcdn.com/lago/Qjm2ad-IYGtRgG7r/integrations/alerting/images/slack-test-message-alerting-f3d6a83c9a3468528ebc31d9e57c1a47.png?fit=max&auto=format&n=Qjm2ad-IYGtRgG7r&q=85&s=29e48963f80e61a9ce9aa157544128f7" width="1644" height="286" data-path="integrations/alerting/images/slack-test-message-alerting-f3d6a83c9a3468528ebc31d9e57c1a47.png" />
</Frame>
