How can I use the Mautic API?

I looked for documentation on the Mautic API. I wasn’t able to find documentation. So, I looked through the source for anything API related. I saw that there are references to authentication via OAuth/OAuth2 and API Clients Objects.



Do I have to write an integration or is there something more like a REST API where I can do simple actions?



Are there also endpoints for like updating a lead? [Most important]

Firing off an email?

etc.



I also looked at the calls that the Mautic UI itself is doing via Firebug. These seem to be proprietary. I was hoping I could do something like that and use that to update a lead.

I looked for documentation on the Mautic API. I wasn’t able to find documentation. So, I looked through the source for anything API related. I saw that there are references to authentication via OAuth/OAuth2 and API Clients Objects.

Do I have to write an integration or is there something more like a REST API where I can do simple actions?

Are there also endpoints for like updating a lead? [Most important]
Firing off an email?
etc.

I also looked at the calls that the Mautic UI itself is doing via Firebug. These seem to be proprietary. I was hoping I could do something like that and use that to update a lead.

If you are trying to integrate something outside of Mautic, we have an oAuth based API here https://github.com/mautic/api-library ALTHOUGH, apparently beta3 didn’t install some of the necessary columns for oauth2 to work which is now fixed in the coming beta4. If you don’t have Mautic running under ssl, oauth1a is recommended anyway.

So, as stated, it is oauth based as most of the major CRMs, etc are rather than REST. We may implement REST in the future, but for now, oauth seems to be the leading standard (almost all the social networks, CRMs, etc seem to be using it). But of course, we’re open to discussing alternatives!

If you’re wanting to develop something to work from within Mautic, like an addon, that gets into a whole new level and more to come on that once we get the “stable” product complete (so I can write appropriate documentation without having to go back and change everything :-))

Thanks!
Alan

I’m fine with oAuth. As I understand oAuth, it is for authorization. That doesn’t tell me how to pass data to Mautic, such as updating a lead.

This may be easier with a use case. Imagine that I have certain pages in my website that are topic based. I have a a custom field on my lead with a list of topics. If a lead hits one of those topic pages, then I’ll want to update the field on the lead with the list of topics to include that topic. Then, perhaps I’ll have a smart list to email people about those topics in the future.

I have Mautic on the same domain as the website and it does have ssl certificate.

Long story short, I’m asking, what can I do after I connect to Mautic via oAuth?

Check out that link I posted (https://github.com/mautic/api-library). It gives you code examples on how to update a lead after authenticating with Mautic.

Is the lead already logged in on your own website? That will be the kick -> how to connect the lead on your website with the lead in Mautic. There’s a couple ways to do this. If you have the tracking pixel embedded, you can try snagging the lead ID via cookies. Of course that depends on your user’s browser settings.

Getting a lead ID via cookies is two fold. First, get the value of mautic_session_id then get the value of that ID.

$sessionId = $_COOKIE['mautic_session_id'];
$leadId      = $_COOKIE[$sessionId];

Then you can use the API to push updates to the lead:

$leadApi = MauticApi::getContext("leads", $auth, 'http://mymautic.com');

$updatedData = array(
    'field_alias' => 'value to update'
);

$result = $leadApi->edit($leadId, $updatedData);

If you have identifying information, like an email for a user logged into your site, or you are doing the update after the fact when the user is no longer browsing, then you can do a search for the ID.

$leadApi = MauticApi::getContext("leads", $auth, 'http://mymautic.com');

$leads = $leadApi->getList(array('email' => 'user@emial.com'));
if (!empty($leads)) {
    $leadId = $leads[0]['id'];
    $result = $leadApi->edit($leadId, $updatedData);
} else {
    $data = array(
    	'firstname'   => 'Bob',
    	'email'       => 'user@email.com'
    	'ipAddress'   => $_SERVER['REMOTE_ADDR'],
    	'field_alias' => 'custom value'
    );
    
	$lead = $leadApi->create($data);
	
	//Maybe store $lead['id'] in local user profile so you don't have to fetch again
}

That code is quickly written and thus may not work out of the box :slight_smile:

Thanks!
Alan

https://github.com/mautic/api-library

Thanks. That helps a lot. Totally missed that repo.

Let us know if you run into issues with it.

Thanks!
Alan

In regards to easy updates of leads, I had forgot about this (so many hidden features I’ve forgotten about).

See https://www.mautic.org/community/index.php/123-logged-in-users (the additional lead field support will be in next release where fields can be set to be publicly updatable via the /p/mtracking.gif query).

Thanks,
Alan

I’m trying to connect Zapier with mautic, there’s a beta plugin, I can connect to mautic.net test install I have, but my self hosted version of mautic it doesn’t work, it’s like the Api is broke, also there’s no where to change configuration to oAuth2 I see referenced?

So the zapier app is trying to go to http://MyWebSite.com/oauth/v2/authorize but these folders don’t exist in my self hosted site, I’ve install the API library from github in the root of the site, so MyWebSite.com/lib exists etc but in the API library there is no oauth/v2/ folder?