TutorialsJoint

Mautic REST API with 3 Type of Authentication – Explained with Postman Examples

May 17, 2021

In this tutorial I am going to explain how you can use Mautic API in order to connect Mautic with any other platform as per requirements. Mautic uses REST API and I assume you already know the basics of REST API as I am not going into details of REST APIs, if you don’t know about it, Here’s a compact overview of REST API by RedHat. My focus in this article will remain mainly on Mautic APIs.

All the details about Mautic REST API and its available endpoints as well as supported authentication is available in official developer documentation. However in this tutorial I’ll show you how to access the API by doing all 3 types of Authentications available.

How to Enable Mautic API ?

The Mautic API is not enabled by default in a fresh instance. If you wish to use Mautic API then you need to first enable it from Configuration section. Following are the steps:

  1. Go to Settings > Configuration > API Settings.
  2. Set API enabled? to YES.
  3. If you wish to use HTTP basic auth set it to YES as well. For the purpose of this tutorial I will be demonstrating basic auth as well, thus I’ll enable it as well.
  4. Save and Close.
  5. Clear the Mautic Cache to ensure the API is enabled and accessible.

mautic settings configuration

mautic configuration api settings

Once API is enabled you should be able to access it using the domain of your Mautic by appending /api on it. for example if your domain is https://mautic.example.com you can access API at https://mautic.example.com/api.

Anything that comes after the /api is known as ‘endpoint’, that says which object you will be accessing, for example ‘contact’ end point is available at https://mautic.example.com/api/contacts. That I’ll show ahead in this tutorial.

If you test the API at this stage by calling endpoint in your browser or postman, you shall see a JSON response saying access denied, that is because you need to have authentication before you can actually access any endpoint. In the next section I’ll show how to authenticate.

{"errors":[{"message":"API authorization denied.","code":401,"type":"access_denied"}]}

Mautic API authentication

In this section I’ll demonstrate what kind of Authentications are available and how to obtain valid authentication in order to access API and call the endpoints.

What type of authentication is available ?

Mautic supports 3 kind of authentication methods, OAuth1a, OAuth2 and Basic HTTP. OAuth2 is most secured way to access API out of all 3. However in this tutorial I’ll show all 3 of them. This article by Synopsys summarizes the key differences between OAuth 1 and 2 in easy words.

So Let’s start with our demonstrations now.

HTTP Basic Authentication in Mautic

This is the easiest of all, once you enable it from the Settings > Configurations > API Settings it becomes super easy to access the API. Basic HTTP method requires only username and password in order to access API. So for example if your username and password is myusername and mypassword then the step goes as follows:

  1. Combine username and password by colon(:) for example myusername:mypassword.
  2. Then base64 encode it, here’s free tool for base64 encoding strings. However assuming your will be doing it with some programming application there will be various methods for the same. the outcome will look something like bXl1c2VybmFtZTpteXBhc3N3b3Jk .
  3. The encoded string generated from step 2 can be used as Authorization header when making requests. the header will look like Authorization: Basic bXl1c2VybmFtZTpteXBhc3N3b3Jk .

The video shows how to use HTTP Basic Auth in Mautic using Postman.

OAuth1 Authentication in Mautic

For Oauth1 you need to obtain the API credentials from Mautic. When in first step you enabled the API for Mautic, a new menu was added in the settings sidebar. This new Menu is called, ‘API Credentials’.

mautic settings api credentials

Mautic API Credentials View

Mautic API Key Credentials

OAuth1 Auth token request

Mautic Auth Login Page

oauth1 access token request postman

Mautic oauth1 request example

OAuth2 Authentication in Mautic

Initial steps for OAuth2 remains same, you need to go to API Credentials section and generate credentials, further steps are also somewhat similar but little difference. Please note that OAuth2 requires HTTPS and a redirect URL, for this tutorial I will be using https://example.com only.

Oauth 2 credentials in mautic

oauth2 keys generated mautic

mautic oauth 2 grant screen

mautic oauth2 access token request

{
"access_token": "ZTRjODFhYWE1Y2NjMmM5ZjYyxxxxxxzODExMGQxMDgyOxxxxxNjc4NjBhOA",
"expires_in": 3600,
"token_type": "bearer",
"scope": null,
"refresh_token": "NGJmZThlMmZiZWQwNWYyNDVjxxxxxx3ZGQ5OTNiMWIxNjxxxxxxTExYQ"
}

mautic oauth2 refresh token postman example

{
"access_token": "M2ZkZmQ1YzMxxxxxxxxMwYmU2NzQ5MTI0MDJkYTYyNw",
"expires_in": 3600,
"token_type": "bearer",
"scope": null,
"refresh_token": "MWJhNTgxNxxxxxxxxZWI3MGQ2NmRlY2NhMTIzNTE5MDIxZA"
}

mautic request example with oauth2

Request examples in Mautic Rest API

Now that you are familiar with the Authentications and able to obtain access token, Let me show you some examples of the Mautic API. In the following section I’ll show you results using Contact API, some results are already shown in the previous sections in the form of example, in this section I’ll demonstrate 4 key type of requests using contact endpoint.

In the examples I’ll be using the same access_token generated in previous section.

GET Request to Mautic contact endpoint

The GET request is simplest of all. You make GET request all the time in browser by accessing any URL. even Contact endpoint can be called in browser as long as valid access_token is available. For example, to get contact details from Mautic about any known contact can be made by accessing following URL in the browser.

https://mautic.example.com/api/contacts/ID?access_token=xxxxxx
{
"contact": {
"isPublished": true,
"dateAdded": "2021-05-18T18:12:46+00:00",
"dateModified": null,
"createdBy": 1,
"createdByUser": "Name",
"modifiedBy": 1,
"modifiedByUser": "Name",
"id": 3,
"points": 0,
"color": null,
"fields": {
"core": {
"firstname": {
"id": "2",
"label": "First Name",
"alias": "firstname",
"type": "text",
"group": "core",
"object": "lead",
"is_fixed": "1",
"properties": "a:0:{}",
"default_value": null,
"value": "mifn1",
"normalizedValue": "mifn1"
},
"lastname": {
"id": "3",
"label": "Last Name",
"alias": "lastname",
"type": "text",
"group": "core",
"object": "lead",
"is_fixed": "1",
"properties": "a:0:{}",
"default_value": null,
"value": "miln1",
"normalizedValue": "miln1"
},
"company": {
"id": "4",
"label": "Primary company",
"alias": "company",
"type": "text",
"group": "core",
"object": "lead",
"is_fixed": "1",
"properties": "a:0:{}",
"default_value": null,
"value": "TestCompany1",
"normalizedValue": "TestCompany1"
},
"email": {
"id": "6",
"label": "Email",
"alias": "email",
"type": "email",
"group": "core",
"object": "lead",
"is_fixed": "1",
"properties": "a:0:{}",
"default_value": null,
"value": "name@example.com",
"normalizedValue": "name@example.com"
},
//and so on
},
"personal": [],
"professional": [],
"all": {
"id": "3",
"honey": null,
"title": null,
"firstname": "mifn1",
"lastname": "miln1",
"company": "TestCompany1",
"position": null,
"email": "name@example.com",
"mobile": null,
"phone": null,
"points": 0,
"fax": null,
"address1": null,
"address2": null,
"city": null,
"state": null,
"zipcode": null,
"country": null,
"preferred_locale": null,
"timezone": null,
"last_active": null,
"attribution_date": null,
"attribution": null,
"website": null,
"facebook": null,
"foursquare": null,
"instagram": null,
"linkedin": null,
"skype": null,
"twitter": null
}
},
"lastActive": null,
"owner": {
"createdByUser": null,
"modifiedByUser": null,
"id": 1,
"username": "mayank",
"firstName": "Mayank",
"lastName": "Tiwari"
},
"ipAddresses": [],
"tags": [],
"utmtags": [],
"stage": null,
"dateIdentified": "2021-05-18T18:12:46+00:00",
"preferredProfileImage": "gravatar",
"doNotContact": [],
"frequencyRules": []
}
}

This is how a complete Mautic contact object JSON looks like. In further examples I’ll not be using full response as that’ll take a lot of space on the page.

GET Request with paging to Mautic contact endpoint

When you try to get a list of objects, It can get messy and huge, thank fully Mautic API provide paging just like in UI. There are multiple options you can pass as parameter to limit the results and also to search and filter just like you do in UI. Let me give an example.

https://mautic.example.com/api/contacts?start=0&limit=5&orderBy=id&minimal=true&where[0][col]=email&where[0][expr]=like&where[0][val]=%mayank%

This URL will return a list of Objects from Mautic. The parameters are explained below.

mautic get list with where example

POST Request to Create Mautic contact

Creating contact or any other object in Mautic is as easy as getting an object. To create a new contact you need to make a POST request with JSON object. Let me show you with an example.

mautic api contact create example

PUT Request to Update Mautic contact

To update the contact same data structure is used but the request changes to PUT and endpoint should contain the ID of the object.

Mautic API edit example

please note that currently there seems to be a bug which updates the field passed in json but erases other fields not passed in json. I have raised a forum entry for this issue. The workaround for this currently is to pass all the fields with values.

DELETE Request to remove Mautic contact

Delete is simple request and doesn’t require much effort, you just need to make a DELETE type request with ID of the object.

Mautic API Delete Example

The Conclusion

Wow, that was a long one. I hope, I was able to demonstrate the API well. The examples in the authentication are standard for any platform like Mautic. OAuth1, Oauth2 and HTTP Basic described in this tutorial works similarly for any other platform which support these type of authentication.

Mautic API examples shown in the article applies to all other endpoints of Mautic as well. However there are few type of endpoints I have not explained but with examples in this tutorial because, i am sure it is sufficient to understand and use those endpoints and their working as well.