Mistake on this page? Email us

Using attributes and filters through the APIs

This page explains how to create device attributes and filters using the Device Management APIs. See the page on the concepts of attributes and filters before following this tutorial.

There are two main ways to use filters through the APIs:

  • By appending the filter (starting with ?) to the URL of a request:
    • Everything after ? is part of the filter.
    • The filter must be URL-encoded.
    • This method does not save the filter for later use.
  • By making a request to POST /v3/device-queries.
    • You define the filter in the message body of the request, not as part of the URL.
    • This method saves the filter for later use.

Creating custom attribute and value pairs

When you create an attribute, you create the attribute and assign its value at the same time. You can assign custom attributes to one device at a time.

Create attributes using PUT /v3/devices/{device_id}, defining the attribute(s) in the message body. When you give the value of a custom attribute:

  • Surround the value in quotation marks.
  • Escape inner quotation marks with a backward slash. See the example for Location below.

Note: You can add custom attributes to one device at a time.

One important thing to understand about attributes and values is that Device Management makes no assumptions about them, and doesn't validate them. If you misspell an attribute or value, you'll create a new one. If you create a new device and try to give its Location attribute the value Camridge (missing b), Device Management will not indicate this error. Similarly, you might create a second attribute by spelling it Locaion (missing t), so you won't find Cambridge by filtering on Location.

To create the attribute Location and the value Cambridge, use the following request:

curl -X PUT
    -H "Authorization: Bearer <api_key>" \
     -H "Content-Type: application/json" \
     -d '{"custom_attributes": "{\"Location\":\"Cambridge\"}"}'

Note: Attribute-value pairs are case-sensitive. The attribute location is distinct from Location.

The device's entry in Device Directory now has "custom_attributes": {"Location": "Cambridge"},.

Filter structure

A filter consists of an attribute, an operator, an equal sign (=), and a value. Separate the operator from the attribute with two underscores (__):

lifecycle_status__eq=enabled // Finds all devices with lifecycle status 'enabled'.


id__neq=016e58d1b1760000000000010014dd97 // Finds devices with a device ID not equal to 016e58d1b1760000000000010014dd97.

If the value of the field contains more than one word, (for example, a two-word name), insert an ampersand between them:


See the full list of attributes and operators.

You can experiment with creating filters and test them using Advanced view in Device Management Portal. The raw query string you create in Portal's advanced filter view will work in your HTTP requests.

Constructing multi-attribute filters

To create a multi-attribute filter, insert an ampersand (&) between attributes. You can also use multiple operators:


or, to find devices where state is registered and lifecycle status is not enabled:


One-time device filters

This method of filtering returns a list of devices, but does not save the filter for later use. Construct the filter, and put it at the end of the URL in your request.

Call `GET /v3/devices?{filter}:

  curl -X POST \
  -H "Authorization: Bearer <api_key>"

This will list devices with lifecycle status enabled. However, you can't use this type of filter to target devices in an update campaign.

Saving filters

Creating a filter using POST /v3/device-queries saves the filter for later use. You can refer to the filter using the id this interface returns. You can also append the filter to the URL of a request.

You can save filters to reuse and edit later through both the APIs and Device Management Portal; this section explains how to save and list filters through the APIs.

The following command creates a filter with:

  • The name Test Filter.
  • The value "mechanism=connector".
curl -X POST \
     -H "Authorization: Bearer <api_key>" \
     -H "Content-Type: application/json" \
     -d '{"name": "Test Filter","query": "mechanism__eq=connector","description": "Filtering by mechanism"}'

This produces a filter entry. The most relevant fields in it are:

   "name":"Test Filter",
   "description":"Filtering by mechanism",

The id field is the ID of the saved filter. You can use this ID to retrieve the filter later:

curl -X GET
-H "Authorization: Bearer <api_key>"

This is also the ID you use to specify which devices you're applying an update to when you create a campaign.

Listing created filters

To list all the filters in your account, call:

curl -X GET
-H "Authorization: Bearer <api_key>"

This produces a list of all your filters:

         "name":"Special Devices",
         "name":"device-query: 0158f2de9992000000000001001001f8",
         "name":"Test Filter",