Hello,
I’m encountering an issue where the ChirpStack API creates duplicate applications instead of adding new devices to an existing application. Here’s a concise breakdown of the problem:
Technical Issue
- Setup: We use the API to create applications based on a unique application name and add devices to the application.
- Expected Behavior: If an application with the specified name already exists, the API should add new devices to this application.
- Problem: Instead of reusing the existing application, the API frequently creates a duplicate application with the same name and only adds devices to the new duplicate application.
Steps to Replicate
- Attempt to create or retrieve an application by name.
- Check if an application with that name already exists before creating a new one.
- Add multiple devices to this application.
Request
Any insights into avoiding duplicate application creation through the API would be greatly appreciated. If others have encountered similar issues or have suggestions for ensuring that devices are consistently added to existing applications, I’d welcome your feedback.
Thank you!
Sounds like a code error rather than a API error, if you are using the create device request it would not make an additional application even if it was not there. Likely there is an issue with your logic for checking if the application is already created.
Maybe share your code and it could provide some more insight?
A more robust way to ensure unique-ness instead of using a different names is to use the application ID, which is innately unique.
Thanks, Liam, for your reply. I’m sharing the code for you to review. However, regardless of the accuracy of the code, still making two applications with the same name has to be impossible, returning an error. Duplicated applications are actually different considering their IDs, but when calling the API to make new applications, there is no way to get their IDs, I can only check the name:
<?php
// Function to check if an application exists in ChirpStack by name, and create if it doesn't
function chirpstack_check_create_application($app_name) {
// API URL for ChirpStack
$api_url = 'http://lns1.thingseye.io:8090/api/applications';
// ChirpStack API key
$api_key = 'Bearer My ChirpStack API key
// Tenant ID
$tenant_id = 'My ChirpStack tenant ID
// First, check if the application already exists
$existing_application_id = chirpstack_find_application($app_name, $api_key);
if ($existing_application_id) {
error_log("Application $app_name already exists with ID: $existing_application_id.");
return $existing_application_id; // Return the existing application ID
}
// If not, proceed to create the application
error_log("Creating new ChirpStack application: $app_name");
$headers = [
'Grpc-Metadata-Authorization: ' . $api_key,
'Content-Type: application/json'
];
$data = [
"application" => [
"name" => $app_name,
"tenantId" => $tenant_id,
"description" => "Application for " . $app_name
]
];
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
error_log('ChirpStack API Error: ' . curl_error($ch));
return false;
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_body = json_decode($response, true);
curl_close($ch);
if ($http_code == 200 || $http_code == 201) {
return $response_body['id']; // Return the new application ID
} else {
error_log("Failed to create ChirpStack application: " . json_encode($response_body));
return false;
}
}
// Function to find an existing application in ChirpStack by name
function chirpstack_find_application($app_name, $api_key) {
$api_url = 'http://lns1.thingseye.io:8090/api/applications?limit=100&name=' . urlencode($app_name);
$headers = [
'Grpc-Metadata-Authorization: ' . $api_key,
'Content-Type: application/json'
];
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
error_log('ChirpStack Find Application Error: ' . curl_error($ch));
return false;
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_body = json_decode($response, true);
curl_close($ch);
if ($http_code == 200) {
foreach ($response_body['result'] as $application) {
if ($application['name'] == $app_name) {
return $application['id']; // Return the application ID if found
}
}
}
return false; // Return false if the application doesn't exist
}
?>