Web API Example: Manage PIN-Codes for your Nuki Keypad

Hi!

Just tested it with

{"name": "Test", "type": 13, "code": 999999, "allowedFromDate": "2020-03-19T16:00:00.000Z", "allowedUntilDate": "2020-03-20T16:00:00.000Z","allowedWeekDays": 0}

and had no issue creating.

The problem seems to arise when you miss to add the "allowedWeekDays": 0

Thanks, that helped!
As a question, could the api may improved with a debug parameter where helpful error messages come back? That would help debugging such problems without the Forum.

Thanks for your time and assistance!

Hi. I succeed to get the smartlock Id without problems, but when I try to create new pinpad key I recieve the following error code: 400 BAD REQUEST.
My code in PHP:
<?php
$url = ‘https://api.nuki.io/smartlock/593895460/auth’;
$ch = curl_init($url);
$data = array(
‘name’ => ‘TEST’,
‘type’ => 13,
‘code’ => 999999,
‘allowedFromDate’ => ‘2020-12-01T16:00:00.000Z’,
‘allowedUntilDate’ => ‘2020-12-09T16:00:00.000Z’,
‘allowedWeekDays’ => 0
);
$jsonData = json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Accept: application/json’,‘Content-Type: application/json’,‘Authorization: Bearer dcbe2e69424747028b92b9bfdef444cefc26127cf7dd3c35153006ebd35694ef0254d21cefc65665’));
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,“PUT”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
Can somebody tell me where is the probelm? Thank you.

Can you also provide the Body of the response? It should contain more details on the reason the request failed.

Hi Stephan,

First of all I wish to thank you for these solutions and to your help.
I found the problem. There is.

The curlopt_put is working with files, and server aspect for file. I used just CURLOPT_CUSTOMREQUEST and everything is working fine.

The complete solution to create access codes in php:

Best regards,
Zoli B.

1 Like

Hello Stephan,
The expired (after “allowedUntilTime”) entry codes are automatically deleted, or we have to find them and delete one by one?
Thank you,
Zoli B.

No, currently there is no option to create authorizations which are automatically deleted after end-date has passed. You have to have your own routine doing that.

Thank you. A cron job will solve the problem.

Hello, thanks for the API. It works well, except for the list of users. I request the https://api.nuki.io/smartlock/SMARTLOCK_ID/auth URL to retrieve the list of “users”, but it looks like the list is cached and doesn’t reflect the current list of PINs/users in the application. The response headers contain the Date 2 hours older than the current date and the Expires header is set to Jan 01 1970 which means never, right? Am I missing something here? Thanks a lot.

Hi @kordys!

The list is cached server-side, but changes should be updated from the device. In doubt you can try a

POST /service/{serviceId}/sync

(Note: This will sync complete data directly from the device, meaning it should not be used too often to avoid battery drain.)

to check if data is correctly updated then.

Thanks Stephan for the reply. Apparently, the cache is not always flushed correctly? I will give it a try. Maybe it was just some sort of glitch on my side.

Hello all

Maybe someone can help me further. I am trying to edit the code on the keypad via the web API.

The authentication and reading from the smartlockId works fine. With this information I then try to send the following command via PHP and Curl:

$curl = curl_init(‘https://api.nuki.io/smartlock/’.$smartlockId.’/auth/2’);

$data = array(
‘name’ => ‘TEST’,
‘type’ => ‘13’,
‘code’ => ‘987654’,
‘allowedFromDate’ => ‘2020-12-01T16:00:00.000Z’,
‘allowedUntilDate’ => ‘2020-12-09T16:00:00.000Z’,
‘allowedWeekDays’ => ‘127’
);
$jsonData = json_encode($data);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, false);

curl_setopt($curl, CURLOPT_HTTPHEADER, [“Authorization: Bearer $authToken”]);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST,“PUT”);
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);
echo $response; // Ausgabe fĂźr Test
curl_close($curl);

Unfortunately, I get the error message: {“code”:405,“description”:“The method specified in the request is not allowed for the resource identified by the request URI”,“homeRef”:"/",“reasonPhrase”:“Method Not Allowed”,“uri”:“HTTP/1.1: Status Code Definitions”}

What could be the reason for this?

Thanks for the help.

Greetings Jason

Quick shot in the dark:
Could it be you mixed up id and authId in the URL you call?

If it’s not that I can try to reproduce the error with my test setup later.

Hi Stephan

I took the “authId” (a short number). But if i take the “id” (a lot of numbers), i get the same error:

{“code”:405,“description”:“The method specified in the request is not allowed for the resource identified by the request URI”,“homeRef”:"/",“reasonPhrase”:“Method Not Allowed”,“uri”:“HTTP/1.1: Status Code Definitions”}

If it’s possible, i can send you the URL with the “real” ids as direct message?

Thank you.

Yes, please as DM. I can check the format against my test setup.

I found the error!

Instead of:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST,“PUT”);

I use:
curl_setopt($curl, CURLOPT_POST, 1);

Instead of:
curl_setopt($curl, CURLOPT_HTTPHEADER, [“Authorization: Bearer $authToken”]);

I use:
curl_setopt($curl, CURLOPT_HTTPHEADER, [“Content-Type: application/json”,“Authorization: Bearer $authToken”]);

And i use the id, not the autId. Now it works!

Thank you!

Nice to hear you found it!

I missed that at first, but that explains it, yes.

Dear All!
We have an office.
The office has meeting rooms.
The meeting rooms are rented on an hourly basis.
Is there an access control system/app that is compatible with smart lock 3.0 Pro and manages access on an hourly basis? In order not to have to develop a custom API.
Zsolt