The Twig Templates plugin extends the Mautic API, allowing you to manage your custom templates programmatically. This is useful for automation, integration with other systems, or bulk operations.

Ensure you have enabled and configured API access in your Mautic settings and have the necessary permissions for twigtemplates.

Get List of Twig Templates

Retrieves a collection of all configured Twig Templates.

Successful Response (Example)

{
  "twigTemplates": [
    {
      "id": 1,
      "name": "Welcome Email Snippet",
      "description": "Personalized greeting for new contacts",
      "template": "Hello {{ contact.firstname|default('there') }}!",
      "dateAdded": "..."
    },
    {
      "id": 2,
      "name": "Expiry Date Logic",
      // ... other fields
    }
    // ... more templates
  ]
}

Get a Specific Twig Template

Fetches the details of a single Twig Template using its unique ID.

Path Parameters

  • {templateId} (integer, required): The ID of the Twig Template you want to retrieve.

Successful Response (Example)

{
  "twigTemplate": {
    "id": 1,
    "name": "Welcome Email Snippet",
    "description": "Personalized greeting for new contacts",
    "template": "Hello {{ contact.firstname|default('there') }}!",
    "dateAdded": "...",
    "dateModified": "...",
    "createdBy": "...",
    "modifiedBy": "..."
  }
}

Create a Twig Template

Allows you to add a new Twig Template to your Mautic instance.

Request Body Parameters

  • name (string, required): The name for the new template.
  • template (string, required): The Twig code for the template.
  • description (string, optional): A brief description of the template’s purpose.

Example Request Body

{
  "name": "Dynamic Footer",
  "template": "<p>Copyright {{ 'now'|date('Y') }}</p>",
  "description": "Shows the current year in the footer"
}

Successful Response Returns the newly created Twig Template object, similar to the response for GET /api/twigTemplates/{templateId}.

Edit a Twig Template

Updates an existing Twig Template identified by its ID. You only need to send the fields you want to change.

Path Parameters

  • {templateId} (integer, required): The ID of the Twig Template to edit.

Request Body Parameters

  • name (string, optional): The updated name for the template.
  • template (string, optional): The updated Twig code.
  • description (string, optional): The updated description.

Example Request Body (Updating only the template code)

{
  "template": "<p>&copy; {{ 'now'|date('Y') }} Your Company</p>"
}

The documentation states that if the template doesn’t exist, it might create a new one. Please verify this behavior in your Mautic version.

Successful Response Returns the updated Twig Template object.

Delete a Twig Template

Permanently removes a Twig Template from Mautic based on its ID.

Path Parameters

  • {templateId} (integer, required): The ID of the Twig Template to delete.

Successful Response Typically returns a success status code (e.g., 200 OK or 204 No Content) and potentially a confirmation message in the response body.