Skip to content
© 2026 The ThingsBoard Authors
Try for free

ThingsBoard Cloud

Choose your data region

Your data stays in the region you choose, for residency and compliance. No credit card required.

Rather run it yourself? Install on your own servers

Attributes

The MQTT Attributes API covers three operations: publishing client-side attributes to ThingsBoard, requesting current attribute values from the server, and subscribing to server-pushed attribute updates. See Attributes for the difference between client-side, server-side, and shared attributes, and Getting Connected for connection parameters and credential types.

Report device state (serial number, firmware version, hardware config) to ThingsBoard.

Format Topic
Short v2/a
Standard v1/devices/me/attributes
{"firmwareVersion": "2.1.0", "serialNumber": "SN-4A21F", "hardwareRevision": "B"}
Terminal window
mosquitto_pub -d -q 1 -h "mqtt.thingsboard.cloud" -p 1883 -t "v2/a" -u "$ACCESS_TOKEN" -m '{"firmwareVersion": "2.1.0", "serialNumber": "SN-4A21F"}'

Ask ThingsBoard for the current values of client-side or shared attributes. Useful on device boot to read back configuration stored in ThingsBoard.

Format Subscribe to responses Publish request to
Short v2/a/res/+ v2/a/req/$request_id
Standard v1/devices/me/attributes/response/+ v1/devices/me/attributes/request/$request_id

$request_id is any integer you choose; ThingsBoard echoes it in the response topic so you can match replies to requests.

Request and response payloads:

Request — specify which keys you want:

Field Description
clientKeys Comma-separated client-side attribute keys to return; "" returns all client attributes; omit to exclude the client scope
sharedKeys Comma-separated shared attribute keys to return; "" returns all shared attributes; omit to exclude the shared scope

An empty request body ({}) returns all client and all shared attributes.

Request:

{"clientKeys": "firmwareVersion,serialNumber", "sharedKeys": "targetTemperature,enabled"}

Response:

{"client": {"firmwareVersion": "2.1.0", "serialNumber": "SN-4A21F"}, "shared": {"targetTemperature": 24, "enabled": true}}

The response includes a scope only if that scope returned at least one attribute.

Example

The following example is written in JavaScript and is based on mqtt.js. A pure command-line example is not available: ThingsBoard only accepts a subscription to v1/devices/me/attributes/response/+, and the command-line MQTT clients cannot use a wildcard topic to receive a response.

  1. Save the mqtt-js-attributes-request.js file to your PC.

    The content of the mqtt-js-attributes-request.js file:

    mqtt-js-attributes-request.js
    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://localhost', {
    username: process.env.TOKEN
    });
    var requestId = 1;
    client.on('connect', function () {
    console.log('connected');
    client.subscribe('v1/devices/me/attributes/response/+');
    // clientKeys/sharedKeys are comma-separated strings. An empty value returns
    // ALL keys in that scope, e.g. sharedKeys: "". Omit a field to exclude that scope.
    var request = { clientKeys: 'firmwareVersion,serialNumber', sharedKeys: 'targetTemperature,enabled' };
    client.publish('v1/devices/me/attributes/request/' + requestId, JSON.stringify(request));
    });
    client.on('message', function (topic, message) {
    console.log('response.topic: ' + topic);
    console.log('response.body: ' + message.toString());
    client.end();
    });
  2. Now, follow these steps:

    Install the mqtt package and run the example:

    Terminal window
    npm install mqtt --save
    export TOKEN=$ACCESS_TOKEN
    node mqtt-js-attributes-request.js

Receive pushed notifications whenever a shared attribute is changed from the ThingsBoard side (e.g. an operator changes a setpoint in the dashboard).

Format Subscribe to
Short v2/a
Standard v1/devices/me/attributes

ThingsBoard publishes only the changed keys, request:

Terminal window
mosquitto_sub -d -q 1 -h "mqtt.thingsboard.cloud" -p 1883 -t "v2/a" -u "$ACCESS_TOKEN"

Response:

{"targetTemperature": 26}