Table of Contents

Overview

Base URL

Authentication

Mailgun Regions

API Response Codes

Overview

The Mailgun API is part of the Sinch family and enables you to send, track, and receive email effortlessly. The complete OpenAPI spec is available here:

Base URL

All API calls referenced in our documentation start with a base URL. Mailgun allows the ability to send and receive email in both US and EU regions. Be sure to use the appropriate base URL based on which region you have created for your domain.

It is also important to note that Mailgun uses URI versioning for our API endpoints, and some endpoints may have different versions than others. Please reference the version stated in the URL for each endpoint.

For domains created in our US region the base URL is:

Copy
Copied
https://api.mailgun.net/

For domains created in our EU region the base URL is:

Copy
Copied
https://api.eu.mailgun.net/

Your Mailgun account may contain multiple sending domains. To avoid passing the domain name as a query parameter, most API URLs must include the name of the domain you are interested in:

Copy
Copied
https://api.mailgun.net/v3/mydomain.com

Authentication

Authentication to the Mailgun API is done by providing an Authorization header using [HTTP Basic Auth:](http://en.wikipedia.org/wiki/Basic_access_authentication)

  • Username: api
  • Password: Your API key

Mailgun provides two types of API keys for authenticating against the API:

Primary account API key

When you sign up for Mailgun, a primary account API key is generated. This key allows you to perform all CRUD operations via our various API endpoints and for any of your sending domains. To view your primary account API key:

  1. Go to the Mailgun Dashboard
  2. Click on Account** Settings** on the right-hand side.
  3. Select API Keys and click on the eye icon next to Private API key.

Domain Sending Keys

Domain Sending Keys are API keys that only allow sending messages via a POST call on /messages and /messages.mime endpoints for the domain in which they are created for. To create a sending API key:

  1. Go to the Mailgun Dashboard
  2. Click the Sending tabon the left-hand side of the Mailgun dashboard
  3. Click the Domains tab and select the domain in which you wish to add a sending key to
  4. Click the Domain Settings and navigate to the Sending API keys tab
  5. Click on Add Sending Key

Give your key a suitable description (such as the name of the application or client you are creating the key for) and click Create Sending Key

Copy your API key and keep it in a safe place. For security purposes, we will not be able to show you the key again. If you lose your key, you will need to create a new key.

Here is how you use basic HTTP auth with curl:

Copy
Copied
curl --user 'api:YOUR_API_KEY'
Warning!
Important Reminder** to keep your API safe and secure

Date Format

Mailgun returns JSON for all API calls. JSON does not have a built-in date type; dates are passed as strings encoded according to [RFC](https://tools.ietf.org/html/rfc2822.html#page-14) 2822#page-14. This format is native to JavaScript and is also supported by most programming languages out of the box:

Copy
Copied
'Thu, 13 Oct 2011 18:02:00 +0000'
Warning!
Abbreviated time zones like (EST, CET, IST, HLC) may not result in the correct offset due to the ambiguous nature of abbreviated time zones. The numerical offset (+0500) or GMT/UTC is preferred.

Mailgun Regions

Using a single account and billing plan, you can choose to provision new sending domains in the EU environment. It is important to note that message data never leaves the region in which it is processed. Only a limited amount of account data is replicated globally, giving you a single account from which to manage domains in both the US and the EU.

Here are the specifics on the type of data that is replicated globally versus what is region-bound:

GlobalRegion-Bound (US / EU)
Account Information, User Accounts, Billing Details (invoices/plan information), API Keys, Domain NamesDomain Metadata (e.g., SMTP credentials), Messages, Event Logs, Suppressions, Mailing Lists, Tags, Statistics, Routes, IP Addresses

Below are the endpoints you will use for sending/receiving/tracking messages in the EU:

ServiceUS EndpointEU Endpoint
REST APIapi.mailgun.netapi.eu.mailgun.net
Outgoing SMTP Serversmtp.mailgun.orgsmtp.eu.mailgun.org
Inbound SMTP Server (Routes)mxa.mailgun.orgmxa.eu.mailgun.org
Inbound SMTP Server (Routes)mxb.mailgun.orgmxb.eu.mailgun.org
Open/Click Tracking Endpointmailgun.orgeu.mailgun.org

API Response Codes

All of Mailgun's HTTP response codes follow standard HTTP definitions. For some additional information and troubleshooting steps, please see below.

  • 400 - Will typically contain a JSON response with a "message" key which contains a human readable message / action to interpret.
  • 403 - Auth error or access denied. Please ensure your API key is correct and that you are part of a group that has access to the desired resource.
  • 404 - Resource not found. NOTE: this one can be temporal as our system is an eventually-consistent system but requires diligence. If a JSON response is missing for a 404 - that's usually a sign that there was a mistake in the API request, such as a non-existing endpoint.
  • 429 - Mailgun does have rate limits in place to protect our system. Please retry these requests as defined in the response. In the unlikely case you encounter them and need them raised, please reach out to our support team.
  • 500 - Internal Error on the Mailgun side. Retries are recommended with exponential or logarithmic retry intervals. If the issue persists, please reach out to our support team.