# Unable to create Lead through API

**URL:** <https://forum.mautic.org/t/unable-to-create-lead-through-api/2216>\
**Category:** Development\
**Created:** [August 20, 2015, 5:24pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216 "2015-08-20T17:24:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![domainero](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@domainero](https://forum.mautic.org/u/domainero)\
**Post date:** [August 20, 2015, 5:24pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/1 "2015-08-20T17:24:40Z")

</div>

Hi everyone,  
  
  
  
I’m having some problems trying to create a new Lead through Mautic API and I’m always getting the following error code:

Code:

array(1) { 'error' =\> array(2) { 'code' =\> int(403) 'message' =\> string(45) "access\_denied: OAuth2 authentication required" } }

  
Here is the code I'm using:  

Code:

// I'm loading settings from our container ... It contains client key and secret key, ... $settings = $domainero\_container['mautic.api.auth\_settings'];

$auth = ApiAuth::initiate($settings);

if ($auth-\>validateAccessToken()) {  
if ($auth-\>accessTokenUpdated()) {  
$accessTokenData = $auth-\>getAccessTokenData();  
}  
}

$leadApi = MauticMauticApi::getContext(“leads”, $auth, “[http://domainero.mautic.com/api/](http://domainero.mautic.com/api/)”);

$fields = $leadApi-\>getFieldList();

$data = array();

foreach ($fields as $f) {  
$data[$f[‘alias’]] = ‘’;  
}

$data[‘email’] = ‘some-test-email’;

// Create the lead  
$lead = $leadApi-\>create($data);

var\_dump($lead);

  
What's funny is that everything else seems to be working (retrieving lead by id, listing all leads, ...) but creating does not work. I'm not sure what seems to be the problem? Does it have something to do with the fact that we are using Free membership?  
  
Thanks in advance!  
  
Mihailo

---

<div class="post-metadata">

**Author:** ![domainero](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@domainero](https://forum.mautic.org/u/domainero)\
**Post date:** [August 20, 2015, 5:24pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/2 "2015-08-20T17:24:40Z")

</div>

Hi everyone,

I’m having some problems trying to create a new Lead through Mautic API and I’m always getting the following error code:

```auto
array(1) {
  'error' =>
  array(2) {
    'code' =>
    int(403)
    'message' =>
    string(45) "access_denied: OAuth2 authentication required"
  }
}
```

Here is the code I’m using:

```auto

// I'm loading settings from our container ... It contains client key and secret key, ...
$settings = $domainero_container['mautic.api.auth_settings'];

$auth = ApiAuth::initiate($settings);

if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
    }
} 

$leadApi = MauticMauticApi::getContext("leads", $auth, "http://domainero.mautic.com/api/");

$fields = $leadApi->getFieldList();

$data = array();

foreach ($fields as $f) {
    $data[$f['alias']] = '';
}

$data['email'] = 'some-test-email';

// Create the lead
$lead = $leadApi->create($data);

var_dump($lead);
```

What’s funny is that everything else seems to be working (retrieving lead by id, listing all leads, …) but creating does not work. I’m not sure what seems to be the problem? Does it have something to do with the fact that we are using Free membership?

Thanks in advance!

Mihailo

---

<div class="post-metadata">

**Author:** ![escopecz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/escopecz/32/370_2.png) [@escopecz](https://forum.mautic.org/u/escopecz)\
**Post date:** [August 21, 2015, 7:15am UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/3 "2015-08-21T07:15:44Z")

</div>

I think the problem is that you are not saving and using $accessTokenData. Read more at

[https://www.mautic.org/blog/how-to-use-the-mautic-rest-api/](https://www.mautic.org/blog/how-to-use-the-mautic-rest-api/)  
[https://github.com/mautic/api-library](https://github.com/mautic/api-library)  
[https://developer.mautic.org/#authorization](https://developer.mautic.org/#authorization)

---

<div class="post-metadata">

**Author:** ![domainero](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@domainero](https://forum.mautic.org/u/domainero)\
**Post date:** [August 21, 2015, 12:13pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/4 "2015-08-21T12:13:06Z")

</div>

If that was the case, I wouldn’t be able to execute other API operations, no? I tried initiating the Auth object with Access & Refreshen token as well, but I got the same response from the API.

I can successfully get list of lids, get lead by specific ID, etc. But all “write” operations (create, update, delete) don’t seem to be working.

---

<div class="post-metadata">

**Author:** ![domainero](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@domainero](https://forum.mautic.org/u/domainero)\
**Post date:** [August 21, 2015, 2:55pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/5 "2015-08-21T14:55:27Z")

</div>

Ok I figured it out eventually. Problem was due to the fact that when I was creating an API instance here:

```auto
$leadApi = MauticMauticApi::getContext("leads", $auth, "http://domainero.mautic.com/api/");
```

I was using HTTP protocol instead of HTTPS. Took me a lots of debugging to figure it out LOL. Here’s the code that works:

```auto
$leadApi = MauticMauticApi::getContext("leads", $auth, "https://domainero.mautic.com/api/");
```

Thanks for your help @escopecz !

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [February 2, 2020, 10:58pm UTC](https://forum.mautic.org/t/unable-to-create-lead-through-api/2216/6 "2020-02-02T22:58:44Z")

</div>


