# Add device attribute using Device: patch

**URL:** <https://forums.losant.com/t/add-device-attribute-using-device-patch/4559>\
**Category:** Help\
**Tags:** workflow, device\
**Created:** [May 30, 2022, 11:19am UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559 "2022-05-30T11:19:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 30, 2022, 11:19am UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/1 "2022-05-30T11:19:54Z")

</div>

How can I set device attributes without loosing my current device attributes using the API Node Device: patch (or Devices: patch)?  
I did this:

But its not working when the attribute doesn’t exists a priori.

```auto
let { key } = payload.value;

let indicatorsQuery = {
    "email": payload.experience.user.email,
    "query": {
        "id": payload.data.params.machineId,
    },
    "updateOperations": [
        {
            "operation": "set",
            "target": "attributeTag",
            "value": {
                "key": 'type',
                "value": 'indicator'
            },
            "attributeName": key
        }
    ],
}

payload.indicators = indicatorsQuery;

```

“indicators” is the input of Devices: patch node.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [May 31, 2022, 12:06pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/2 "2022-05-31T12:06:58Z")

</div>

Two ways to add, edit, or remove attributes from a device via the REST API:

1. [Device: Patch](https://docs.losant.com/rest-api/device/#patch) - This method requires passing up the entire array of attributes with the request - as in, you must first [fetch the device](https://docs.losant.com/rest-api/device/#get), operate on the correct array item, and then make the patch request with the full edited array in the body.
2. [Devices: Patch](https://docs.losant.com/rest-api/devices/#patch) - This is the bulk update endpoint (for updating multiple devices at once), but you can use it for your case as well.

I just tested out your example and it worked for me; can you verify the value of “key” is the name of an attribute on the device with the ID found at “data.params.machineId”? What is the response you are getting from the operation (if you put a payload path in the “Response” section and add a Debug Node after)?

---

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 31, 2022, 12:48pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/3 "2022-05-31T12:48:27Z")

</div>

No, the value of “key” isnt the name of an attribute, but that is the point. I want to add the attribute when the attribute doesn’t already exists.

---

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 31, 2022, 12:56pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/4 "2022-05-31T12:56:42Z")

</div>

I know that what I was doing was setting attribute tags, but my idea is to add attributes. I was just trying to do this using the updateOperations because I was thinking: what if i want to add attribute with attribute tags when the attribute doesnt exists a priori?

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [May 31, 2022, 12:56pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/5 "2022-05-31T12:56:46Z")

</div>

In that case, the issue is you are using the wrong operation; in your code sample, you are attempting to add an [attribute tag](https://docs.losant.com/devices/attributes/#including-optional-fields) to an existing attribute, not adding a new attribute to the device.

Instead, the item you pass in the “updateOperations” array should look like the following:

```json
{
  "operation": "add",
  "target": "attribute",
  "value": {
    "name": "nameOfNewAttribute",
    "dataType": "number|string|gps|bool|blob",
    "attributeTags": {
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 31, 2022, 12:57pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/6 "2022-05-31T12:57:49Z")

</div>

Nice Dylan, that will not overwrite/remove my current device attributes, right?

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [May 31, 2022, 12:59pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/7 "2022-05-31T12:59:47Z")

</div>

No, following that syntax it will only add the attribute and will leave the remaining ones in place.

When it comes to building these bulk update queries, I like to create the operation first in the user interface, submit the request, and then view how it is shaped in the browser’s “Network” tab. That’s what I did in this case as well. Just a tip for working with this endpoint in the future.

---

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 31, 2022, 1:00pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/8 "2022-05-31T13:00:26Z")

</div>

This attributeTags object have the entries key and value?

> [@Dylan\_Schuster](#):
>
> ```auto
> "attributeTags": {
> }
> 
> ```

For example:

```auto
"attributeTags": {
    "key": key,
    "value": value
}

```

---

<div class="post-metadata">

**Author:** ![Igo\_Monteiro](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@Igo\_Monteiro](https://forums.losant.com/u/Igo_Monteiro)\
**Post date:** [May 31, 2022, 1:03pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/9 "2022-05-31T13:03:25Z")

</div>

Yepp, that is exactly what i’m doing.  
My interface have a form that the user set this attributes and values, so firstly the attributes doesnt exists.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [May 31, 2022, 1:03pm UTC](https://forums.losant.com/t/add-device-attribute-using-device-patch/4559/10 "2022-05-31T13:03:40Z")

</div>

No, `attributeTags` should be shaped as:

```json
{
  "attributeTags": {
    "key1": "value1",
    "key2": "value2"
  }
}

```
