Mautic Community Forums

Creating lead not working. $leadApi->create() method returns an empty array.

I’m using the mautic api from github. The authentication works perfectly. I can retrieve leads via API however when I attempt to create leads nothing happens. I get an empty array in response. How can I fix this?

I’m using the mautic api from github. The authentication works perfectly. I can retrieve leads via API however when I attempt to create leads nothing happens. I get an empty array in response. How can I fix this?

@escopecz perhaps you can help @jjosephs out.

@jjosephs Could you share the code and what values do you send to create a lead? Also, make sure you are using POST, not GET.

@escopecz

I’m using the library found here https://github.com/mautic/api-library . I assume by default it’s sending the data via a POST request.

Below is the array I send over

[ "firstName" => "First" "lastname" => "Last" "email" => "first@last.com" "companyName" => "Acme Hospital" "client_codes" => "A" "client_marketing_codes" => null "city" => "Toronto" "state" => "ON" "client_suspend_reminders" => "False" "client_anniversary_date" => "5/30/2016" "client_membership_in_year" => 19 "client_patient_count" => 0.0 "client_referral_source" => "5 (Slome)" "entity_type" => "CLIENT" "client_classification" => "Standard" "ipAddress" => "127.0.0.1" ]

Good, there is the API Tester PHP app in a subfolder which makes the testing faster. Try it out if you want.

Mautic won’t accept any values which are not in a format as it needs it to be. For example "state" => "ON" is not known value from the state list. If you get in trouble, start small. Try to create a lead with email. Does it work? If so, add a new field. This way you’ll know what field is making the trouble.

Another example: "client_anniversary_date" => "5/30/2016" This is quite confusing date format for computers. Use 2016-05-30. Why? Consider this date: 5/6/2016. This means May 6 (month/day/year) in the US, here in Czech it would mean June 5 (day/month/year).

Thank you for pointing that out @escopecz . Quick question regarding the quote above:

Since we are in Canada, how do we test for Canadian values? Or is the testing limited by American values? Sorry if my questions seems silly.

It doesn’t matter much from what country you are. If you want to know what values you can use for the country field, go to a lead edit form and inspect the country select box there. You will see the available values.

@escopecz I limited the fields to first name, last name and email and I still got a blank response. Could the ipAddress field being set to 127.0.0.1 be the issue? I tried to send over the data with and without it and got the same response.

Why would I get a blank response when trying to create a lead? Shouldn’t an error come back if something is wrong? I can get fields and data with no issues.

@escopecz Perhaps this error log helps?

[2016-01-24 04:44:59] mautic.CRITICAL: Uncaught PHP Exception OAuth2OAuth2ServerException: “redirect_uri_mismatch” at /path/to/mautic/vendor/friendsofsymfony/oauth2-php/lib/OAuth2.php line 1164 {“exception”:"[object] (OAuth2\OAuth2ServerException(code: 0): redirect_uri_mismatch at /path/to/mautic/vendor/friendsofsymfony/oauth2-php/lib/OAuth2.php:1164)"} []

Maybe the authentication is not done properly. Seems the redirect URL saved in the OAuth credentials in Mautic is not equal to the redirect URL you are sending with the auth request. Test the API tester as I mentioned above.

@escopecz when the remember token I can access the field list so I should be able to utilize the create leads method with same remember token procedure.

when with

@jjosephs could you write a note whether you’ve tried the API Tester and if it worked from there? It could tell us if the problem is in your code or in Mautic.

I know it’s an old topic but i had the same issue using the api package from github and found a solution.
I could search leads without any issue. But when trying to create new leads, i got error 400 bad request.

The problem was the field alias i was sending… If they don’t exist in mautic, you get the error 400. (beware of camel case)
You get the same result with the api tester.

In your exemple data you should have firstname and not firstName, also the field alias companyName don’t exist in mautic, it’s just company (except if you created this field but in this case mautic remove the camel case when saving the field alias and it would be companyname)

so something like this

[ "firstname" => "First" "lastname" => "Last" "email" => "first@last.com" "company" => "Acme Hospital" // or this if it's a custom field "companyname" => "Acme Hospital" //... snip ... "ipAddress" => "127.0.0.1" // This one is the exception, and require the camel case. ]

So yeah… double check your fields aliases ! :slight_smile: