A noobie struggles with Mautic API

I want to start building things with the Mautic API. Specifically, I want to build something that creates/updates a user when Twilio fires a webhook at it.

First, I am confused as to whether or not I actually need to install the Mautic API code or not… There are configuration options for the API in the Mautic Dashboard, but then there is also documentation saying I need to install it.

The Mautic API installer links also seem to have some 404s…

I’m trying to follow along Joey Keller’s “Webhook/Woocommerce” tutorial.

In it, it says to call "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",

In my Mautic install, there is no such directory as …/api

When I make a webhook.php following Joey’s directions, and fire it, nothing happens.

1 Like

Try to capture the response and see what the problem is. Maybe its your payload.
Also: make sure you have api and basic auth turned on.
You dont actually need that path, its a virtual path managed by mautic.

In my install’s public_html directory I made a directory called “connectors.”

/public_html/connections has a file called twilioconnector.php

Even with the default data from the article (except for my URL and user credentials I created, still nothing happens.

API is enabled, as is HTTP basic auth.

Running twilioconnector.php in the servers CLI doesn’t return anything.

<?php
$loginname = 'apiuser';
// Loginname of your API user
$password = 'yourpasswordhere';
// This is the password of the API user
$siteurl = 'yoursiteurl.tld';
// example: mymautic.com

$email = 'captain_enterprise-d@starfleet.com';
$firstname = 'Jean-Luc';
$lastname = 'Pickard';
$tag = 'purchased';

$curl = curl_init();
// Set some options - we are passing in a user agent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/contacts/new",
  CURLOPT_USERAGENT => 'Mautic Connector',
  CURLOPT_POST => 1,
// posting the payload
  CURLOPT_POSTFIELDS => array(
    'firstname' => $firstname,
    'lastname' => $lastname,
    'email' => $email,
    'tags' => $tag
  )
));
curl_exec($curl);

?>

After enabling the api, did you clear the cache?

@oguruma , I am also struggling getting started with Mautic API here. Have you found out the basics? Do you have a minimal example how to communicate with the API - just get the firstname from a contact by ID or something like that?

Aha! That did it. I cleared the cache, and now it works when I fire it from the Mautic server’s CLI.

Now, what I am trying to figure out is how to fire a webhook at it from another source. Eventually I will have Twilio fire a Curl call at it. In the example below, I am using a Wordpress site to fire it. I verified the CURL call works by sending it to my Pipedream. Pipedream received the JSON payload, which for this example, contains only the username and email address.

I have the curl call below

	$mautic_array = [
    'username' => $userdata['user_login'],
    'email' => $userdata['user_email']
    ];


    $url = 'http://mymautic.com/connections/twilioconnector.php';

	$mautic_array_json = json_encode($mautic_array);

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        
          rawurlencode($mautic_array_json)    
               );
        
       

    $resp = curl_exec($ch);
    curl_close($ch);
    return json_decode($resp,true);

When I fire this (which happens when I create a new Wordpress user, in this case), nothing happens. Even when I just leave the “Jean-Luc” variables unchanged.

Can u print your $resp? var_dump($resp);