• What's new

Same API, New Tricks: Get Event Notifications Just In Time With Webhooks

Mailgun Team
5 min read

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 APIand 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:

1curl -s --user ‘api:YOUR_API_KEY’
2https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/webhooks
3-X POST
4-F id=clicked
5-F url="https://api.your.domain.com/v1/mg/clicked"
6-F url="https://api.your.domain.com/v2/mg/clicked"
7-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:

1{
2
3"message": "Webhook has been created",
4
5"webhook": {
6
7 "urls": [
8
9 "https://api.your.domain.com/v1/mg/clicked",
10
11 "https://api.your.domain.com/v2/mg/clicked",
12
13 "https://api.partner.com/v1/you/clicked"
14
15 ]
16
17 }
18
19}

The received data on your url/urls should be:

1{
2
3 “signature”:
4
5 {
6
7 "timestamp": "1529006854",
8
9 "token": "a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0",
10
11 "signature": "d2271d12299f6592d9d44cd9d250f0704e4674c30d79d07c47a66f95ce71cf55"
12
13 }
14
15 “event-data”:
16
17 {
18
19 "timestamp": 1529006854.329574,
20
21 "id": "DACSsAdVSeGpLid7TN03WA",
22
23 "event": "delivered",
24
25 "tags": [...],
26
27 "user-variables": {...},
28
29 "message": {
30
31 "headers": {
32
33 "message-id": "20180618211821.example.org"
34
35 }
36
37 },
38
39
40
41 }
42
43}

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:

1curl -s --user ‘api:YOUR_API_KEY’ \
2
3https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/webhooks/HOOK_NAME/test \
4
5-X PUT \
6
7-F url=YOUR_URL

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:

1 {
2 "code" : null,
3 "message": "{\"message\":\"Post received. Thanks!\"
4 }

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!

Not a Mailgun user? No problem, it only takes a few seconds to sign up.

sign up

It's easy to get started. And it's free.

What will you accomplish with 10,000 free emails and a 100 free validations every month?

Sign Up For Free

Enjoy!

Last updated on August 20, 2019

  • Related posts
  • Recent posts
  • Top posts
View all

Always be in the know and grab free email resources!

No spam, ever. Only musings and writings from the Mailgun team.

Mailgun is committed to protecting your privacy. Please read ourPrivacy Policybefore providing us with your details.

sign up

It's easy to get started. And it's free.

What will you accomplish with 10,000 free emails and a 100 free validations every month?
Sign up for Free