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 turn it on in Configuration -> API Settings in the Mautic UI, or by setting 'api_enable_basic_auth' => true in config/local.php directly.
After enabling Basic Authentication, you can use it in Mautic’s API.
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 the value. For example,
echo -n 'user:password' | base64results indXNlcjpwYXNzd29yZA==. The output varies based on the specific credentials used.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 turning on Mautic’s API, the API Credentials menu item shows up in the administrator menu. Create a Client ID and Secret there, and use them 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 at fixed times of day. All actions get registered under the name that you provided in Settings > API Credentials.
So if you called your API Credential |
Client Credentials flow
Using Mautic’s API library for the Client Credentials flow
Warning
Mautic’s API library doesn’t support this flow yet, but there’s an open PR that adds support. See Client Credentials Support.
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 API requests with OAuth2 is straightforward. Choose one of the following methods that fits the app’s needs.
Other methods
You can also append the access token to the query or include it in 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