Create user with PHP script / Guzzle

Hi,
I want to add a user over php script like as follow.

<?php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new GuzzleHttp\Client();
$res = $client->request(
            'PUT',
            'https://api.nuki.io/account/user',
            [
                'headers' => [
                    'Accept'     => 'application/json',
                    'Authorization: Bearer' => 'My API token',
                    'd' => '{"email": "mail@company.com","name": "Max Mustermann"}'
                ],
            ]
        );
        
?>

But I get this Error

Client error: PUT https://api.nuki.io/account/user resulted in a 400 Bad Request response: {“code”:400,“description”:“The request could not be understood by the server due to malformed syntax”
Can you help me please ?

BR Ertan

Not a PHP dev, so just a quick guess, but it seems to me like you have the body (user email/name) in the headers instead of in the request body, where they should be.

See http://docs.guzzlephp.org/en/stable/request-options.html#body for how to send a body using Guzzle.

1 Like

Same guess from my side (as I already wrote via PM): The JSON should be in the body.

Hi @joshuajung, hi @Stephan, thank you for your reply.

This is the solution I found out:

$client = new GuzzleHttp\Client();
$client->request(
	'PUT', 
	'https://api.nuki.io/account/user',
	[
        'headers' => [
            'Content-Type' => 'application/json',
            'Accept'     => 'application/json',
            'Authorization: Bearer' => 'My API token',
        ],
		'body' => json_encode([
			'email' => 'mail@company.com',
			'name' => 'Max Mustermann', 
			'language' => 'de'
		])                
	]
);

In the Swagger UI is described that a parameter type exists. 0 = user / 1 = company.
If you use this parameter then you got an error as follow:

400 Bad Request response: {“detailMessage”:"The supplied value ‘0’ for parameter ‘type’ is not valid

If you don’t use this parameter then the code above works and a user will be created.

BR Ertan

Just another wild guess: make sure you’re sending the type parameter as an integer, not as a string. Likely like this:

'body' => json_encode([
	'email' => 'mail@company.com',
	'name' => 'Max Mustermann', 
	'language' => 'de',
	'type' => 0 // Note that there are no quotes here
])
1 Like

@joshuajung Thank you for your notice,
but even if I use with quotes ‘type’ => ‘0’ or not ‘type’ => 0
I get the same error message

Client error: PUT https://api.nuki.io/account/user resulted in a 400 Bad Request response: {“detailMessage”:“The supplied value ‘0’ for parameter ‘type’ is not valid”,“stackTrace”:[],“suppressedExceptions”:[]}

Ah. My bad, sorry. - I finally noticed the issue:

This option is only needed for special Nuki Web account types and so is not usable when the API token used to create the account user is not of this type.

This is something which needs to be made clear in the documentation and I will try to get an update for that as soon as possible.

For your case please just ignore the type.

@Stephan Thank you for your Information. :+1: