Product

Same API, new tricks: Get event notifications just in time with webhooks

With this webhooks update, you have more choices to communicate with Mailgun and see the details of what’s happening with your messages.

So, we 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 is 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?

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:

  • ‘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 temporarily 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:

Here, id should be the webhook name (only one webhook per request) and urlshould indicates your URL (up to 3 URLs per request).

And the response message:

The received data on your URL/URLs should be:

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?

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

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:

Here, code indicates the received HTTP code from your side (null means 200 OK) and messageshows the received HTTP body from your side or the error message

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

Can't wait to migrate to the new Webhooks API? No need to worry both APIs can be used at the same time. You’ll be notified by both 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.

See what you can accomplish with the world’s best email delivery platform.

Enjoy!

Related readings

Your guide to webhooks

Webhooks – user-defined HTTP callbacks – are a very easy way for developers to monitor their email campaigns and build programs to handle bounces, unsubscribes, spam reports, and more in real-time.

Read more

A practical guide to using Mailgun’s webhooks

Learn how to set webhooks up, handle data from your transactional emails, and secure webhook data in this guest post from a Mailgun customer.

Read more

New events API, detailed email tracking and search

The completely refactored Events API makes it easy and fast to determine exactly what happened with each and every one of your emails.

Read more

Popular posts

Mailgun iconSee what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
Milgun-Icon