Hallo Nuki Developer,
I have encountered an issue while using the Nuki Lock API to generate keypad codes. Below, I have provided details of the code and the problem I am facing.
Code Explanation:
I have created a PHP function to generate keypad codes for Nuki smart locks using the Nuki API-Rest. The code consists of two main parts:
generateNukiKeyPad
function: This function is responsible for making a request to the Nuki API to create a keypad code. It includes necessary data such as the lock ID, pin code, and time constraints.- Code Generation: Before calling the
generateNukiKeyPad
function, I generate a random pin code that meets certain criteria (e.g., does not start with ‘12’ or contain ‘0’). The generated pin code is then used in the API request.
function generateNukiKeyPad(array $api_data): array|string|bool
{
try {
// Nuki API endpoint to generate a keypad code
$nukiKeypadCodeUrl = "https://api.nuki.io/smartlock/{$api_data['smartLockId']}/auth";
// Set your Nuki API access token
$accessToken = nukiToken();
// Create an array with the data to send in the request
$data = [
'name' => $api_data['name'], // Replace with a name for your code
'type' => 13, // Fixed value for keypad code
'code' => $api_data['pinCode'],
'allowedFromDate' => $api_data['startDateTime'],
'allowedUntilDate' => $api_data['endDateTime'],
'remoteAllowed' => true, // Allow remote access
'smartActionsEnabled' => true, // Enable smart actions (optional)
];
// Convert the data to JSON format
$jsonData = json_encode($data);
// generate pinCode
$pinCode = rand(111111, 999999);
// check if the pinCode starts with 12 or contains 0
while (substr($pinCode, 0, 2) == 12 || str_contains($pinCode, '0')) {
$pinCode = rand(111111, 999999);
}
// make all the data into an array
$keyPadData = [
'name' => $data['customer']->first_name . " " . $data['customer']->last_name,
'smartLockId' => $data['smartLockId'],
'pinCode' => $pinCode,
'startDateTime' => $start_at->format('Y-m-d\TH:i:s'),
'endDateTime' => $end_at->format('Y-m-d\TH:i:s')
];
$result = generateNukiKeyPad($keyPadData);
Issue:
After sending the generated PIN code to the Nuki API, I received an empty response. While I can see the generated PIN code on the Nuki website, the code does not seem to open the door when entered on the keypad lock itself. To open the door, I need to use the Nuki app or website.
Desired Outcome:
I would like the keypad code to work directly on the lock keypad, allowing me to open the door by entering the code on the lock itself. Currently,
Additional Information:
- I have checked the pin code generation logic, and it meets the specified criteria.
- The API access token is correctly set using the
nukiToken
function. - I have verified that the lock ID and other data being sent to the API are accurate.
Thank you for your support.