Mautic API Quick Start Guide

The REST API serves as a powerful gateway to the Mautic system. It supports an ample amount of calls allowing users to programmatically create, read, delete, and modify different components within Mautic.

The API can be used to:

  1. Build meaningful integrations with in-house systems – CRM, ERP, social, and more.
  2. Batch update Mautic components.
  3. Create advanced triggers and alerts.
  4. Achieve the valuable use case that you envision.

This guide allows you to quickly communicate with the Mautic API. For full API documentation, please refer to https://developer.mautic.org/#rest-api.

 

Step 1: Authenticating API Requests

Mautic supports three options for authentication – Basic Authentication, OAuth1a, and OAuth2.

Here, we’ll be using Basic Authentication where your credentials are your Mautic username and password.

To enable Basic Authentication, navigate to Settings -> Configuration -> API Settings and verify that ‘API enabled’ is toggled to ‘Yes’ and ‘Enable HTTP basic auth’ is toggled to ‘Yes’. Copy down your username and password to use in the upcoming sections.

 

Step 2: Some Quick Basics

The base format for the http request URL is:

https://[[your-mautic.com]]/api/[[request-extension]]

Mautic generally supports GET, POST, DELETE, PATCH, and PUT requests which can be examined in more detail in the API documentation.

Successful responses are returned in JSON format.

 

Step 3: GET a Contact’s Profile Data Using the API

Now that we have authentication and the API request URL, let’s talk to the Mautic API!

This query returns the profile data of an individual Mautic contact:

GET https://[[your-mautic.com]]/api/contacts/[[contact-id]]

Choose your preferred environment below and use the code snippets as guides for sending your first Mautic query. Change the [[contact-id]] to the ID of the Mautic contact you want to view. Update your [[username]], [[password]], and [[your-mautic.com]].

cURL
PHP
Python
node.js
Ruby
cURL

curl -X GET https://[[your-mautic.com]]/api/contacts/[[contact-id]] -u [[username]]:[[password]]

PHP

<<?php
use Mautic\\\\MauticApi;
use Mautic\\\\Auth\\\\ApiAuth;

$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);
$apiUrl = "[[https://your-mautic.com]]";
$api = new MauticApi();
$contactApi = $api->newApi("contacts", $auth, $apiUrl);

$contact = $contactApi->get($[[contact-id]]);

Python

import requests

 

url = 'https://[[your-mautic.com]]/api/contacts/[[contact-id]]'

payload = ""
headers = {'Accept': "application/json"}

 

r = requests.request("GET", url, data=payload, auth=('[[username]]','[[password]'))

 

print(r.text)

node.js

var request = require('request');

var username = "[[username]]";
var password = "[[password]]";
var auth = "Basic " + new Buffer.from(username + ":" + password).toString("base64");


var options = {
url: 'https://[[your-mautic.com]]/api/contacts/[[contact-id]]',
headers: {
"Authorization": auth
}
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});

Ruby

require 'uri'
require 'net/http'
require 'openssl'

uri = URI.parse("https://[[your-mautic.com]/api/contacts/[[contact-id]]")


request = Net::HTTP::Get.new(uri)
request.basic_auth '[[username]]','[[password]]'
request.content_type = "application/json"

req_options = {
use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

puts response.body

 

Awesome job! You’re working with the Mautic API and on your way to increasing your Mautic impact. If you have any questions or comments on API use, please contact the Customer Success team at success@mautic.com!