Tago API listing devices with filter

Hi all,

I’m doing an external integration for a client, so I’m using the Rest API documentation, specificially the GET method for https://api.tago.io/device.

I need to retrieve and filter all the devices with a specific tag (on this case, the tag is

[{"key": "company_id","value": "client_company_id"}]

However, reading the documentation ([List of Devices | TagoIO Docs)) does not provide a good example of how to use the filter. I can retrive individual ids by using filter[id] = ${device.id} but I couldnt make it work for filter[tags]. Any ideas?

(As a workaround, I retrieve all my devices and then I filter them out by searching each tag using native NodeJS tools, but maybe it’s not the best approach).

Hello zsu[@user:10038971975]zsu,

To use the API to filter out tags you want to structure your queryparameters like this:

https://api.tago.io/device?page=1&fields[0]=id&fields[1]=name&filter[tags][0][key]=company_id&filter[tags][0][value]=client_company_id

Hope that answers your question!

Hi Vitor. This works perfecly, thank you.

By the way, is there a way to do the same on the SDK?

Hi ischacht,

Yes, you can achieve this by using the device list method. Here’s an example code snippet along with a reference to the documentation:
Device List Documentation

const { Analysis, Resources } = require("@tago-io/sdk");

async function listDevicesByTag(scope, context) {
  const devices = await Resources.devices.list({
    fields: ["id", "name"],
    filter: {
      tags: [{ key: "company_id", value: "client_company_id" }],
    },
  });

  if (!devices.length) {
    return console.debug("Devices not found");
  }

  console.debug(JSON.stringify(devices));
}

This script will help you list devices filtered by a specific tag, such as company_id. If no devices are found, it will log “Devices not found”.