which is the correct API path to associate a device to a folder?

Hello everyone, I’m new here and to Syncthing. I’ve been trying for a while now to programmatically (via python) add a new device to a folder, but I’m getting stuck… I can retrieve and create folders without any problems, I verified that my folders and devices ID are correct… So here is one of the bit of code I tried:

# Set the data for the API request
data = {
    "deviceID": device_id,
}

# Send the POST request to add the device to the folder
response = requests.post(f"{host}/rest/config/folders/{folder_id}/devices", json=data, headers=headers)
print(response.text)

the response I get is 404… bad path… additionally, I tried to modify the path to: {host}/rest/config/folders/{folder_id}

I tried to pass the deviceID as a list instead of a string

Can you help me figure out what I’m doing wrong? I cannot find any example anywhere. Thanks in advance for your help

You need to POST the entire folder object to the path ending in the folders, or you can PATCH the folder object with just the devices array to the path ending in folder ID.

https://docs.syncthing.net/rest/config.html#rest-config

3 Likes

thank you so much for your help, it worked perfectly well, for completeness shall anyone else come with the same question here the very simple code I wrote:

    #create the list of devices to add
    device_ids = [device1, device2,]

    # Get the folder configuration of the specified id
response = requests.get(f"{host}/rest/config/folders/{folder_id}", headers=headers)
# Check the status code of the response
if response.status_code == 200:
    # Retrieve the current folder configuration
    folder = response.json()

    # Add the device to the folder
    for device_id in device_ids:
        folder["devices"].append({'deviceID': device_id})

    # Send a PATCH request to update the folder configuration
    patch_response = requests.patch(f"{host}/rest/config/folders/{folder_id}", json=folder, headers=headers)

    # Check the status code of the response
    if patch_response.status_code == 200:
        print("Folder configuration updated successfully")
    else:
        print(f"folder configuration not updated: {patch_response.status_code}")
1 Like

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