Unable to add device to group via API?

I am trying to do a simple “add device to group” via the API per the documentation at:

I am not having success. My group name is “Servers” and show’s up in the devicegroups endpoint:

$ curl -H ‘X-Auth-Token: ’ http://:8000/api/v0/devicegroups/
{
“status”: “ok”,
“groups”: [
{
“id”: 1,
“name”: “Servers”,
“desc”: null,
“type”: “static”,
“rules”: {
“condition”: null,
“rules”: ,
“valid”: true,
“joins”:
},
“pattern”: null
}
],
“message”: “Found 1 device groups”,
“count”: 1
}

However when adding a device to the group per the documentation, the route is invalid:

$curl -H ‘X-Auth-Token: ’ -X POST http://:8000/api/v0/devicegroups/Servers/devices -d ‘{“devices”:[78]}’
{
“status”: “error”,
“message”: “This API route doesn’t exist.”
}

Version info as follows:

|Version|[23.11.0 - Sat Nov 25 2023 14:17:34 GMT-0500]
|Database Schema|2023_11_04_125846_packages_increase_name_column_length (273)|
|Web Server||
|PHP|8.1.22|
|Python|3.10.13|
|Database|MariaDB 10.5.23-MariaDB-1:10.5.23+maria~ubu2004|
|Laravel|10.16.1|
|RRDtool|1.7.2|

Also I believe there may be a typo in the documentation, it shows the route as:

/api/v0/devicesgroups/:name/devices

…but the example shows:

/api/v0/devicegroups/devices

I assume “devicegroups” is correct, but neither work for me.

It looks like you are correct, should be devicegroups. So the curl example is good but the route piece of the doc is wrong. It also looks like this was added in late Jan. of 2024 in the following PR. So you’re version is a few months too old.

That explains it - thanks.

Updating did resolve the issue. However the example in the docs is incorrect:

-X POST https://librenms.org/api/v0/devicegroups/devices

…should be:

-X PATCH http://librenms.org/api/v0/devicegroups/:name/

Well, now I’m confused.

The PATCH I showed above replaces everything in the group with whatever new devices you specify. I am unable to figure out how to add a new device to an existing group…

PATCH calls update_device_group, POST calls update_device_group_add_devices. I’m able to add a device with POST.

~ % curl -H 'X-Auth-Token: d72b64d6649141ac022e497700e45953' http://localhost:8000/api/v0/devicegroups/
{
    "status": "ok",
    "groups": [
        {
            "id": 1,
            "name": "New",
            "desc": "new static group",
            "type": "static",
            "rules": null,
            "pattern": null
        }
    ],
    "message": "Found 1 device groups",
    "count": 1
}%

~ % curl -H 'X-Auth-Token: d72b64d6649141ac022e497700e45953' http://localhost:8000/api/v0/devicegroups/New
{
    "status": "ok",
    "devices": [
        {
            "device_id": 2
        }
    ],
    "count": 1
}%

~ % curl -H 'X-Auth-Token: d72b64d6649141ac022e497700e45953' -X POST http://localhost:8000/api/v0/devicegroups/New/devices -d '{"devices":[1]}'
{
    "status": "ok",
    "message": "Devices added"
}%

~ % curl -H 'X-Auth-Token: d72b64d6649141ac022e497700e45953' http://localhost:8000/api/v0/devicegroups/New
{
    "status": "ok",
    "devices": [
        {
            "device_id": 1
        },
        {
            "device_id": 2
        }
    ],
    "count": 2
}%

Thank you! Good to go now.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.