Templates

On this page:

Get all templates

The /api/templates endpoint lists all your templates.

$ MAILCOACH_TOKEN="your API token"
$ curl https://<your-mailcoach-domain>/api/templates \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json'

Searching for a specific template by its name is possible on this endpoint using ?filter[search]=searchterm. Replace searchterm with the name of your template.

Sorting is possible on this endpoint on name and updated_at. For example ?sort=-updated_at to sort descending on updated_at.

As a result, you get the details of all your templates:

{
    "data": [
        {
            "uuid": "2ca0e737-b907-468f-819f-5d16e6bd6852",
            "name": "alias",
            "html": "<html><head>...</html>\n",
            "structured_html": null,
            "created_at": "2020-08-06T12:11:26.000000Z",
            "updated_at": "2020-08-06T12:11:26.000000Z"
        },
        {
            "uuid": "a69075c6-ff5c-4b6a-b217-12026cb72e4f",
            "name": "consectetur",
            "html": "<html><head>...</html>\n",
            "structured_html": null,
            "created_at": "2020-08-06T12:11:26.000000Z",
            "updated_at": "2020-08-06T12:11:26.000000Z"
        },
        {
            "uuid": "11ce123b-57c4-429b-9840-c79f8047d8aa",
            "name": "velit",
            "html": "<html><head>...</html>\n",
            "structured_html": null,
            "created_at": "2020-08-06T12:11:26.000000Z",
            "updated_at": "2020-08-06T12:11:26.000000Z"
        }
    ],
    "links": {
        "first": "https://<your-mailcoach-domain>/api/templates?page=1",
        "last": "https://<your-mailcoach-domain>/api/templates?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "https://<your-mailcoach-domain>/api/templates",
        "per_page": 15,
        "to": 3,
        "total": 3
    }
}

Get a specific template

If you don’t want to retrieve all templates, you can get a specific template if you know its ID. The example below will get the details of the template with ID 99.

$ MAILCOACH_TOKEN="your API token"
$ curl https://<your-mailcoach-domain>/api/templates/11ce123b-57c4-429b-9840-c79f8047d8aa \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json'

Response:

{
    "data": {
        "id": 99,
        "name": "alias",
        "html": "<html><head>...</html>\n",
        "structured_html": null,
        "created_at": "2020-08-06T12:11:26.000000Z",
        "updated_at": "2020-08-06T12:11:26.000000Z"
    }
}

Add a template

To add a template, create a POST call to the /api/templates/ endpoint.

$ MAILCOACH_TOKEN="your API token"
$ curl -X POST https://<your-mailcoach-domain>/api/templates \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{"name":"Template name", "html":"<html>...</html>"}'

Your request payload should also be a valid JSON. The actual payload, when formatted, looks like this:

{
  "name": "Template name",
  "html": "<html>...</html>"
}

The only required field is the template’s name. Other fields are html and structured_html.

If the API call succeeds, you will be given output like this:

{
    "data": {
        "id": 1,
        "name": "Template name",
        "html": "<html>...</html>\n",
        "structured_html": null,
        "created_at": "2020-08-06T12:11:26.000000Z",
        "updated_at": "2020-08-06T12:11:26.000000Z"
    }
}

Update a template

To update a template, create a PUT call to the /api/templates/<uuid> endpoint. In the example below, we are updating the template with the ID 99.

$ MAILCOACH_TOKEN="your API token"
$ curl -X PUT https://<your-mailcoach-domain>/api/templates/11ce123b-57c4-429b-9840-c79f8047d8aa \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{"name":"Updated name", "html":"<html>...</html>"}'

The only required field is the template’s name. Other fields are html and structured_html.

If the API call succeeds, you will be given output like this:

{
    "data": {
        "id": 99,
        "name": "Updated name",
        "html": "<html>...</html>\n",
        "structured_html": null,
        "created_at": "2020-08-06T12:11:26.000000Z",
        "updated_at": "2020-08-06T12:11:26.000000Z"
    }
}

Delete a template

To delete a template, create a DELETE call to the /api/templates/<uuid> endpoint. In the example below, we are deleting the template with ID 99.

$ MAILCOACH_TOKEN="your API token"
$ curl -X DELETE https://<your-mailcoach-domain>/api/templates/11ce123b-57c4-429b-9840-c79f8047d8aa \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json'

If the API call succeeds, you will be given an empty response with a 204 status code.

Error handling

If an error occurs, you will be given a non-HTTP/200 response code. The resulting payload might look like this:

{
  "message": "The given data was invalid.",
  "errors": {
    "name": [
      "The name field is required."
    ]
  }
}
Subscriber imports