Templates¶
This API allows you to store predefined templates and use them to send messages using the Sending API.
The Templates API endpoint is available at:
v3/<domain>/templates
The API has the following limitations:
- 100 templates per domain
- 10 versions per template
- 100Kb max template size
Templates API¶
Store new template¶
POST /<domain>/templates
This API stores a new template, including its name, description and (optionally) the template content. If the template content is provided, a new version is automatically created and becomes the active version.
Parameter | Description |
---|---|
name | Name of the template being stored. The name can contain alpha characters, digits and next symbols: .-_~ |
description | Description of the template being stored |
template | (Optional) Content of the template |
tag | (Optional) Initial tag of the created version. If the template parameter is provided and the tag is missing, the default value initial is used. The tag can contain alpha-numeric characters and next symbols, such as .-_~ |
engine | (Optional) The template engine used to render the template. This is valid only if the template parameter is provided. Currently, the Templates API supports only the handlebars engine. |
comment | (Optional) Version comment. This is valid only if a new version is being created. (template parameter is provided.) |
Example of storing only the template metadata:
curl -s --user 'api:YOUR_API_KEY' -X POST \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates \
-F name='template.name' \
-F description='template description'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateRequest;
import com.mailgun.model.templates.TemplateWithMessageResponse;
// ...
public TemplateWithMessageResponse createTemplate() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
TemplateRequest request = TemplateRequest.builder()
.name("template.name")
.description("template description")
.build();
return mailgunTemplatesApi.storeNewTemplate(YOUR_DOMAIN_NAME, request);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function create_template() {
$params = array(
'name' => 'template.name',
'description' => 'template description',
);
$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, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def store_template():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
auth=("api", "YOUR_API_KEY"),
data={'name': 'template.name',
'description': 'template description'})
def store_template
RestClient.post "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
:name=> 'template.name',
:description: => 'template description'
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class StoreTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (StoreTemplate ().Content.ToString ());
}
public static IRestResponse StoreTemplate ()
{
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 = "{domain}/templates";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "template.name");
request.AddParameter ("description", "template description")
request.Method = Method.POST;
return client.Execute (request);
}
}
func CreateTemplate(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.CreateTemplate(ctx, &mailgun.Template{
Name: "template.name",
Version: mailgun.TemplateVersion{
Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
Engine: mailgun.TemplateEngineGo,
Tag: "v1",
},
})
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const createdTemplate = await client.domains.domainTemplates.create(DOMAIN, {
name: 'template.test',
description: 'template description',
template: "<div class=\"entry\"> <h1>{{title}}</h1> <div class=\"body\"> {{body}} </div> </div>",
tag: 'v1',
comment: 'comment text'
});
console.log('createdTemplate', createdTemplate);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:13 UTC",
"description": "template description",
"name": "template.name",
},
"message": "template has been stored"
}
Example of storing a template with a version:
curl -s --user 'api:YOUR_API_KEY' -X POST \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates \
-F name='template.name' \
-F description='template description' \
-F template='{{fname}} {{lname}}' \
-F engine='handlebars'
-F comment='version comment'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateRequest;
import com.mailgun.model.templates.TemplateWithMessageResponse;
// ...
public TemplateWithMessageResponse storeVersion() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
TemplateRequest request = TemplateRequest.builder()
.name("template.name")
.description("template description")
.template("{{fname}} {{lname}}")
.engine("handlebars")
.comment("version comment")
.build();
return mailgunTemplatesApi.storeNewTemplate(YOUR_DOMAIN_NAME, request);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function create_template_version() {
$params = array(
'name' => 'template.name',
'description' => 'template description',
'template' => '{{fname}} {{lname}}',
'engine' => 'handlebars',
'comment' => 'version comment'
);
$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, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def store_template():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
auth=("api", "YOUR_API_KEY"),
data={'name': 'template.name',
'description': 'template description',
'template': '{{fname}} {{lname}}',
'engine': 'handlebars',
'comment': 'version comment'})
def store_template
RestClient.post "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
:name => 'template.name',
:description => 'template description',
:template => '{{fname}} {{lname}}',
:engine => 'handlebars',
:comment => 'version comment'
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class StoreTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (StoreTemplate ().Content.ToString ());
}
public static IRestResponse StoreTemplate ()
{
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 = "{domain}/templates";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "template.name");
request.AddParameter ("description", "template description")
request.AddParameter ("template", "{{fname}} {{lname}}")
request.AddParameter ("engine", "handlebars")
request.AddParameter ("comment", "version comment")
request.Method = Method.POST;
return client.Execute (request);
}
}
func AddTemplateVersion(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.AddTemplateVersion(ctx, "template.name", &mailgun.TemplateVersion{
Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
Engine: mailgun.TemplateEngineGo,
Tag: "v2",
Active: true,
})
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
const TEMPLATE_NAME = 'template.name';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const createdTemplateVersion = await client.domains.domainTemplates.createVersion(
DOMAIN,
TEMPLATE_NAME,
{
description: 'template description',
template: '{{fname}} {{lname}}',
tag: 'v2',
comment: 'comment',
active: 'yes'
}
);
console.log('createdTemplateVersion', createdTemplateVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:13 UTC",
"description": "template description",
"name": "template.name",
"version": {
"createdAt": "Wed, 29 Aug 2018 23:31:14 UTC",
"engine": "handlebars",
"tag": "initial",
"comment": "version comment"
}
},
"message": "template has been stored"
}
Get template¶
GET /<domain>/templates/<name>
Returns metadata information about the stored template specified in the url. If the active flag is provided, the content of the active version of the template is returned.
Parameter | Description |
---|---|
active | (Optional) If this flag is set to yes the active version of the template is included in the response. |
Example:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateResponse;
// ...
public TemplateResponse getTemplate() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.getTemplate(YOUR_DOMAIN_NAME, TEMPLATE_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function get_template() {
$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/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def get_template():
return requests.get(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME",
auth=("api", "YOUR_API_KEY"))
def get_template
RestClient.
get("https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME"){|response, request, result| response }
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class GetTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (GetTemplate ().Content.ToString ());
}
public static IRestResponse GetTemplate ()
{
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 = "/{domain}/templates/{name}";
request.AddUrlSegment ("domain", "YOUR_DOMAIN_NAME");
request.AddUrlSegment ("name", "TEMPLATE_NAME");
return client.Execute (request);
}
}
func GetTemplate(domain, apiKey string) (mailgun.Template, error) {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.GetTemplate(ctx, "my-template")
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const domainTemplate = await client.domains.domainTemplates.get(DOMAIN, 'YOUR_TEMPLATE_NAME');
console.log('domainTemplate', domainTemplate);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:13 UTC",
"description": "template description",
"name": "template.name"
}
}
Example of retrieving the active version of a template:
curl -s --user 'api:YOUR_API_KEY' -X GET \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME \
-F active='yes'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateWithVersionResponse;
// ...
public TemplateWithVersionResponse getTemplate() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.getActiveTemplateVersionContent(YOUR_DOMAIN_NAME, TEMPLATE_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function get_active_template() {
$params = array(
'active' => 'yes'
);
$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/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def get_template():
return requests.get(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME",
auth=("api", "YOUR_API_KEY"),
params={"active": "yes"})
def get_template
RestClient.get "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME", :params => {
:active => "yes"
}
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class GetTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (GetTemplate ().Content.ToString ());
}
public static IRestResponse GetTemplate ()
{
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 = "/{domain}/templates/{name}";
request.AddUrlSegment ("domain", "YOUR_DOMAIN_NAME");
request.AddUrlSegment ("name", "TEMPLATE_NAME");
request.AddParameter ("active", "yes");
return client.Execute (request);
}
}
func ListActiveTemplates(domain, apiKey string) ([]mailgun.Template, error) {
mg := mailgun.NewMailgun(domain, apiKey)
it := mg.ListTemplates(&mailgun.ListTemplateOptions{Active: true})
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
var page, result []mailgun.Template
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';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const domainTemplateWithActiveVersion = await client.domains.domainTemplates.get(DOMAIN, 'YOUR_TEMPLATE_NAME', { active: 'yes' });
console.log('domainTemplateWithActiveVersion', domainTemplateWithActiveVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:13 UTC",
"description": "template description",
"name": "template.name",
"version": {
"createdAt": "Wed, 29 Aug 2018 23:31:15 UTC",
"engine": "handlebars",
"tag": "v0",
"template": "{{fname}} {{lname}}",
"comment": "version comment"
}
}
}
Update template¶
PUT /<domain>/templates/<name>
Update the metadata information of the template specified in url.
Parameter | Description |
---|---|
description | Updated description of the template |
Example:
curl -s --user 'api:YOUR_API_KEY' -X PUT \
https://api.mailgun.net/v3/domains/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME \
-F description = 'new template description'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateStatusResponse;
// ...
public TemplateStatusResponse updateTemplate() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.updateTemplate(YOUR_DOMAIN_NAME, TEMPLATE_NAME, "new template description");
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function update_template() {
$params = array(
'description' => 'new template description'
);
$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, 'PUT');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def update_template():
return requests.put(
("https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME"),
auth=('api', 'YOUR_API_KEY'),
data={'description': 'new template description'})
def update_template
RestClient.put("https://api:YOUR_API_KEY" \
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME",
:description => 'new template description')
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class UpdateTemplateChunk
{
public static void Main (string[] args)
{
Console.WriteLine (UpdateTemplate ().Content.ToString ());
}
public static IRestResponse UpdateTemplate ()
{
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 = "/YOUR_DOMAIN_NAME/template/TEMPLATE_NAME";
request.AddParameter ("description", "new template description");
request.Method = Method.PUT;
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func UpdateTemplate(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.UpdateTemplate(ctx, &mailgun.Template{
Name: "TEMPLATE_NAME",
Description: "Add a description to the template",
})
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const updatedDomainTemplate = await client.domains.domainTemplates.update(
DOMAIN,
'YOUR_TEMPLATE_NAME',
{
description: 'new template description'
});
console.log('updatedDomainTemplate', updatedDomainTemplate);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"message": "template has been updated",
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:15 UTC",
"description": "new template description",
"name": "template.name"
}
}
Delete template¶
DELETE /<domain>/templates/<name>
Delete the template specified in the url. NOTE: This method deletes all versions of the specified template.
Example:
curl -s --user 'api:YOUR_API_KEY' -X DELETE \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateStatusResponse;
// ...
public TemplateStatusResponse deleteTemplate() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.deleteTemplate(YOUR_DOMAIN_NAME, TEMPLATE_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function delete_template() {
$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, 'DELETE');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def delete_template
RestClient.delete "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME"
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class DeleteTemplate
{
public static void Main (string[] args)
{
Console.WriteLine (DeleteTemplate ().Content.ToString ());
}
public static IRestResponse DeleteTemplate ()
{
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 = "{domain}/templates/{name}";
request.AddUrlSegment ("name", "TEMPLATE_NAME");
request.Method = Method.DELETE;
return client.Execute (request);
}
}
func DeleteTemplate(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.DeleteTemplate(ctx, "TEMPLATE_NAME")
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const deletedDomainTemplate = await client.domains.domainTemplates.destroy(
DOMAIN,
'YOUR_TEMPLATE_NAME'
);
console.log('deletedDomainTemplate', deletedDomainTemplate);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"name": "template.name"
},
"message": "template has been deleted"
}
View all templates in a domain¶
GET /<domain>/templates
Returns a list of stored templates for the domain
Parameter | Description |
---|---|
page | Name of a page to retrieve. first , last , next , prev |
limit | Number of records to retrieve. Default value is 10 |
p | Pivot is used to retrieve records in chronological order |
Example:
curl -s --user 'api:YOUR_API_KEY' -G \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates \
-d limit=10
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplatesResult;
// ...
public TemplatesResult listTemplates() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.getAllTemplates(YOUR_DOMAIN_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function get_templates() {
$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/YOUR_DOMAIN_NAME/templates');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def list_templates():
return requests.get(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
auth=("api", "YOUR_API_KEY"),
params={"limit": 10})
def list_templates
RestClient.get "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates", :params => {
:limit => 10
}
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class ListTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (ListTemplates ().Content.ToString ());
}
public static IRestResponse ListTemplates ()
{
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.AddParameter ("limit", 10);
request.Resource = "/{domain}/templates";
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func ListTemplates(domain, apiKey string) ([]mailgun.Template, error) {
mg := mailgun.NewMailgun(domain, apiKey)
it := mg.ListTemplates(nil)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
var page, result []mailgun.Template
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';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const domainTemplates = await client.domains.domainTemplates.list(DOMAIN, {
limit: 5
});
console.log('domainTemplates', domainTemplates);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"items": [
{
"createdAt": "Wed, 29 Aug 2018 23:31:15 UTC",
"description": "Template description",
"name": "template.0",
},
{
"createdAt": "Wed, 29 Aug 2018 23:31:18 UTC",
"description": "Template description",
"name": "template.1"
},
{
"createdAt": "Wed, 29 Aug 2018 23:31:21 UTC",
"description": "Template description",
"name": "template.2"
}
],
"paging": {
"first": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates?limit=10",
"last": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates?page=last&limit=10",
"next": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates?page=next&p=template.2&limit=10",
"prev": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates?page=prev&p=template.0&limit=10"
}
}
Delete all templates in a domain¶
DELETE /<domain>/templates
Delete all stored templates for the domain.
Example:
curl -s --user 'api:YOUR_API_KEY' -X DELETE \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.ResponseWithMessage;
// ...
public ResponseWithMessage deleteTemplates() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.deleteAllTemplatesInDomain(YOUR_DOMAIN_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function delete_all_templates() {
$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, 'DELETE');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def delete_templates():
return requests.delete(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates",
auth=("api", "YOUR_API_KEY"))
def delete_templates
RestClient.delete "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates"
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class DeleteTemplatesChunk
{
public static void Main (string[] args)
{
Console.WriteLine (DeleteTemplates ().Content.ToString ());
}
public static IRestResponse DeleteTemplates ()
{
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 = "{domain}/templates";
request.Method = Method.DELETE;
return client.Execute (request);
}
}
// Not implemented
const DOMAIN = 'YOUR_DOMAIN_NAME';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const deletedDomainTemplates = await client.domains.domainTemplates.destroyAll(DOMAIN);
console.log('deletedDomainTemplates', deletedDomainTemplates);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"message": "templates have been deleted"
}
Create new version¶
POST /<domain>/templates/<template>/versions
Create a new version of a template. If the template doesn’t contain any other versions, the first version becomes active.
Parameter | Description |
---|---|
template | Content of the template |
tag | Tag of the version is being created |
engine | (Optional) Template engine. The only engine currently supported is handlebars . |
comment | (Optional) Comments relating to the stored version |
active | (Optional) If this flag is set to yes , this version becomes active |
Example:
curl -s --user 'api:YOUR_API_KEY' -X POST \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions \
-F tag='v0' \
-F template='{{fname}} {{lname}}' \
-F engine='handlebars'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateVersionRequest;
import com.mailgun.model.templates.TemplateWithMessageResponse;
// ...
public TemplateWithMessageResponse storeTemplateVersion() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
TemplateVersionRequest request = TemplateVersionRequest.builder()
.template(TEMPLATE_2)
.tag("v1")
.template("{{fname}} {{lname}}")
.engine("handlebars")
.comment("comment")
.active(true)
.build();
return mailgunTemplatesApi.createNewTemplateVersion(YOUR_DOMAIN_NAME, TEMPLATE_NAME, request);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function create_template_version() {
$params = array(
'tag' => 'v0',
'template' => '{{fname}} {{lname}}',
'engine' => 'handlebars'
);
$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, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def store_template_version():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions",
auth=("api", "YOUR_API_KEY"),
data={'tag': 'v0',
'template': '{{fname}} {{lname}}',
'engine': 'handlebars'})
def store_template_version
RestClient.post "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions",
:tag => 'v0',
:template => '{{fname}} {{lname}}',
:engine => 'handlebars'
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class StoreTemplateVersionChunk
{
public static void Main (string[] args)
{
Console.WriteLine (StoreTemplateVersion ().Content.ToString ());
}
public static IRestResponse StoreTemplateVersion ()
{
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 = "{domain}/templates/{name}/versions";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "TEMPLATE_NAME", ParameterType.UrlSegment);
request.AddParameter ("tag", "v0");
request.AddParameter ("template", "{{fname}} {{lname}}");
request.AddParameter ("engine", "handlebars");
request.Method = Method.POST;
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func AddTemplateVersion(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.AddTemplateVersion(ctx, "TEMPLATE_NAME", &mailgun.TemplateVersion{
Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
Engine: mailgun.TemplateEngineGo,
Tag: "v2",
Active: true,
})
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
const TEMPLATE_NAME = 'template.name';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const createdTemplateVersion = await client.domains.domainTemplates.createVersion(
DOMAIN,
TEMPLATE_NAME,
{
description: 'template description',
template: '{{fname}} {{lname}}',
tag: 'v2',
comment: 'comment',
active: 'yes'
}
);
console.log('createdTemplateVersion', createdTemplateVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:11 UTC",
"description": "template description",
"name": "template.name",
"version": {
"createdAt": "Wed, 29 Aug 2018 23:31:21 UTC",
"engine": "handlebars",
"tag": "v1.0.0",
"comment": "version comment"
}
},
"message": "new version of the template has been stored"
}
Get version¶
GET /<domain>/templates/<name>/versions/<tag>
Retrieve the information and content of the specified version of a template.
Example:
curl -s --user 'api:YOUR_API_KEY' -G \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateWithVersionResponse;
// ...
public TemplateWithVersionResponse getTemplateVersion() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.getSpecifiedVersionTemplateContent(YOUR_DOMAIN_NAME, TEMPLATE_NAME, VERSION_TAG);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function get_template_version() {
$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/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def get_template_version():
return requests.get(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG",
auth=("api", "YOUR_API_KEY"))
def get_template_version
RestClient.get "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG"
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class GetTemplateVersionChunk
{
public static void Main (string[] args)
{
Console.WriteLine (GetTemplateVersion ().Content.ToString ());
}
public static IRestResponse GetTemplateVersion ()
{
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 = "/{domain}/templates/{name}/versions/{tag}";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "TEMPLATE_NAME", ParameterType.UrlSegment);
request.AddParameter ("tag", "VERSION_TAG", ParameterType.UrlSegment);
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func GetTemplateVersion(domain, apiKey string) (mailgun.TemplateVersion, error) {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
// Get the template version tagged as 'VERSION_TAG'
return mg.GetTemplateVersion(ctx, "TEMPLATE_NAME", "VERSION_TAG")
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
const TEMPLATE_NAME = 'template.name';
const VERSION_TAG = 'v2';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const templateVersion = await client.domains.domainTemplates.getVersion(
DOMAIN,
TEMPLATE_NAME,
VERSION_TAG
);
console.log('templateVersion', templateVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:11 UTC",
"description": "template description",
"name": "template.name",
"version": {
"createdAt": "Wed, 29 Aug 2018 23:31:21 UTC",
"engine": "handlebars",
"tag": "v1.1.0",
"comment": "version comment",
"template": "{{fname}} {{lname}}"
}
}
}
Update version¶
PUT /<domain>/templates/<name>/versions/<tag>
Update information or content of the specific version of the template.
Parameter | Description |
---|---|
template | (Optional) The new content of the version |
comment | (Optional) New comment for the provided version |
active | (Optional) If this flag is set to yes this version becomes active |
Example:
curl -s --user 'api:YOUR_API_KEY' -X PUT \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG \
-F template='{{fname}} {{lname}}' \
-F comment='Updated version comment' \
-F active='yes'
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateVersionResponse;
import com.mailgun.model.templates.UpdateTemplateVersionRequest;
// ...
public TemplateVersionResponse updateTemplateVersion() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
UpdateTemplateVersionRequest request = UpdateTemplateVersionRequest.builder()
.template("{{fname}} {{lname}}")
.comment("Updated version comment")
.active(true)
.build();
return mailgunTemplatesApi.updateSpecificTemplateVersion(YOUR_DOMAIN_NAME, TEMPLATE_NAME, VERSION_TAG, request);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function update_template_version() {
$params = array(
'template' => '{{fname}} {{lname}}',
'comment' => 'Updated version comment',
'active' => 'yes'
);
$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, 'PUT');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def update_version():
return requests.put(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG",
auth=("api", "YOUR_API_KEY"),
data={'template': '{{fname}} {{lname}}',
'comment': 'Updated version comment',
'active': 'yes'})
def update_version:
RestClient.put "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG",
:template => '{{fname}} {{lname}}',
:comment => 'Updated version comment',
:active => 'yes'
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class UpdateVersionChunk
{
public static void Main (string[] args)
{
Console.WriteLine (UpdateVersion ().Content.ToString ());
}
public static IRestResponse UpdateVersion ()
{
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 = "{domain}/templates/{name}/versions/{tag}";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "TEMPLATE_NAME", ParameterType.UrlSegment);
request.AddParameter ("tag", "VERSION_TAG", ParameterType.UrlSegment);
request.AddParameter ("template", "{{fname}} {{lname}}");
request.AddParameter ("comment", "Updated version comment");
request.AddParameter ("active", "yes");
request.Method = Method.PUT;
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func UpdateTemplateVersion(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
return mg.UpdateTemplateVersion(ctx, "TEMPLATE_NAME", &mailgun.TemplateVersion{
Comment: "Add a comment to the template and make it 'active'",
Tag: "VERSION_TAG",
Active: true,
})
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
const TEMPLATE_NAME = 'template.name';
const VERSION_TAG = 'v2';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const updatedTemplateVersion = await client.domains.domainTemplates.updateVersion(
DOMAIN,
TEMPLATE_NAME,
VERSION_TAG,
{
template: '{{fname}} {{lname}}',
comment: "Updated version comment",
active: 'yes'
}
);
console.log('updatedTemplateVersion', updatedTemplateVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"name": "template.name",
"version": {
"tag": "v1.2.0"
}
"message": "version has been updated"
}
Delete version¶
DELETE /<domain>/templates/<template>/versions/<version>
Delete a specific version of the template.
Example:
curl -s --user 'api:YOUR_API_KEY' -X DELETE -G \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateVersionResponse;
// ...
public TemplateVersionResponse deleteTemplateVersion() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.deleteSpecificTemplateVersion(YOUR_DOMAIN_NAME, TEMPLATE_NAME, VERSION_TAG);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function delete_template_version() {
$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, 'DELETE');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def delete_template_version():
return requests.delete(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG",
auth=("api", "YOUR_API_KEY"))
def delete_template_version
RestClient.get "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions/VERSION_TAG"
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class DeleteTemplateVersionChunk
{
public static void Main (string[] args)
{
Console.WriteLine (DeleteTemplateVersion ().Content.ToString ());
}
public static IRestResponse DeleteTemplateVersion ()
{
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 = "/{domain}/templates/{name}/versions/{tag}";
request.AddParameter ("domain", "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
request.AddParameter ("name", "TEMPLATE_NAME", ParameterType.UrlSegment);
request.AddParameter ("tag", "VERSION_TAG", ParameterType.UrlSegment);
request.Method = Method.DELETE
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func DeleteTemplateVersion(domain, apiKey string) error {
mg := mailgun.NewMailgun(domain, apiKey)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
// Delete the template version tagged as 'VERSION_TAG'
return mg.DeleteTemplateVersion(ctx, "TEMPLATE_NAME", "VERSION_TAG")
}
const DOMAIN = 'YOUR_DOMAIN_NAME';
const TEMPLATE_NAME = 'template.name';
const VERSION_TAG = 'v1';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const deletedVersion = await client.domains.domainTemplates.destroyVersion(
DOMAIN,
TEMPLATE_NAME,
VERSION_TAG
);
console.log('deletedVersion', deletedVersion);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"name": "template.name",
"version": {
"tag": "v1.3.0"
}
"message": "version has been deleted"
}
View all versions in template¶
GET /<domain>/templates/<template>/versions
Returns a list of stored versions of the template
Parameter | Description |
---|---|
page | Name of a page to retrieve. first , last , next , prev |
limit | Number of records to retrieve. Default value is 10 |
p | Pivot is used to retrieve records in chronological order |
Example:
curl -s --user 'api:YOUR_API_KEY' -G \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions
-d limit=10
import com.mailgun.api.v3.MailgunTemplatesApi;
import com.mailgun.model.templates.TemplateAllVersionsResponse;
// ...
public TemplateAllVersionsResponse listTemplateVersions() {
MailgunTemplatesApi mailgunTemplatesApi = MailgunClient.config(API_KEY)
.createApi(MailgunTemplatesApi.class);
return mailgunTemplatesApi.getAllTemplateVersions(YOUR_DOMAIN_NAME, TEMPLATE_NAME);
}
# Currently, the PHP SDK does not support the Templates endpoint.
# Consider using the following php curl function.
function get_all_versions() {
$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/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
def list_template_versions():
return requests.get(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions",
auth=("api", "YOUR_API_KEY"))
def list_template_version
RestClient.get "https://api:YOUR_API_KEY"\
"@api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/TEMPLATE_NAME/versions"
end
using System;
using System.IO;
using RestSharp;
using RestSharp.Authenticators;
public class ListTemplateVersionsChunk
{
public static void Main (string[] args)
{
Console.WriteLine (ListTemplateVersions ().Content.ToString ());
}
public static IRestResponse GetTemplateVersion ()
{
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.AddParameter ("name", "TEMPLATE_NAME", ParameterType.UrlSegment);
request.Resource = "/{domain}/templates/{name}/versions";
return client.Execute (request);
}
}
import (
"context"
"github.com/mailgun/mailgun-go/v3"
"time"
)
func ListTemplateVersions(domain, apiKey string) ([]mailgun.TemplateVersion, error) {
mg := mailgun.NewMailgun(domain, apiKey)
it := mg.ListTemplateVersions("TEMPLATE_NAME", nil)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
var page, result []mailgun.TemplateVersion
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 TEMPLATE_NAME = 'template.name';
import formData from 'form-data';
import Mailgun from 'mailgun.js';
const mailgun = new Mailgun(formData);
const client = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' || '' });
(async () => {
try {
const domainTemplateVersions = await client.domains.domainTemplates.listVersions(DOMAIN, TEMPLATE_NAME);
console.log('domainTemplateVersions', domainTemplateVersions);
} catch (error) {
console.error(error);
}
})();
Sample response:
{
"template": {
"createdAt": "Wed, 29 Aug 2018 23:31:11 UTC",
"description": "Template description",
"name": "template.name",
"versions": [
{
"createdAt": "Wed, 29 Aug 2018 23:31:21 UTC",
"engine": "handlebars",
"tag": "v0",
"comment": "Version comment"
},
{
"createdAt": "Wed, 29 Aug 2018 23:31:31 UTC",
"engine": "handlebars",
"tag": "v1",
"comment": "Version comment"
},
{
"createdAt": "Wed, 29 Aug 2018 23:31:41 UTC",
"engine": "handlebars",
"tag": "v2",
"comment": "Version comment"
}
]
},
"paging": {
"first": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/z4ujt7CiEeik0RJbspqxaQ/versions?limit=10",
"last": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/z4ujt7CiEeik0RJbspqxaQ/versions?page=last&limit=10",
"next": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/z4ujt7CiEeik0RJbspqxaQ/versions?page=next&p=v2&limit=10",
"prev": "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/templates/z4ujt7CiEeik0RJbspqxaQ/versions?page=prev&p=v0&limit=10"
}
}