The domains endpoint is available at:
v4/domains
The domains API allows you to create, access, and validate domains programmatically.
GET /domains
Returns a list of domains under your account in JSON. See examples below.
Parameter | Description |
---|---|
authority | Filter the list by a given DKIM authority name |
state | Filter the list by a given state. Can be active , unverified or disabled |
limit | Maximum number of records to return. (100 by default) |
skip | Number of records to skip. (0 by default) |
GET /domains/<domain>
Returns a single domain, including credentials and DNS records. See examples below.
PUT /domains/<domain>/verify
Places the domain in an unverified state and triggers the verification process. Returns a single domain, including credentials and DNS records.If the domain is successfully verified the domain’s state will be ‘active’. For more information on verifying domains,visit the Mailgun User Manual.
POST /domains
Create a new domain. See examples below.
Parameter | Description |
---|---|
name | Name of the domain (ex. domain.com) |
smtp_password | Password for SMTP authentication |
spam_action |
If If If The default is |
wildcard |
Determines whether the domain will accept email for sub-domains when sending messages. The default is |
force_dkim_authority |
If set to If set to The default is |
dkim_key_size |
Set the length of your domain’s generated DKIM key The default is |
ips | An optional, comma-separated list of IP addresses to be assignedto this domain. If not specified, all dedicated IP addresses onthe account will be assigned. If the request cannot be fulfilled(e.g. a requested IP is not assigned to the account, etc), a400 will be returned. |
pool_id | The id of the IP Pool that you wish to assign to the domain. The pool must containat least 1 IP. (Note: IP Pools are only available on certain plans; see http://mailgun.com/pricing) |
web_scheme |
Set your open, click and unsubscribe URLs to use http or https The default is |
DELETE /domains/<domain>
Delete a domain from your account.
POST /domains/<domain>/credentials
Creates a new set of SMTP credentials for the defined domain.
Parameter | Description |
---|---|
login | The user name, for example bob.bar |
password | A password for the SMTP credentials. (Length Min 5, Max 32) |
PUT /domains/<domain>/credentials/<login>
Updates the specified SMTP credentials. Currently only the password can be changed.
Parameter | Description |
---|---|
password | A password for the SMTP credentials. (Length Min 5, Max 32) |
DELETE /domains/<domain>/credentials/<login>
Deletes the defined SMTP credentials.
Note
Mailgun imposes a rate limit for the Domains API endpoint. Users mayissue no more than 300 requests per minute, per account. See the resultantrate limit response below.
GET /domains/<domain>/connection
Returns delivery connection settings for the defined domain.
PUT /domains/<domain>/connection
Updates the specified delivery connection settings for the defined domain.
Parameter | Description |
---|---|
require_tls | true or false If set to true, this requires the message only be sent overa TLS connection. If a TLS connection can not be established,Mailgun will not deliver the message. If set to false, Mailgun will still try and upgrade theconnection, but if Mailgun cannot, the message will bedelivered over a plaintext SMTP connection. The default is false. |
skip_verification | true or false If set to true, the certificate and hostname will not beverified when trying to establish a TLS connection and Mailgunwill accept any certificate during delivery. If set to false, Mailgun will verify the certificate andhostname. If either one can not be verified, a TLS connectionwill not be established. The default is false. |
GET /domains/<domain>/tracking
Returns tracking settings for a domain.
PUT /domains/<domain>/tracking/open
Updates the open tracking settings for a domain.
Parameter | Description |
---|---|
active | yes or no |
PUT /domains/<domain>/tracking/click
Updates the click tracking settings for a domain.
Parameter | Description |
---|---|
active |
If set to yes, links will be overwritten and pointed to ourservers so we can track clicks. If set to htmlonly, links will only be rewritten in the HTMLpart of a message. |
PUT /domains/<domain>/tracking/unsubscribe
Updates unsubscribe tracking settings for a domain.
Parameter | Description |
---|---|
active | true or false. |
html_footer | Custom HTML version of unsubscribe footer. |
text_footer | Custom text version of unsubscribe footer. Mailgun can automatically provide an unsubscribe footer ineach email you send and also provides you with severalunsubscribe variables. You can customize your unsubscribefooter by editing the settings in the Control Panel.See Open and Click Bot Detection for more details. |
PUT /domains/<domain>/dkim_authority
Change the DKIM authority for a domain.
Parameter | Description |
---|---|
self | true or false Change the DKIM authority for a domain. If set to true, the domain will be the DKIM authority for itselfeven if the root domain is registered on the same mailgun account If set to false, the domain will have the same DKIM authorityas the root domain registered on the same mailgun account |
Note
Use with caution: Do not forget to change the corresponding DNS record.It can take 24-48 hours for DNS changes to propagate.Changing the DKIM authority of an active domain affects its current deliverability.
Update the DKIM selector for a domains
PUT /domains/<domain>/dkim_selector
Parameter | Description |
---|---|
dkim_selector | change the DKIM selector for a domain. |
Update the CNAME used for tracking opens and clicks
PUT /domains/<domain>/web_prefix
Parameter | Description |
---|---|
web_prefix | change the tracking CNAME for a domain. |
Example¶
Get a list of all domains.
curl -s --user 'api:YOUR_API_KEY' -G \ https://api.mailgun.net/v3/domains \ -d skip=0 \ -d limit=3
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.enums.DomainState;import com.mailgun.model.domains.DomainListResponse;import com.mailgun.model.domains.DomainsParametersFilter;// ...public DomainListResponse getDomains() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); DomainsParametersFilter filter = DomainsParametersFilter.builder() .skip(0) .limit(3) .state(DomainState.ACTIVE) .build(); return mailgunDomainsApi.getDomainsList(filter);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');# Issue the call to the client.$result = $mgClient->domains()->index();
def get_domains(): return requests.get( "https://api.mailgun.net/v3/domains", auth=("api", "YOUR_API_KEY"), params={"skip": 0, "limit": 3})
def get_domains RestClient.get "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains", :params => { :skip => 0, :limit => 3 }end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class GetDomainsChunk{ public static void Main (string[] args) { Console.WriteLine (GetDomains ().Content.ToString ()); } public static IRestResponse GetDomains () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.Resource = "domains"; request.AddParameter ("skip", 0); request.AddParameter ("limit", 3); return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func ListDomains(domain, apiKey string) ([]mailgun.Domain, error) { mg := mailgun.NewMailgun(domain, apiKey) it := mg.ListDomains(nil) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() var page, result []mailgun.Domain for it.Next(ctx, &page) { result = append(result, page...) } if it.Err() != nil { return nil, it.Err() } return result, nil}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const domainsList = await client.domains.list(); console.log('domainsList', domainsList); } catch (error) { console.error(error); }})();
Sample response:
{ "items": [ { "created_at": "Mon, 30 Sep 2019 16:20:48 GMT", "id": "55a55f40803267158aa113e8", "is_disabled": false, "name": "samples.mailgun.org", "require_tls": false, "skip_verification": false, "smtp_login": "postmaster@samples.mailgun.org", "spam_action": "disabled", "state": "active", "type": "sandbox", "web_prefix": "email", "web_scheme": "http", "wildcard": false } ], "total_count": 1}
Get a single domain.
curl -s --user 'api:YOUR_API_KEY' -G \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.domains.SingleDomainResponse;// ...public SingleDomainResponse getDomain() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); return mailgunDomainsApi.getSingleDomain(YOUR_DOMAIN_NAME);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = "YOUR_DOMAIN_NAME";# Issue the call to the client.$result = $mgClient->domains()->show($domain);
def get_domain(): return requests.get( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME", auth=("api", "YOUR_API_KEY"))
def get_domain RestClient.get("https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME"\ {|response, request, result| response }end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class GetDomainChunk{ public static void Main (string[] args) { Console.WriteLine (GetDomain ().Content.ToString ()); } public static IRestResponse GetDomain () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "/domains/{domain}"; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func GetDomain(domain, apiKey string) (mailgun.DomainResponse, error) { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.GetDomain(ctx, domain)}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const domain = await client.domains.get(DOMAIN); console.log('domain', domain); } catch (error) { console.error(error); }})();
Sample response:
{ "domain": { "created_at": "Tue, 14 Jul 2015 19:13:04 GMT", "id": "55a55f40803267158aa113e8", "is_disabled": false, "name": "samples.mailgun.org", "require_tls": false, "skip_verification": false, "smtp_login": "postmaster@samples.mailgun.org", "spam_action": "disabled", "state": "active", "type": "sandbox", "web_prefix": "email", "web_scheme": "http", "wildcard": false },"receiving_dns_records": [ { "priority": "10", "record_type": "MX", "valid": "valid", "value": "mxa.mailgun.org" }, { "priority": "10", "record_type": "MX", "valid": "valid", "value": "mxb.mailgun.org" } ], "sending_dns_records": [ { "record_type": "TXT", "valid": "valid", "name": "domain.com", "value": "v=spf1 include:mailgun.org ~all" }, { "record_type": "TXT", "valid": "valid", "name": "domain.com", "value": "k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUA...." }, { "record_type": "CNAME", "valid": "valid", "name": "email.domain.com", "value": "mailgun.org" } ]}
Adding a domain.
curl -s --user 'api:YOUR_API_KEY' \ -X POST https://api.mailgun.net/v3/domains \ -F name='YOUR_NEW_DOMAIN_NAME' \ -F smtp_password='supersecretpassword'
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.enums.SpamAction;import com.mailgun.enums.WebScheme;import com.mailgun.model.domains.DomainRequest;import com.mailgun.model.domains.DomainResponse;import java.util.List;// ...public DomainResponse addDomain() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); DomainRequest request = DomainRequest.builder() .name(YOUR_NEW_DOMAIN_NAME) .spamAction(SpamAction.BLOCK) .wildcard(true) .forceDkimAuthority(false) .dkimKeySize(1024) .ips(List.of(IP_1, IP_2)) .webScheme(WebScheme.HTTPS) .build(); return mailgunDomainsApi.createNewDomain(request);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = 'YOUR_DOMAIN_NAME';# Issue the call to the client.$result = $mgClient->domains()->create($domain);
def add_domain(): return requests.post( "https://api.mailgun.net/v3/domains", auth=("api", "YOUR_API_KEY"), data={'name':'YOUR_NEW_DOMAIN_NAME', 'smtp_password':'supersecretpassword'})
def add_domain RestClient.post("https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains", :name => 'YOUR_NEW_DOMAIN_NAME', :smtp_password => 'supersecretpassword')end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class AddDomainChunk{ public static void Main (string[] args) { Console.WriteLine (AddDomain ().Content.ToString ()); } public static IRestResponse AddDomain () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3/"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.Resource = "domains"; request.AddParameter ("name", "YOUR_NEW_DOMAIN_NAME"); request.AddParameter ("smtp_password", "supersecretpassword"); request.Method = Method.POST; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func AddDomain(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{ Password: "super_secret", SpamAction: mailgun.SpamActionTag, Wildcard: false, })}
const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const newDomain = await client.domains.create({ name: "YOUR_NEW_DOMAIN_NAME", smtp_login: "smtpLogin", smtp_password: 'supersecret!' }); console.log('newDomain', newDomain); } catch (error) { console.error(error); }})();
Sample response:
{ "domain": { "created_at": "Tue, 14 Jul 2015 19:13:04 GMT", "id": "55a55f40803267158aa113e8", "is_disabled": false, "name": "samples.mailgun.org", "require_tls": false, "skip_verification": false, "smtp_login": "postmaster@samples.mailgun.org", "spam_action": "disabled", "state": "active", "type": "sandbox", "web_prefix": "email", "web_scheme": "http", "wildcard": false }, "message": "Domain has been created", "receiving_dns_records": [ { "cached": [], "priority": "10", "record_type": "MX", "valid": "valid", "value": "mxa.mailgun.org" }, { "cached": [], "priority": "10", "record_type": "MX", "valid": "valid", "value": "mxb.mailgun.org" } ], "sending_dns_records": [ { "cached": [], "name": "example.com", "record_type": "TXT", "valid": "valid", "value": "v=spf1 include:mailgun.org ~all" }, { "cached": [], "name": "k1._domainkey.example.com", "record_type": "TXT", "valid": "valid", "value": "k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4G...." }, { "cached": [], "name": "email.example.com", "record_type": "CNAME", "valid": "valid", "value": "mailgun.org" } ]}
Deleting a domain.
curl -s --user 'api:YOUR_API_KEY' -X DELETE \ https://api.mailgun.net/v3/domains/example.mailgun.org
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.ResponseWithMessage;// ...public ResponseWithMessage deleteDomain() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); return mailgunDomainsApi.deleteDomain(YOUR_DOMAIN_NAME);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = 'YOUR_DOMAIN_NAME';# Issue the call to the client.$result = $mgClient->domains()->delete($domain);
def delete_domain(): return requests.delete( "https://api.mailgun.net/v3/domains/example.mailgun.org", auth=("api", "YOUR_API_KEY"))
def delete_domain RestClient.delete "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/example.mailgun.org"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class DeleteDomainChunk{ public static void Main (string[] args) { Console.WriteLine (DeleteDomain ().Content.ToString ()); } public static IRestResponse DeleteDomain () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.Resource = "/domains/{name}"; request.AddUrlSegment ("name", "example.mailgun.org"); request.Method = Method.DELETE; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func DeleteDomain(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.DeleteDomain(ctx, "example.com")}
const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const destroyedDomain = await client.domains.destroy('DOMAIN_TO_DELETE'); console.log('destroyedDomain', destroyedDomain); } catch (error) { console.error(error); }})();
Sample response:
{ "message": "Domain has been deleted"}
Rate Limit Response:
{ "retry-seconds": 60,}
Listing all SMTP credentials:
curl -s --user 'api:YOUR_API_KEY' -G \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials
import com.mashape.unirest.http.HttpResponse;import com.mashape.unirest.http.JsonNode;import com.mashape.unirest.http.Unirest;import com.mashape.unirest.http.exceptions.UnirestException;public class MGSample { // ... public static JsonNode getCredentials() throws UnirestException { HttpResponse<JsonNode> request = Unirest.get("https://api.mailgun.net/v3/domains/" + YOUR_DOMAIN_NAME + "/credentials") .basicAuth("api", API_KEY) .asJson(); return request.getBody(); }}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = "YOUR_DOMAIN_NAME";# Issue the call to the client.$result = $mgClient->domains()->credentials($domain);
def get_credentials(): return requests.get( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials", auth=("api", "YOUR_API_KEY"))
def get_credentials RestClient.get "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class GetCredentialsChunk{ public static void Main (string[] args) { Console.WriteLine (GetCredentials ().Content.ToString ()); } public static IRestResponse GetCredentials () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "domains/{domain}/credentials"; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func ListCredentials(domain, apiKey string) ([]mailgun.Credential, error) { mg := mailgun.NewMailgun(domain, apiKey) it := mg.ListCredentials(nil) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() var page, result []mailgun.Credential for it.Next(ctx, &page) { result = append(result, page...) } if it.Err() != nil { return nil, it.Err() } return result, nil}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const domainCredentials = await client.domains.domainCredentials.list(DOMAIN); console.log('domainCredentials', domainCredentials); } catch (error) { console.error(error); }})();
Sample response:
{ "total_count": 2, "items": [ { "size_bytes": 0, "created_at": "Tue, 27 Sep 2011 20:24:22 GMT", "mailbox": "user@samples.mailgun.org", "login": "user@samples.mailgun.org" }, { "size_bytes": 0, "created_at": "Thu, 06 Oct 2011 10:22:36 GMT", "mailbox": "user@samples.mailgun.org", "login": "user@samples.mailgun.org" } ]}
Creating new SMTP credentials:
curl -s --user 'api:YOUR_API_KEY' \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials \ -F login='alice@YOUR_DOMAIN_NAME' \ -F password='supasecret'
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.ResponseWithMessage;import com.mailgun.model.domains.DomainCredentials;// ...public ResponseWithMessage createCredentials() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); DomainCredentials domainCredentials = DomainCredentials.builder() .login("alice@YOUR_DOMAIN_NAME.com") .password( "super_secret_password") .build(); return mailgunDomainsApi.createNewCredentials(YOUR_DOMAIN_NAME, domainCredentials);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = 'YOUR_DOMAIN_NAME';$smtpUser = 'bob';$smtpPass = 'new_password';# Issue the call to the client.$result = $mgClient->domains()->createCredential($domain, $smtpUser, $smtpPass);
def create_credentials(): return requests.post( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials", auth=("api", "YOUR_API_KEY"), data={"login": "alice@YOUR_DOMAIN_NAME", "password": "secret"})
def create_credentials RestClient.post "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials", :login => "alice@YOUR_DOMAIN_NAME", :password => "secret"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class CreateCredentialsChunk{ public static void Main (string[] args) { Console.WriteLine (CreateCredentials ().Content.ToString ()); } public static IRestResponse CreateCredentials () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "domains/{domain}/credentials"; request.AddParameter ("login", "alice@YOUR_DOMAIN_NAME"); request.AddParameter ("password", "secret"); request.Method = Method.POST; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func CreateCredential(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.CreateCredential(ctx, "alice@example.com", "secret")}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const createdCredentials = await client.domains.domainCredentials.create(DOMAIN, { login: 'alice@YOUR_DOMAIN_NAME', password: 'secret' }); console.log('createdCredentials', createdCredentials); } catch (error) { console.error(error); }})();
Sample response:
{ "message": "Created 1 credentials pair(s)"}
Updating the password for a given credential pair:
curl -s --user 'api:YOUR_API_KEY' -X PUT \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice \ -F password='abc123'
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.ResponseWithMessage;// ...public ResponseWithMessage updatePassword() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); return mailgunDomainsApi.updateCredentials(YOUR_DOMAIN_NAME, YOUR_LOGIN, "super_secret_password");}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = 'YOUR_DOMAIN_NAME';$smtpUser = 'bob';$smtpPass = 'new_password';# Issue the call to the client.$result = $mgClient->domains()->updateCredential($domain, $smtpUser, $smtpPass);
def change_credential_password(): return requests.put( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice", auth=("api", "YOUR_API_KEY"), data={"password": "supersecret"})
def change_credential_password RestClient.put "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice", :password => "supersecret"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class ChangePwdCredentialsChunk{ public static void Main (string[] args) { Console.WriteLine (ChangeCredentialPassword ().Content.ToString ()); } public static IRestResponse ChangeCredentialPassword () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "domains/{domain}/credentials/{username}"; request.AddUrlSegment ("username", "alice"); request.AddParameter ("password", "supersecret"); request.Method = Method.PUT; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func ChangePassword(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.ChangeCredentialPassword(ctx, "alice", "super_secret")}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const updatedCredentials = await client.domains.domainCredentials.update(DOMAIN, 'LOGIN_FROM_CREDENTIALS', { password: 'new_password' }); console.log('updatedCredentials -------->', updatedCredentials); } catch (error) { console.error(error); }})();
Sample response:
{ "message": "Password changed"}
Deleting a given credential pair:
curl -s --user 'api:YOUR_API_KEY' -X DELETE \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.ResponseWithMessage;
// …
- public ResponseWithMessage deleteCredentials() {
- MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY)
- .createApi(MailgunDomainsApi.class);
return mailgunDomainsApi.deleteCredentials(YOUR_DOMAIN_NAME, YOUR_LOGIN);
}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = 'YOUR_DOMAIN_NAME';$smtpUser = 'bob';# Issue the call to the client.$result = $mgClient->domains()->deleteCredential($domain, $smtpUser);
def delete_credentials(): return requests.delete( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice", auth=("api", "YOUR_API_KEY"))
def delete_credentials RestClient.delete "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/credentials/alice"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class DeleteCredentialsChunk{ public static void Main (string[] args) { Console.WriteLine (DeleteCredentials ().Content.ToString ()); } public static IRestResponse DeleteCredentials () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "domains/{domain}/credentials/{login}"; request.AddUrlSegment ("login", "alice"); request.Method = Method.DELETE; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func DeleteCredential(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.DeleteCredential(ctx, "alice")}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const deletedDomainCredentials = await client.domains.domainCredentials.destroy(DOMAIN, 'alice@YOUR_DOMAIN_NAME'); console.log('deletedDomainCredentials', deletedDomainCredentials); } catch (error) { console.error(error); }})();
Sample response:
{ "message": "Credentials have been deleted", "spec": "alice@samples.mailgun.org"}
Listing connection settings:
curl -s --user 'api:YOUR_API_KEY' -G \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.domains.DomainConnectionResponse;// ...public DomainConnectionResponse getConnections() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); return mailgunDomainsApi.getDomainConnectionSettings(YOUR_DOMAIN_NAME);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = "YOUR_DOMAIN_NAME";# Issue the call to the client.$result = $mgClient->domains()->connection($domain);
def get_connection(): return requests.get( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection", auth=("api", "YOUR_API_KEY"))
def get_connection RestClient.get "https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection"end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class GetConnectionChunk{ public static void Main (string[] args) { Console.WriteLine (GetConnection ().Content.ToString ()); } public static IRestResponse GetConnection () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "domains/{domain}/connection"; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func GetDomainConnection(domain, apiKey string) (mailgun.DomainConnection, error) { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.GetDomainConnection(ctx, domain)}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const connectionSettings = await client.domains.getConnection(DOMAIN); console.log('connectionSettings', connectionSettings); } catch (error) { console.error(error); }})();
Sample response:
{ "connection": { "require_tls": false, "skip_verification": false}
Update connection settings:
curl -s --user 'api:YOUR_API_KEY' -X PUT \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection -F require_tls='true' \ -F skip_verification='false'
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.domains.DomainConnectionRequest;import com.mailgun.model.domains.UpdateDomainConnectionResponse;// ...public UpdateDomainConnectionResponse updateConnections() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); DomainConnectionRequest domainConnection = DomainConnectionRequest.builder() .requireTls(true) .skipVerification(false) .build(); return mailgunDomainsApi.updateDomainConnectionSettings(YOUR_DOMAIN_NAME, domainConnection);}
# Include the Autoloader (see "Libraries" for install instructions)require 'vendor/autoload.php';use Mailgun\Mailgun;# Instantiate the client.$mgClient = Mailgun::create('PRIVATE_API_KEY', 'https://API_HOSTNAME');$domain = "YOUR_DOMAIN_NAME";$require_tls = true;$skip_verification = false;# Issue the call to the client.$result = $mgClient->domains()->updateConnection($domain, $require_tls, $skip_verification);
def update_connection(): return requests.put( ("https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection"), auth=('api', 'YOUR_API_KEY'), data={'require_tls': True, 'skip_verification': False})
def update_member RestClient.put("https://api:YOUR_API_KEY" \ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/connection", :require_tls => true, :skip_verification => false)end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class UpdateConnectionChunk{ public static void Main (string[] args) { Console.WriteLine (UpdateConnection ().Content.ToString ()); } public static IRestResponse UpdateConnection () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.Resource = "domains/YOUR_DOMAIN_NAME/connection"; request.AddParameter ("require_tls", true); request.AddParameter ("skip_verification", false); request.Method = Method.PUT; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func UpdateDomainConnection(domain, apiKey string) error { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.UpdateDomainConnection(ctx, domain, mailgun.DomainConnection{ RequireTLS: true, SkipVerification: true, })}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const updatedConnection = await client.domains.updateConnection('DOMAIN', { require_tls: true, skip_verification: false }); console.log('updatedConnection', updatedConnection); } catch (error) { console.error(error); }})();
Sample response:
{ "message": "Domain connection settings have been updated, may take 10 minutes to fully propagate", "require-tls": true, "skip-verification": false}
Get tracking settings for a domain:
curl -s --user 'api:YOUR_API_KEY' -G \ https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/tracking
import com.mailgun.api.v3.MailgunDomainsApi;import com.mailgun.model.domains.DomainTrackingResponse;// ...public DomainTrackingResponse getDomainTracking() { MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(API_KEY) .createApi(MailgunDomainsApi.class); return mailgunDomainsApi.getDomainTrackingSettings(YOUR_DOMAIN_NAME);}
# Currently, the PHP SDK does not support Mailing List verifications.# Consider using the following php curl function.function get_domain_tracking_settings() { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, 'api:PRIVATE_API_KEY'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/tracking'); $result = curl_exec($ch); curl_close($ch); return $result;}
def get_domain_tracking(): return requests.get( "https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/tracking", auth=("api", "YOUR_API_KEY"))
def get_domain_tracking RestClient.get("https://api:YOUR_API_KEY"\ "@api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/tracking"\ {|response, request, result| response }end
using System;using System.IO;using RestSharp;using RestSharp.Authenticators;public class GetDomainTrackingChunk{ public static void Main (string[] args) { Console.WriteLine (GetDomainTracking ().Content.ToString ()); } public static IRestResponse GetDomainTracking () { RestClient client = new RestClient (); client.BaseUrl = new Uri ("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator ("api", "YOUR_API_KEY"); RestRequest request = new RestRequest (); request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment); request.Resource = "/domains/{domain}/tracking"; return client.Execute (request); }}
import ( "context" "github.com/mailgun/mailgun-go/v3" "time")func GetDomainTracking(domain, apiKey string) (mailgun.DomainTracking, error) { mg := mailgun.NewMailgun(domain, apiKey) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() return mg.GetDomainTracking(ctx, domain)}
const DOMAIN = 'YOUR_DOMAIN_NAME';const formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });(async () => { try { const tracking = await client.domains.getTracking(DOMAIN); console.log('tracking', tracking); } catch (error) { console.error(error); }})();
Sample response:
{"tracking": { "click": { "active": false }, "open": { "active": false }, "unsubscribe": { "active": false, "html_footer": "\n<br>\n<p><a href=\"%unsubscribe_url%\">unsubscribe</a></p>\n", "text_footer": "\n\nTo unsubscribe click: <%unsubscribe_url%>\n\n" } }}
FAQs
How do I find my Mailgun API key and domain? ›
First, log in to the Mailgun Control Panel (if you have not already done so). Then, within the left-hand navigation pane, click the Sending option to expand its list of suboptions. Next, click the Domain settings suboption, and then click on the Sending API keys tab.
What is domain in Mailgun? ›Domains are obviously fundamental to begin using your Mailgun account; it's where you're sending from!
How many Domains can you have with Mailgun? ›You can add up to 5 routes.
How do I name my domain API? ›Register a domain name
An API's custom domain name can be the name of a subdomain or the root domain (also known as "zone apex") of a registered internet domain. After a custom domain name is created in API Gateway, you must create or update your DNS provider's resource record to map to your API endpoint.
- Go to the API Console.
- From the projects list, select a project or create a new one.
- If the APIs & services page isn't already open, open the left side menu and select APIs & services.
- On the left, choose Credentials.
- Click Create credentials and then select API key.