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 the Mautic API library
Read the Using Basic Authentication to interact with Mautic’s API.
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
Authorizationheader to each API request. 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, 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 suits Machine-to-Machine - M2M - communications such as Cron jobs. Mautic registers all actions under the name provided in Settings > API Credentials. For example, a credential named |
Client Credentials flow
Using the Mautic API library for Client Credentials flow
Warning
Mautic’s API library doesn’t support this flow yet, but you can track the progress in the Client Credentials Support PR.
Using the standard OAuth2 for Client Credentials flow
To obtain a new access token, send a POST request to the token 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
Client credentials response
{
"access_token": "NEW_ACCESS_TOKEN",
"expires_in": 3600,
"token_type": "bearer",
"scope": ""
}
Note
Store this data securely and use it to authenticate future API requests.
Authenticating the API request
Authenticating API requests with OAuth2 is straightforward. Choose one of the following methods.
Other methods
While the preferred method is to send the access token in the Authorization: Bearer header, you may alternatively include it in the POST body when using application/x-www-form-urlencoded if your client cannot set custom headers. Avoid putting access tokens in URL query strings, since server logs, browser history, and Referer headers often record URLs and can inadvertently expose your 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/api/leads/new