Product
So, we had a few thoughts while working on this update to the API – and maybe you’ve had them, too:
Honestly, the Mailgun webhook API has been around a long time now. But with this update, you have more choices to communicate with us and see the details of what’s happening with your messages.
Let’s dive into the details.
So what is this update all about?
Mailgun can help you receive notifications just in time so you can see when something has happened to your message. You have the choice to use event polling via the Events API, or let us push events to you via the Webhooks API. These alerts have the same data as the event API and are sent to your URL/URLs by HTTP POST.
Now, you get
The good thing about this is that your code for business logic can be used for either option. The difference is in how you connect to Mailgun. And since each event has its own unique ID, if it turns out that your http
endpoint has died for some reason, you can easily pull events and get them sorted out using this unique ID. Of course, we always recommend handling webhooks in an asynchronous manner so events spikes won’t be an issue.
Now let’s see how to configure a domain with the ‘clicked’ webhook. It’s really a one-step process that you can complete by setting up URL/URLs using curl
or your preferred programming language via our HTTP API.
For instance, using the curl
command:
curl -s --user ‘api:YOUR_API_KEY’
https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/webhooks
-X POST
-F id=clicked
-F url="https://api.your.domain.com/v1/mg/clicked"
-F url="https://api.your.domain.com/v2/mg/clicked"
-F url="https://api.partner.com/v1/you/clicked"
Here, “id”
should be the webhook name (only one webhook per request) and “url”
should indicates your URL (up to 3 URLs per request).
And the response message:
{
"message": "Webhook has been created",
"webhook": {
"urls": [
"https://api.your.domain.com/v1/mg/clicked",
"https://api.your.domain.com/v2/mg/clicked",
"https://api.partner.com/v1/you/clicked"
]
}
}
The received data on your URL/URLs should be:
{
“signature”:
{
"timestamp": "1529006854",
"token": "a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0",
"signature": "d2271d12299f6592d9d44cd9d250f0704e4674c30d79d07c47a66f95ce71cf55"
}
“event-data”:
{
"timestamp": 1529006854.329574,
"id": "DACSsAdVSeGpLid7TN03WA",
"event": "delivered",
"tags": [...],
"user-variables": {...},
"message": {
"headers": {
"message-id": "20180618211821.example.org"
}
},
…
}
}
The “event-data”
portion is the same as what the Events API returns and contains: event timestamp, unique event id, event name, message-id, your tags and variables, etc. As a best practice, don’t forget to verify the “signature”
portion (see here how it’s done).
And that’s it. Easy, right?
Yes, you can! If you need to migrate your application to a new version, or you need to send events to your partner site, it can be done for up to 3 endpoints.
If you want to see what your event looks like or you have a question for our support team, it’s easy to configure a webhook with a temporary URL to our request bin at http://bin.mailgun.net and consume events at the same time.
Here’s a look at what happens when using a test API as well, using the following curl
command:
curl -s --user ‘api:YOUR_API_KEY’ \
https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/webhooks/HOOK_NAME/test \
-X PUT \
-F url=YOUR_URL
Here, “url”
is your URL (one per request) and “HOOK_NAME”
is a webhook name (see the list above – one per request as well).
And the response message:
{
"code" : null,
"message": "{\"message\":\"Post received. Thanks!\"
}
Here, “code”
indicates the received HTTP code from your side (null means 200 OK) and “message”
shows the received HTTP body from your side or the error message
Yes. And all legacy webhooks will need to be migrated to this new version. The legacy webhooks API endpoint has been made read only as of April 15, 2023, concluding a deprecation process that began in March 2022.
Product updates are important, and being an email company, we’re pretty good at sending updates to keep you in the loop. Want to explore the why and how behind our moves? Be sure to subscribe to our newsletter for more content like this.
Send me the Mailjet Newsletter. I expressly agree to receive the newsletter and know that I can easily unsubscribe at any time.