Mautic Community Forums

Cannot get API to work

Hi there,

I’m stuck and would really appreciate some help.
I’ve installed 2 copies of Mautic on a linode VPS, each on its own domain.

The installations work fine and so far its an awesome product, my only issue is i can’t for the live of me figure out how to implement the API.

I’ve downloaded the zip file at (https://github.com/mautic/api-library), first off, the documentation is a little unclear on where i need to put it.
Does it go onto my Mautic server, in each of the domains or can i use it on my 3rd domain where i’m capturing my leads so that i can API these leads to my mautic instances?

I’ve tried both and so far nothing seems to work.
in someplaces i saw that the URL to include the class files looked like this:
require_once DIR . ‘/lib/Mautic/MauticApi.php’;
however when downloading the api library its should actually just be require_once DIR . ‘/lib/MauticApi.php’; // not sure if i’m missing something.

I have tried all the examples i could find but nothing seems to work for me.

Currently i have the ‘lib’ extraced on my 3rd domain (one without a Mautic installation), and made a script that must just authenticates. (I used this code - https://developer.mautic.org/#oauth-2)
the only difference to the example code is i added the following line to the top of my script.
require_once DIR . ‘/lib/MauticApi.php’;

however the script seems to die on the following code:
$initAuth = new ApiAuth();

My mautic installation uses index.php at the end of my domain so i have tried my baseUrl value as both with and without it.
my ‘callback’ value is the URL to my script.

here is my code:
<?php
require_once DIR . ‘/lib/MauticApi.php’;
echo “<br>------------ 1 --------------<br>”;

use Mautic\Auth\ApiAuth;
echo “<br>----------- 2 ---------------<br>”;

// $initAuth->newAuth() will accept an array of OAuth settings
$settings = array(
‘baseUrl’ => ‘https://One_of_my_mautic_domains/index.php’,
‘version’ => ‘OAuth2’,
‘clientKey’ => ‘Key_found_in_mautic’,
‘clientSecret’ => ‘SecretKey_found_in_mautic’,
‘callback’ => ‘https://My_3rd_domain/mautic_api/auth.php’
);
echo “<br>----------- 3 ---------------<br>”; // this is the last echo that gets displayed
// Initiate the auth object
$initAuth = new ApiAuth();
echo “<br>----------- 4 ---------------<br>”;
$auth = $initAuth->newAuth($settings);
echo “<br>----------- 5 ---------------<br>”;
// Initiate process for obtaining an access token; this will redirect the user to the authorize endpoint and/or set the tokens when the user is redirected back after granting authorization

var_dump($auth);
echo “<br>----------- 6 ---------------<br>”;
if ($auth->validateAccessToken()) {
if ($auth->accessTokenUpdated()) {
$accessTokenData = $auth->getAccessTokenData();

    //store access token data however you want
}

}

Any help would be appreciated.

Let me just add, i’m not able to use composer on the 3rd domain where i’m capturing the data from which i’m trying to API the data to my 2 Mautic domains.

Sorry for responding to my ticket again. I’m not able to edit it.

Ok so i figured it out.
step one - download the API zip to the domain where mautic is installed.
unzip it
enter the unzipped folder and trigger the composer.json file using composer - without this it does not work, this step creates the vendor folder needed to make the calls.
then add your scripts to the unzipped folder.

I tried using OAuth1 & 2 but as my API will run without human interaction as far as possible the login screen that these options provided will cause issues.

i then got an example of basic auth, added in my credentials and it authed without any hassels, then i tried to list my contact and kept on getting an error that said – Looks like I encountered an error (error #404). If I do it again, please report me to the system administrator! – this ended up being a caching issue which was quick to clear.