Same API, New Tricks: Get Event Notifications Just in Time with Webhooks

Written by Vyacheslav Pobachienko

Categories: What's New

3 minute read time

So, I had a few thoughts while working on this update to the API – and maybe you’ve had them, too:

  • Webhooks are great! Everyone should use them.
  • Having the same data across similar APIs and JSON payload are a time saver.

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.

What’s New?

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:

  • ‘application/json’ payload
  • up to 3 URLs per event
  • Data on the following type of events:

opened – every time a user opens one of your messages

clicked – every time a user clicks on a link in your messages

unsubscribed when a user unsubscribes, either from all messages, a specific tag or a mailing list

complained – when a user reports one of your emails as spam. Note that not all ESPs provide this feedback.

delivered when the recipient email server responds that it has accepted the message. 

permanent_fail – there are several reasons why Mailgun stops attempting to deliver messages and drops them including: hard bounces, messages that reached their retry limit, previously unsubscribed/bounced/complained addresses, or addresses rejected by an ESP.

temporary_fail – when a message is temporary rejected by an ESP

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.

How can I use the API?

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"

Where,
id” – the webhook name, only one webhook per request
url” – your url, up to 3 urls per request
And the response message:
{

<span style="font-weight: 400;">"message": "Webhook has been created",</span>

<span style="font-weight: 400;">"webhook": {</span>

<span style="font-weight: 400;">    "urls": [</span>

<span style="font-weight: 400;">         "</span><span style="font-weight: 400;">https://api.your.domain.com/v1/mg/clicked</span><span style="font-weight: 400;">",</span>

<span style="font-weight: 400;">         "</span><span style="font-weight: 400;">https://api.your.domain.com/v2/mg/clicked</span><span style="font-weight: 400;">",</span>

<span style="font-weight: 400;">         "</span><span style="font-weight: 400;">https://api.partner.com/v1/you/clicked</span><span style="font-weight: 400;">"</span>

<span style="font-weight: 400;">     ]</span>

<span style="font-weight: 400;"> }</span>

<span style="font-weight: 400;">}</span>

The received data on your url/urls should be:
<span style="font-weight: 400;">{</span>

<span style="font-weight: 400;">  “signature”:    </span>

<span style="font-weight: 400;">  {</span>

<span style="font-weight: 400;">    "timestamp": "1529006854",</span>

<span style="font-weight: 400;">    "token": "a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0",</span>

<span style="font-weight: 400;">    "signature": "d2271d12299f6592d9d44cd9d250f0704e4674c30d79d07c47a66f95ce71cf55"</span>

<span style="font-weight: 400;">  }</span>

<span style="font-weight: 400;">  “event-data”:    </span>

<span style="font-weight: 400;">  {</span>

<span style="font-weight: 400;">    "timestamp": 1529006854.329574,</span>

<span style="font-weight: 400;">    "id": "DACSsAdVSeGpLid7TN03WA",</span>

<span style="font-weight: 400;">    "event": "delivered",</span>

<span style="font-weight: 400;">    "tags": [...],</span>

<span style="font-weight: 400;">    "user-variables": {...},</span>

<span style="font-weight: 400;">    "message": {</span>

<span style="font-weight: 400;">         "headers": {</span>

<span style="font-weight: 400;">             "message-id": "20180618211821.example.org"</span>

<span style="font-weight: 400;">          }</span>

<span style="font-weight: 400;">    },</span>

<span style="font-weight: 400;">    …</span>

<span style="font-weight: 400;">  }</span>

<span style="font-weight: 400;">}</span>

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).

That’s it.

Can I send events to multiple endpoints?

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 how 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 using  a test API as well, using the following curl command:

curl -s --user ‘api:<span style="font-weight: 400;">YOUR_API_KEY</span><span style="font-weight: 400;">’ \</span>

<span style="font-weight: 400;">https://api.mailgun.net/v3/domains/</span><span style="font-weight: 400;">YOUR_DOMAIN_NAME</span><span style="font-weight: 400;">/webhooks/</span><span style="font-weight: 400;">HOOK_NAME</span><span style="font-weight: 400;">/test</span><span style="font-weight: 400;"> \</span>

<span style="font-weight: 400;">-X PUT \</span>

<span style="font-weight: 400;">-F </span><b>url</b><span style="font-weight: 400;">=YOUR_URL</span>

Where,
url– is your url (one per request)
HOOK_NAME–  is a webhook name (see the list above) one per request as well
And the response message:
<span style="font-weight: 400;">{</span>
<b>"code"   </b><span style="font-weight: 400;">: null,</span>
<b>"message"</b><span style="font-weight: 400;">: "{\"message\":\"Post received. Thanks!\"
}</span>

Where,
code”- the received HTTP code from your side (null means 200 OK)
message”- the received HTTP body from your side or the error message

I’m a Mailgun user, can I migrate easily to the new API?

Mailgun users that are looking forward to migrate to the new Webhooks API, you don’t need to worry: both APIs can be used at the same time. You’ll be notified by the legacy webhooks and the new webhooks. But don’t forget to remove the legacy url as soon as you migrate your app to the new API!

Enjoy!

Tags:

Modified on: November 13, 2018

Stay up-to-date with our blog & new email resources

We'll let you know when we add new email resources and blog posts. We promise not to spam you.