# Is it possible to define an extra payload fields directly from TTN application different per device like an API Key

**URL:** <https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757>\
**Category:** Application Development\
**Created:** [October 14, 2018, 12:32am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757 "2018-10-14T00:32:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 14, 2018, 12:32am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/1 "2018-10-14T00:32:20Z")

</div>

Hi

For each of my objects, I need to define a custom field eg: API key of approximately 24 characters in order to save the data in a custom application that uses a REST API. Between TTN and my custom application, I use Node-Red ( This contrib : [https://www.npmjs.com/package/node-red-contrib-ttn](https://www.npmjs.com/package/node-red-contrib-ttn) ) formatting my payload as I want. For some devices it’s easy to do a check according to the msg.dev\_id, I assign the correct API key but I would like to skip this step and define the right API key directly from TTN. All of my devices use the same TTN application.

Ex:

APIKEY “1234567890ABCDEFGHIJLKMNO”

- Device 1
- Device 3
- Device 5

APIKEY “QWERTYUIOPASDFG124565457”

- Device 4
- Device 2

Do you think is possible ?

I’ll try to explain correctly what I need, if you need more explanation, let me know.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 14, 2018, 8:50am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/2 "2018-10-14T08:50:42Z")

</div>

In July 2017, @htdvisser wrote:

> [@htdvisser](#):
>
> You can add “attributes” to your device with ttnctl. This functionality will be added to the console too.

For TTN Console, maybe it’s on the roadmap for version 3? Also, I don’t know if you get these attributes in the MQTT message. You surely won’t get them in the Payload Format functions.

See also the other replies in that topic. And maybe related: [How to best write an application that contains many nodes with different measure data types](https://www.thethingsnetwork.org/forum/t/how-to-best-write-an-application-that-contains-many-nodes-with-different-measure-data-types/6916).

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 14, 2018, 2:13pm UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/3 "2018-10-14T14:13:52Z")

</div>

@arjanvanb “attributes” look like what I wanted. If the attributes (key:value) can be in the MQTT message, I’ll be ok from my side to handle it.

I looked the API reference and I’ll to know how I can make the call API for set the key “mycustomkey” and value “mycustomvalue” for a specific device ?

Ex: ttnctl devices set mydeviceid --attr-set {“mycustomkey”:“mycustomvalue”}

Thanks for you help

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 14, 2018, 3:33pm UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/4 "2018-10-14T15:33:42Z")

</div>

Are you asking how to use the command? The [documentation](https://www.thethingsnetwork.org/docs/network/cli/api.html#ttnctl-devices-set) says:

```nohighlight
--attr-set stringSlice Add a device attribute (key:value)

```

So, I doubt it accepts a JSON value like `{"mycustomkey":"mycustomvalue"}`. Also, the code suggests it just splits the parameter on the first colon between the key and its value:

> <https://github.com/TheThingsNetwork/ttn/blob/v2.10.0/ttnctl/cmd/devices_set.go#L157-L169>

But then, the above also shows that `ttnctl` will log an error telling you what it expects.

If your own command does not work, then I’d try:

```nohighlight
ttnctl devices set mydeviceid --attr-set mycustomkey:mycustomvalue

```

If you know how to use spaces in the value, let us know? Maybe simply `--attr-set mycustomkey:"my custom value"`, but I’ve not tried.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 16, 2018, 5:16pm UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/7 "2018-10-16T17:16:35Z")

</div>

A post was split to a new topic: [“ttnctl devices info” throws “Application not registered to this Handler”](https://www.thethingsnetwork.org/forum/t/ttnctl-devices-info-throws-application-not-registered-to-this-handler/18847)

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 18, 2018, 10:42am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/8 "2018-10-18T10:42:37Z")

</div>

As soon I can find my problem [“ttnctl devices info” throws “Application not registered to this Handler”](https://www.thethingsnetwork.org/forum/t/ttnctl-devices-info-throws-application-not-registered-to-this-handler/18847) I’ll confirm if it’s work @arjanvanb

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 27, 2018, 2:25am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/9 "2018-10-27T02:25:33Z")

</div>

After testing, is not possible to get attribute in MQTT message. I need to find a way to include APIKEY in my MQTT Message per device

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [October 27, 2018, 11:46am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/10 "2018-10-27T11:46:46Z")

</div>

It works for me:

1. [Downloaded the latest version](https://www.thethingsnetwork.org/docs/network/cli/quick-start.html) for the Mac and renamed the unzipped executable to `ttnctl`.
2. Followed [the instructions](https://www.thethingsnetwork.org/docs/network/cli/quick-start.html#register-and-login) to log in.
3. Listed the applications: `ttnctl applications list`
4. Followed [the instructions](https://www.thethingsnetwork.org/docs/network/cli/quick-start.html#selecting-an-application) to select the active application for the subsequent commands: `ttnctl applications select`
5. Listed the active application’s devices: `ttnctl devices list`
6. Executed:
  - `ttnctl devices set my-device-id --attr-set my1stkey:my1stvalue`
  - `ttnctl devices set my-device-id --attr-set my2ndkey:"my 2nd value"`
  - `ttnctl devices set my-device-id --attr-set "my 3rd key":"my 3rd value"`
  - `ttnctl devices set my-device-id --attr-set "my 4th key:my 4th value"`
  - `ttnctl devices set my-device-id --attr-set my5thkey:123`

…and am nicely getting the additional attributes in each uplink I see using `mosquitto_sub -h eu.thethings.network -u my-app-id -P ttn-account-v2.w1...Gk -t my-app-id/devices/+/+`

```json
{
   "app_id": "my-app-id",
   "dev_id": "my-device-id",
   ...
   "payload_raw": "By4HLgkSKXUpmSnKACEAIQApYQ==",
   "metadata": {
      "time": "2018-10-27T11:35:41.447910401Z",
      ...
   },
   "attributes": {
      "my 3rd key": "my 3rd value",
      "my 4th key": "my 4th value",
      "my1stkey": "my1stvalue",
      "my2ndkey": "my 2nd value",
      "my5thkey": "123"
   }
}

```

Also, running `ttnctl devices info my-device-id` nicely shows:

```nohighlight
 Attributes:
          my 3rd key: my 3rd value
          my 4th key: my 4th value
            my1stkey: my1stvalue
            my2ndkey: my 2nd value
            my5thkey: 123

```

So:

- `ttnctl` is quite lenient when it comes to quoting whitespace.
- All values are string values, even when they could be numeric.
- Beware that at most 5 attributes are supported: `Could not set device on Handler: Maximum number of custom attributes (5) exceeded`.

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 27, 2018, 2:31pm UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/11 "2018-10-27T14:31:38Z")

</div>

Really ?? Ok ok let me try it and let you know.

BTW thank you for your help,

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 29, 2018, 1:11am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/12 "2018-10-29T01:11:06Z")

</div>

Hi @arjanvanb following your instructions, yes I am using ttnctl command to create my application, create a device and set attributes on the device and fetch it. It’s work if my handler = ttn-handler-eu. If I set my application to “ttn-handler-us-west” like I want, I am not longer able to register a new device

```
iMac:~ admin$ ttnctl devices register my-device-bob-1
INFO Using Application AppEUI=1234567890 AppID=hello-word-bob
INFO Generating random DevEUI...             
INFO Generating random AppKey...             
INFO Discovering Handler... Handler=ttn-handler-eu
INFO Connecting with Handler... Handler=eu.thethings.network:1904
FATAL Could not register Device error=Could not set device on Handler: Application not registered to this Handler: handler:application:hello-word-bobnot found source=Wrap: /go/src/github.com/TheThingsNetwork/ttn/utils/errors/errors.go:222

```

Why Discovering Handler… is ttn-handler-eu while my application is set to ttn-handler-us-west ?

I think my problem is about the wrong handler

---

<div class="post-metadata">

**Author:** ![gaspar](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/gaspar/32/25844_2.png) [@gaspar](https://www.thethingsnetwork.org/forum/u/gaspar)\
**Post date:** [October 29, 2018, 1:31am UTC](https://www.thethingsnetwork.org/forum/t/is-it-possible-to-define-an-extra-payload-fields-directly-from-ttn-application-different-per-device-like-an-api-key/18757/13 "2018-10-29T01:31:19Z")

</div>

ok @arjanvanb I found on to specify my handler-id using the .ttnctl.yml saved in my home directory. Now, I am able to set a custom attributes and get it in my message object.

Thank you for your help
