Skip to content
Stand with Ukraine flag

Gateway API Overview

A ThingsBoard Gateway is a device that maintains a single MQTT connection to ThingsBoard while proxying data for multiple physical devices connected behind it. All gateway topics use the v1/gateway/ prefix and all payloads include the downstream device name.

The gateway authenticates with its own credentials — see MQTT Getting Connected for connection and authentication details.

The gateway can also use the standard MQTT Device API to publish its own telemetry and attributes and to receive RPC commands for itself.

Before a device can use the gateway topic namespace, it must have Is gateway enabled in ThingsBoard. Toggle this flag when creating the device or in the device edit form — see Devices for details.

When Overwrite activity time is also enabled, the gateway’s own connection activity keeps all connected downstream devices in Active state — even when those devices are not sending telemetry. This ensures RPC commands and shared attribute updates can be delivered to downstream devices as long as the gateway is connected.

TopicDirectionPurpose
v1/gateway/connectPublishAnnounce a downstream device connection
v1/gateway/disconnectPublishAnnounce a downstream device disconnection
v1/gateway/telemetryPublishUpload telemetry for one or more devices
v1/gateway/attributesPublishUpload client-side attributes for devices
v1/gateway/attributesSubscribeReceive shared attribute updates for devices
v1/gateway/attributes/requestPublishRequest client/shared attribute values
v1/gateway/attributes/responseSubscribeReceive attribute request responses
v1/gateway/rpcSubscribeReceive server-side RPC commands for devices
v1/gateway/rpcPublishSend RPC command responses
v1/gateway/claimPublishInitiate device claiming

Inform ThingsBoard that a downstream device is now connected. ThingsBoard begins routing shared attribute updates and RPC commands for that device through the gateway.

Topic: v1/gateway/connect

{"device": "Device A", "type": "Sensor A"}
FieldRequiredDescription
deviceYesDevice name in ThingsBoard
typeNoDevice profile name. Defaults to default if omitted.

If the device does not exist, ThingsBoard creates it automatically. If the profile does not exist, it is also created automatically.

Terminal window
mosquitto_pub -h "$THINGSBOARD_HOST" -t "v1/gateway/connect" -u "$ACCESS_TOKEN" -m '{"device":"Device A"}'

Connect with a specific device profile:

Terminal window
mosquitto_pub -h "$THINGSBOARD_HOST" -t "v1/gateway/connect" -u "$ACCESS_TOKEN" -m '{"device":"Device A","type":"Sensor A"}'
CodeNamePacketsDescription
128Unspecified errorCONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECTAn unspecified error occurred.
129Malformed PacketCONNACK, DISCONNECTThe received packet cannot be correctly parsed according to the protocol specification.
130Protocol ErrorCONNACK, DISCONNECTData that the protocol does not allow or behavior that does not conform to protocol requirements.
131Implementation specific errorCONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECTThe packet is valid but not accepted by the current receiver’s implementation.
132Unsupported Protocol VersionCONNACKThe requested protocol version is not supported by the server.
133Client Identifier not validCONNACKThe Client ID is valid but not accepted by the server (e.g., exceeds maximum length).
134Bad User Name or PasswordCONNACKThe client was refused because it used an incorrect username or password.
135Not authorizedCONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, DISCONNECTThe operation is not authorized (e.g., no permission to publish to this topic).
136Server unavailableCONNACKThe server is currently unavailable.
143Topic Filter invalidPUBACK, PUBREC, DISCONNECTThe Topic Filter format is correct but not accepted by the server.
144Topic Name invalidPUBACK, PUBREC, DISCONNECTThe Topic Name format is correct but not accepted by the client or server.
149Packet too largeCONNACK, DISCONNECTThe packet exceeds the maximum size agreed upon at connection time.
151Quota exceededCONNACK, PUBACK, PUBREC, SUBACK, DISCONNECTAn administratively imposed limit has been exceeded.
153Payload format invalidPUBACK, PUBREC, DISCONNECTThe payload format does not match the Payload Format Indicator property.

Inform ThingsBoard that a downstream device is no longer active. ThingsBoard stops sending attribute updates and RPC commands for that device to the gateway.

Topic: v1/gateway/disconnect

{"device": "Device A"}
Terminal window
mosquitto_pub -h "$THINGSBOARD_HOST" -t "v1/gateway/disconnect" -u "$ACCESS_TOKEN" -m '{"device":"Device A"}'