Authentication
Mautic supports OAuth2 or Basic Authentication for API authentication.
Basic authentication
To get started quickly with Mautic’s API, you can use Basic Authentication.
Note
Mautic recommends OAuth2 for security reasons. If you still want to use Basic Authentication, you must first enable it in Configuration -> API Settings
in the Mautic UI, or by setting 'api_enable_basic_auth' => true
in app/config/local.php
directly.
After enabling Basic Authentication, you can use it in Mautic’s API as follows:
Using Mautic’s API library with BasicAuth
<?php
use GuzzleHttp\Client;
use Mautic\Auth\ApiAuth;
use Mautic\MauticApi;
// Initiate an HTTP Client
$httpClient = new Client([
'timeout' => 10,
]);
// Initiate the auth object
$settings = [
'userName' => 'YOUR_USERNAME',
'password' => 'YOUR_PASSWORD'
];
$apiUrl = 'https://mautic.example.com';
$initAuth = new ApiAuth($httpClient);
$auth = $initAuth->newAuth($settings, 'BasicAuth');
$api = new MauticApi();
$contactsApi = $api->newApi('contacts', $auth, $apiUrl);
$contacts = $contactsApi->getList();
Plain HTTP requests
Combine the username and password of a Mautic User with a colon
:
. For example,user:password
.Base64 encode this value. For example, with
echo -n 'user:password' | base64
. This outputs something likedXNlcjpwYXNzd29yZA==
.Add an Authorization header to each API request as
Authorization: Basic dXNlcjpwYXNzd29yZA==
Here’s an example:
curl -H "Authorization: Basic dXNlcjpwYXNzd29yZA==" https://mautic.example.com/api/contacts
OAuth2
After enabling Mautic’s API, the “API Credentials” menu item shows up in the administrator menu. You can create Client ID and Secret there, which you can then use in the next steps.
Note
Mautic supports the authorization_code
, refresh_token
and client_credentials
grant types.
There are two main flows that Mautic supports:
Name |
Description |
---|---|
Authorization code flow |
This flow is best if you want Users to log in with their own Mautic accounts. All actions taken get registered as if the User performed them in Mautic’s UI. |
Client Credentials flow |
This flow is best for Machine-to-Machine, M2M, communications. For example, in cron jobs that run on at fixed times of the day.
All actions get registered under the name that you provided in |
Client Credentials flow
Using Mautic’s API library for the Client Credentials flow
Warning
Mautic’s API library doesn’t have support yet for this flow, but there’s an open PR that adds support: https://github.com/mautic/api-library/pull/269
Using plain OAuth2 for the Client Credentials flow
To obtain a new access token, make a POST request to the access token’s endpoint oauth/v2/token
using the client_credentials
grant type.
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET" \
https://mautic.example.com/oauth/v2/token
The response returned should be a JSON encoded string:
{
"access_token": "NEW_ACCESS_TOKEN",
"expires_in": 3600,
"token_type": "bearer",
"scope": ""
}
Authenticating the API Request
Authenticating the API request with OAuth2 is easy. Choose one of the following methods that’s appropriate for the app’s needs.
Other methods
You can also append the access token to the query or include it the POST body, but only when using x-www-form-unencoded
.
GET https://mautic.example.com/api/leads?access_token=ACCESS_TOKEN
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "firstname=John&lastname=Smith&access_token=ACCESS_TOKEN" \
https://mautic.example.com.com/api/leads/new