Account create API data load failure (v4.4.4 | White label)

I have been working on a shell script for synchronizing my google contacts into Invoice Ninja (v4.4.4 | White label) and when I attempt to create the new contact with the API as documented the account is created but no data is pushed into the new record. So basically I get an empty record, any help on what I am doing wrong would be greatly appreciated.

I stared out the personal data but I confirmed that the data is correct with no additional spaces or special characters in it.

Shell Code

function generate_post_data ()
{
cat <<EOF
‘{
“is_owner”:true,
“name”:"$name",
“display_name”:"$name",
“address1”:"$address",
“city”:"$city",
“state”:"$state",
“postal_code”:"$zip",
“contact”:{
“is_owner”:true,
“first_name”:"$fname",
“last_name”:"$lname",
“is_primary”:true,
“phone”:"$phone"
}
}’
EOF
}

Echo command if check and run command if sync

` if [ $process = “sync” ]
then

– CURL Command –

curl --insecure
-X POST -H “Content-Type:application/json”
-H “X-Requested-With: XMLHttpRequest”
-d “$(generate_post_data)”
-H “X-Ninja-Token: c8hgv6cxruxocivoimvftiw1bswvc3qy” “https://192.168.2.250/api/v1/clients
#-------------------#
if [ $? -ge 1 ]; then pcode=1; else pcode=0; fi; wrapper “Invoince Ninja Insert”
else

– CURL Command –

echo “curl --insecure
-X POST -H “Content-Type:application/json”
-H “X-Requested-With: XMLHttpRequest”
-d “$(generate_post_data)”
-H “X-Ninja-Token: c8hgv6cxruxocivoimvftiw1bswvc3qy” “https://192.168.2.250/api/v1/clients””
#-------------------#
fi

Command Sent via Curl

curl --insecure -X POST -H Content-Type:application/json -H X-Requested-With: XMLHttpRequest -d ‘{ “is_owner”:true, “name”:"****an ******din", “display_name”:"**an din", “address1”:"5 **** ", “city”:"Bu", “state”:“MA”, “postal_code”:"0", “contact”:{ “is_owner”:true, “first_name”:"****an", “last_name”:"******din", “is_primary”:true, “phone”:"*****8626" } }’ -H X-Ninja-Token: *******************************qy https://192.168.2.250/api/v1/clients

Maybe try testing by just setting the client name field to see if it works.

Note: the display_name field is read only.

So that was an interesting idea and I modified the code to do it this way; again I get a null entry for the name. It’s a little hard getting the ’ and " quotes correct on the curl call from a shell script but from other posts online I feel confident I have this working as needed.

Command Sent via Curl

curl --insecure -X POST -H Content-Type:application/json -H X-Requested-With: XMLHttpRequest -d ‘{ “name”:"****an ******din" }’ -H X-Ninja-Token: ******************************3qy https://192.168.2.250/api/v1/clients

Response

{
“data”: {
“account_key”: “bRrv4x3oHuAtodtHgKu01KHwKlO59YAP”,
“is_owner”: true,
“user_id”: 1,
“id”: 98,
“name”: null,
“display_name”: “”,
“balance”: 0,
“paid_to_date”: 0,
“updated_at”: 1532603188,
“archived_at”: null,
“address1”: null,
“address2”: null,
“city”: null,
“state”: null,
“postal_code”: null,
“country_id”: 0,
“work_phone”: null,
“private_notes”: null,
“public_notes”: null,
“last_login”: null,
“website”: null,
“industry_id”: 0,
“size_id”: 0,
“is_deleted”: false,
“payment_terms”: 30,
“vat_number”: null,
“id_number”: “AC0098”,
“language_id”: 0,
“currency_id”: 0,
“custom_value1”: null,
“custom_value2”: null,
“invoice_number_counter”: 0,
“quote_number_counter”: 0,
“task_rate”: 0,
“shipping_address1”: null,
“shipping_address2”: null,
“shipping_city”: null,
“shipping_state”: null,
“shipping_postal_code”: null,
“shipping_country_id”: 0,
“show_tasks_in_portal”: false,
“send_reminders”: false,
“credit_number_counter”: 0,
“custom_messages”: “{}”,
“contacts”: [
{
“account_key”: “bRrv4x3oHuAtodtHgKu01KHwKlO59YAP”,
“is_owner”: true,
“id”: 97,
“first_name”: null,
“last_name”: null,
“email”: “”,
“contact_key”: “tp05sbnjulkpfhgwuwrumfcmst5bis0c”,
“updated_at”: 1532603188,
“archived_at”: null,
“is_primary”: true,
“phone”: null,
“last_login”: null,
“send_invoice”: true,
“custom_value1”: null,
“custom_value2”: null
}
]
}
}

I’m not sure, there are two things I noticed looking into this.

  • The quotes posted in your example are incorrect but it could be from the formatting built in the forum plugin we’re using.

  • In my tests the token header needs to be wrapped in quotes (-H “X-Ninja-Token: …”), that said I don’t think it would explain the name being blank.

Wouldn’t not having the token properly stop the creation of an account in total which is not what’s happening here; we are getting an account created but with no data. I am pretty sure this is just because I use echo to output what the command is to screen which removes the " from the statement. The actual curl command is correct.

Agreed.

Not sure… I’d suggest running cURL from the command line and comparing to the examples here:

https://invoice-ninja.readthedocs.io/en/latest/api.html

Ok, I literally copied the output command as provided in the top and it’s works directly from the command line. Well that tells me two things.

  1. The API is working correctly
  2. The command sent from the shell script has some issue with the " or ’ being removed from the bash command

Well I’ll keep testing till I figure this out but thank you for your help it has gotten me closer than I was. Knowing what the problem isn’t is just as valuable as knowing what the problem is.

Figured it out, it was definitely because of ’ and " quotes. The final fix was to adjust the EOF statement to this.

Shell Code

function generate_post_data ()
{
cat <<EOF
{
“is_owner”:true,
“name”:”$name”,
“display_name”:”$name”,
“address1″:”$address”,
“city”:”$city”,
“state”:”$state”,
“postal_code”:”$zip”,
“contact”:{
“is_owner”:true,
“first_name”:”$fname”,
“last_name”:”$lname”,
“is_primary”:true,
“phone”:”$phone”
}
}
EOF
}

Awesome, glad to hear it’s working!

Thanks for sharing the solution.