Error when trying to create a new client via the API

Hello,
I am using Laravel to create a Client via the api. This is my code below:

$clientData = [
    'name'     => 'Acme Limited',
        'contacts' => [
        'email'      => 'test@test.com',
        'first_name' => 'John',
        'last_name'  => 'Doe',
    ]
];

$response = HttpwithHeaders([
    'X-Api-Token' => 'TOKEN-HERE',
    'X-Api-Secret' => 'SECRET-HERE'
])->post('http://ninja.test/api/v1/clients', $clientData);

if($response->successful()) {
    return $response->body();
} else {
    \Log::error('Create client failed');
    retrun false;
}

Unfortunately, I keep getting an internal server error (500). I checked the Invoice Ninja Server logs and this was the output:

[2021-03-17 12:27:16] production.ERROR: array_key_exists() expects parameter 2 to be array, string given {"userId":1,"exception":"[object] (ErrorException(code: 0): array_key_exists() expects parameter 2 to be array, string given at /var/www/app/app/Http/Requests/Request.php:131)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /var/www/app/app/Http/Requests/Request.php(131): array_key_exists()
#2 /var/www/app/app/Http/Requests/Client/StoreClientRequest.php(86): App\\Http\\Requests\\Request->decodePrimaryKeys()

Would appreciate any assistance in fixing this? Or perhaps an example script that can allow me to add a Client to Invoice Ninja via the API? Thank you.

HI,

I think the problem is that contacts is set to one contact whereas it should be an array of contacts.

1 Like

Awesome! Thanks for that. Works well now. I have a question, I notice that the response I get from the API starts with something like below:

"data": {
    "id": "Opnel5aKBz",
    "user_id": "VolejRejNm",
    "assigned_user_id": "",
    "name": "Arnold Rwakimari",
    "website": "",
    "private_notes": "",
    "balance": 0,
    "group_settings_id": "",
    "paid_to_date": 0,
    "credit_balance": 0,
    "last_login": 0,
    "size_id": "",
    "public_notes": "",
    "client_hash": "Fs0yjFwi0XVwxHRzguOdUlouC9fvEc1iK4lH6qha",

What is the difference between the id field and user_id? Thanks.

Nice, glad to hear it!

The id is identifier for the record whereas user_id is the identifier of the user who created the record.

1 Like

@realnsleo

Here are the API docs in case you need them!

https://app.swaggerhub.com/apis/invoiceninja/invoiceninja

1 Like

Thanks David,
The API docs have been extremely helpful and everything is running smoothly except for one minor issue. I am trying to pass the country_id field to set the country for the client. I am passing the country code using the code below:

$clientData = [
    ...
    'country_id' => 'UG',
]

I notice the API docs state that the field is string and this worked in v4 where it would correctly set the country as per the passed code. However, in v5, I get the response below:

{
    "message": "The given data was invalid.",
    "errors": {
        "country_id": [
            "The country id must be an integer."
        ]
    }
}

My question is, what number is used to identify the different countries. It would also be much cleaner and easier to add a country_iso_code field instead of country_id? Thanks!

Hi There,

You’ll need to do a look up in the countries table and see which ID is required for the country you need.

1 Like

FYI… this is supported in v4, here’s the code if you want to port it.

1 Like

Awesome! Thanks for this!