Send Mailgun events to Tinybird¶
Mailgun is a platform for sending email, and it provides a way to send events to Tinybird using webhooks.
Read on to learn how to send events from Mailgun to Tinybird.
Before you start¶
Before you connect Mailgun to Tinybird, ensure:
- You have a Mailgun account.
- You have a Tinybird Workspace.
Connect Mailgun to Tinybird¶
Mailgun provides a variety of webhook event types that you can use to send events to Tinybird.
This guide covers the base case for sending Mailgun events to Tinybird.
In Mailgun, go to Send > Sending > Webhooks.
Select Domain and Add webhook.
In Tinybird, create a Data Source, called
mailgun
with the following schema:
SCHEMA > `date` DateTime `json:$.record.event-data.timestamp`, `event` LowCardinality(String) `json:$.record.event-data.event`, `message_id` String `json:$.record.event-data.message.headers.message-id` DEFAULT '', `recipient` String `json:$.record.event-data.recipient` DEFAULT '', `recipient_domain` String `json:$.record.event-data.recipient-domain` DEFAULT '', `tags` Array(String) `json:$.record.event-data.tags[:]` DEFAULT [], `log_level` LowCardinality(String) `json:$.record.event-data.log-level` DEFAULT '', `city` String `json:$.record.event-data.geolocation.city` DEFAULT '', `country` String `json:$.record.event-data.geolocation.country` DEFAULT '', `region` String `json:$.record.event-data.geolocation.region` DEFAULT '', `client_info` JSON `json:$.record.event-data.client-info` DEFAULT '{}', `delivery_status` JSON `json:$.record.event-data.delivery-status` DEFAULT '{}', `envelope` JSON `json:$.record.event-data.envelope` DEFAULT '{}', `user_variables` JSON `json:$.record.event-data.user-variables` DEFAULT '{}' ENGINE "MergeTree" ENGINE_PARTITION_KEY "toYYYYMM(date)" ENGINE_SORTING_KEY "event, date"
Some columns use the JSON Data Type to store the semi-structured data you receive from Mailgun webhooks in a single column. You can later retrieve various events and their metadata as needed in your Pipes.
In Tinybird, copy a token with privileges to append to the Data Source you created. You can use the admin token or create one with the required scope.
Back in Mailgun, paste the Events API URL in your Webhook Endpoint URL. Use the query parameter
name
to match the name of the Data Source you created in Tinybird.
https://api.tinybird.co/v0/events?name=mailgun&format=json&token=<your user token>
Make sure to use the format=json
query parameter.
Replace the Tinybird API hostname or region with the API region that matches your Workspace.
Select Event type and choose the event you want to send to Tinybird. You can use the same Tinybird Data Source for multiple events.
Select Create webhook abd you're done.
Check the status of the integration from the Log tab in the Tinybird mailgun
Data Source.