# Cannot get API to work

**URL:** <https://forum.mautic.org/t/cannot-get-api-to-work/10587>\
**Category:** Development\
**Created:** [June 14, 2018, 6:18am UTC](https://forum.mautic.org/t/cannot-get-api-to-work/10587 "2018-06-14T06:18:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![n-b-v](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@n-b-v](https://forum.mautic.org/u/n-b-v)\
**Post date:** [June 14, 2018, 6:18am UTC](https://forum.mautic.org/t/cannot-get-api-to-work/10587/1 "2018-06-14T06:18:29Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![n-b-v](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@n-b-v](https://forum.mautic.org/u/n-b-v)\
**Post date:** [June 15, 2018, 2:33am UTC](https://forum.mautic.org/t/cannot-get-api-to-work/10587/2 "2018-06-15T02:33:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![n-b-v](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@n-b-v](https://forum.mautic.org/u/n-b-v)\
**Post date:** [June 15, 2018, 7:35am UTC](https://forum.mautic.org/t/cannot-get-api-to-work/10587/3 "2018-06-15T07:35:38Z")

</div>

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.
