REST
The REST Connector starts an HTTP(S) server on the gateway, creates API endpoints, and forwards incoming request data to ThingsBoard as device attributes and telemetry. It can also push data to external HTTP(S) APIs based on attribute updates and RPC commands received from ThingsBoard.
To enable this connector, add it to the connectors list in tb_gateway.json — see the
General Configuration reference.
Connector configuration: rest.json
Section titled “Connector configuration: rest.json”The connector reads its settings from a JSON file. Below is a full example:
{ "server": { "host": "127.0.0.1", "port": "5000", "SSL": false, "security": { "cert": "~/ssl/cert.pem", "key": "~/ssl/key.pem" } }, "mapping": [ { "endpoint": "/my_devices", "HTTPMethods": [ "POST" ],
"response": { "responseExpected": false, "timeout": 120 }, "security": { "type": "anonymous" }, "converter": { "type": "json", "deviceInfo": { "deviceNameExpressionSource": "request", "deviceNameExpression": "${sensorName}", "deviceProfileExpressionSource": "request", "deviceProfileExpression": "${sensorType}" }, "attributes": [ { "type": "string", "key": "model", "value": "${sensorModel}" }, { "type": "string", "key": "certificateNumber", "value": "${certificateNumber}" } ], "timeseries": [ { "type": "double", "key": "temperature", "value": "${temp}" }, { "type": "double", "key": "humidity", "value": "${hum}" } ] } }, { "endpoint": "/anon1", "HTTPMethods": [ "GET", "POST" ],
"response": { "responseExpected": false, "timeout": 120 }, "security": { "type": "anonymous" }, "converter": { "type": "json", "deviceInfo": { "deviceNameExpressionSource": "constant", "deviceNameExpression": "Device 2", "deviceProfileExpressionSource": "constant", "deviceProfileExpression": "default" }, "attributes": [ { "type": "string", "key": "model", "value": "Model2" } ], "timeseries": [ { "type": "double", "key": "temperature", "value": "${temp}" }, { "type": "double", "key": "humidity", "value": "${hum}" } ] } } ], "requestsMapping": { "attributeRequests": [ { "endpoint": "/sharedAttributes", "type": "shared", "HTTPMethods": [ "POST" ],
"security": { "type": "anonymous" }, "timeout": 10, "deviceNameExpression": "${deviceName}", "attributeNameExpression": "${attribute}${attribute1}" } ], "attributeUpdates": [ { "HTTPMethod": "POST", "SSLVerify": false, "httpHeaders": { "Content-Type": "application/json" },
"security": { "type": "anonymous" }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": "SN.*", "attributeFilter": ".*", "requestUrlExpression": "http://127.0.0.1:5001/", "valueExpression": "{\"deviceName\":\"${deviceName}\",\"${attributeKey}\":\"${attributeValue}\"}" }, { "HTTPMethod": "POST", "SSLVerify": false, "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "cert", "cert": "/thingsboard_gateway/config/cert.pem", "key": "/thingsboard_gateway/config/key.pem" }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": "CERT.*", "attributeFilter": ".*", "requestUrlExpression": "https://127.0.0.1:5002/", "valueExpression": "{\"deviceName\":\"${deviceName}\",\"${attributeKey}\":\"${attributeValue}\"}" }, { "HTTPMethod": "POST", "SSLVerify": false, "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5003/oauth2/token", "clientId": "client_id", "clientSecret": "client_secret", "scope": "read write", "clientAuthentication": "header", "tokenPrefix": "Bearer", "tokenRefreshRatio": 0.75, "tokenRequestTimeout": 10, "tokenRetryDelay": 10, "defaultExpiresIn": 3600 }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": "OAUTH2.*", "attributeFilter": ".*", "requestUrlExpression": "http://127.0.0.1:5003/", "valueExpression": "{\"deviceName\":\"${deviceName}\",\"${attributeKey}\":\"${attributeValue}\"}" } ], "serverSideRpc": [ { "deviceNameFilter": ".*", "methodFilter": "echo", "requestUrlExpression": "http://127.0.0.1:5001/${deviceName}", "responseTimeout": 1, "HTTPMethod": "GET", "valueExpression": "${params}", "timeout": 10, "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "anonymous" } }, { "deviceNameFilter": "SN.*", "methodFilter": "post_attributes", "requestUrlExpression": "http://127.0.0.1:5000/my_devices", "responseTimeout": 1, "HTTPMethod": "POST", "valueExpression": "{\"sensorName\":\"${deviceName}\", \"sensorType\":\"${params.sensorType}\", \"sensorModel\":\"${params.sensorModel}\", \"certificateNumber\":\"${params.certificateNumber}\", \"temp\":\"${params.temp}\", \"hum\":\"${params.hum}\"}", "timeout": 10, "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "anonymous" } }, { "deviceNameFilter": ".*", "methodFilter": "no-reply", "requestUrlExpression": "http://127.0.0.1:5001/${deviceName}/request/${methodName}/${restId}", "HTTPMethod": "POST", "valueExpression": "${params}", "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "anonymous" } } ] }}Section “server”
Section titled “Section “server””Configures the HTTP(S) server the connector starts on the gateway.
| Parameter | Default | Description |
|---|---|---|
host |
127.0.0.1 |
IP address or hostname of the server |
port |
5000 |
Port to listen on |
SSL |
true |
Enable TLS |
security.cert |
Path to the PEM certificate file | |
security.key |
Path to the private key file |
{ "server": { "host": "127.0.0.1", "port": 5000, "SSL": true, "security": { "cert": "~/ssl/cert.pem", "key": "~/ssl/key.pem" } }}| Parameter | Default | Description |
|---|---|---|
host |
127.0.0.1 |
IP address or hostname of the server |
port |
5000 |
Port to listen on |
SSL |
false |
Disable TLS |
{ "server": { "host": "127.0.0.1", "port": 5000, "SSL": false }}Section “mapping”
Section titled “Section “mapping””An array of endpoint definitions. Each entry creates one HTTP endpoint on the gateway server, defines the allowed HTTP methods, the authorization scheme for incoming requests, and how to convert the request payload into ThingsBoard device data.
| Parameter | Default | Description |
|---|---|---|
endpoint |
/test_device |
URL path of the endpoint |
HTTPMethods |
["GET"] |
Allowed HTTP methods (GET, POST, etc.) |
Subsection “security”
Section titled “Subsection “security””Per-endpoint authorization for incoming client requests.
The connector expects an Authorization: Basic <base64(username:password)> header on every request.
| Parameter | Default | Description |
|---|---|---|
type |
basic |
Authorization type |
username |
username |
Username |
password |
password |
Password |
{ "security": { "type": "basic", "username": "username", "password": "password" }}Example curl request with Basic auth:
curl -X POST http://127.0.0.1:5000/test_device \ -u username:password \ -H "Content-Type: application/json" \ -d '{"name": "SN-001", "sensorModel": "example"}'No authorization is required. Useful for testing or trusted internal networks.
| Parameter | Default | Description |
|---|---|---|
type |
anonymous |
Authorization type |
{ "security": { "type": "anonymous" }}Subsection “converter”
Section titled “Subsection “converter””Defines how the incoming request payload is converted to ThingsBoard device data.
The default converter. It parses a JSON (or multipart/form-data) request payload and maps
fields to device attributes and telemetry.
Device info parameters:
| Parameter | Default | Description |
|---|---|---|
deviceNameExpression |
Device ${name} |
Expression to build the device name; ${field} reads from the request payload |
deviceNameExpressionSource |
request |
request — read from payload; constant — treat expression as a fixed value |
deviceProfileExpressionSource |
request |
Same as above, but for the device profile |
deviceProfileExpression |
default |
Expression to resolve the device profile name |
Attributes and timeseries parameters:
Both sections share the same field structure:
| Parameter | Default | Description |
|---|---|---|
key |
model / temperature |
Key name in ThingsBoard |
type |
string / double |
Data type of the incoming value |
value |
${sensorModel} / ${temp} |
JSON-path expression to extract the value from the request |
tsField |
(Optional) JSON-path expression for a field that carries a datetime string; used as the telemetry timestamp | |
dayfirst |
false |
(Optional) When parsing tsField, treat the first number as the day (true) or month (false) |
yearfirst |
false |
(Optional) When parsing tsField, treat the first number as the year |
Multipart/form-data support:
You can send multipart/form-data requests without any extra configuration. The connector
merges all multipart fields into a single key-value object, so you can reference them exactly
the same way as with application/json.
The two requests below are equivalent:
curl -X POST http://127.0.0.1:5000/test_device \ -H "Content-Type: application/json" \ -u username:password \ -d '{ "name": "SensorA", "sensorModel": "TX100", "temp": 23.5, "hum": 56.2, "timestampField": "10.11.24 14:30:00.000" }'curl -X POST "http://127.0.0.1:5000/test_device" \ -u "username:password" \ -F 'name=SensorA' \ -F 'sensorModel=TX100' \ -F 'temp=23.5' \ -F 'hum=56.2' \ -F 'timestampField=10.11.24 14:30:00.000'Full mapping example:
{ "endpoint": "/test_device", "HTTPMethods": ["POST"], "security": { "type": "basic", "username": "username", "password": "password" }, "converter": { "type": "json", "deviceInfo": { "deviceNameExpression": "Device ${name}", "deviceNameExpressionSource": "request", "deviceProfileExpressionSource": "request", "deviceProfileExpression": "default" }, "attributes": [ { "key": "model", "type": "string", "value": "${sensorModel}" } ], "timeseries": [ { "key": "temperature", "type": "double", "value": "${temp}" }, { "key": "humidity", "type": "double", "value": "${hum}", "tsField": "${timestampField}", "dayfirst": true } ] }}You can also combine multiple request fields into a single value using expressions:
{ "key": "combine", "type": "string", "value": "${hum}:${temp}" }Use a custom converter when the request payload has a non-standard structure or requires
preprocessing before sending data to ThingsBoard. Pass any custom fields in extensionConfig
— they are forwarded to the converter class at initialization.
| Parameter | Description |
|---|---|
type |
Converter type, custom |
extension |
Name of your converter class |
extensionConfig |
Settings for your converter class; their structure is defined by the class |
{ "type": "custom", "extension": "CustomRestUplinkConverter", "extensionConfig": { "key": "temperature" }}Subsection “response”
Section titled “Subsection “response””Optional. Controls what HTTP response body the gateway returns to the client after processing the request. Three variants are supported:
1. Default — returns only an HTTP status code (no response body). No extra configuration needed.
2. Hardcoded response body — always returns the same static text:
| Parameter | Default | Description |
|---|---|---|
response.successResponse |
OK |
Body returned when the status is 200 |
response.unsuccessfulResponse |
Error |
Body returned when the status is not 200 |
{ "endpoint": "/test_device", "HTTPMethods": ["POST"], "security": { "type": "anonymous" }, "converter": { "type": "json" }, "response": { "successResponse": "OK", "unsuccessfulResponse": "Error" }}3. Advanced — ThingsBoard rule chain response: the gateway forwards the request to ThingsBoard, waits for a rule chain to set a shared attribute, and returns that attribute value as the HTTP response.
| Parameter | Default | Description |
|---|---|---|
response.responseExpected |
true |
Enable advanced response mode |
response.timeout |
120 |
Seconds to wait for the ThingsBoard response |
response.responseAttribute |
result |
Shared attribute name whose value is returned as the response body |
{ "endpoint": "/test_device", "HTTPMethods": ["POST"], "security": { "type": "anonymous" }, "converter": { "type": "json" }, "response": { "responseExpected": true, "timeout": 120, "responseAttribute": "result" }}To use the advanced response, configure a rule chain in ThingsBoard that sets the shared
attribute named in responseAttribute on the device:
Section “requestsMapping”
Section titled “Section “requestsMapping””Optional. Configures attribute requests, attribute updates, and server-side RPC handling.
Subsection “attributeRequests”
Section titled “Subsection “attributeRequests””Lets a device request its own client-side or shared attributes from ThingsBoard by sending an HTTP request to a dedicated endpoint on the gateway.
| Parameter | Default | Description |
|---|---|---|
endpoint |
/sharedAttributes |
URL path of the attribute request endpoint |
type |
shared |
Attribute type: shared or client |
HTTPMethods |
["POST"] |
Allowed HTTP methods |
security.type |
anonymous |
Security type: basic or anonymous. See Subsection “security” |
timeout |
10.0 |
Request timeout in seconds |
deviceNameExpression |
${deviceName} |
JSON-path expression to extract the device name from the request |
attributeNameExpression |
${attribute} |
JSON-path expression to extract the attribute name from the request |
"attributeRequests": [ { "endpoint": "/sharedAttributes", "type": "shared", "HTTPMethods": ["POST"], "security": { "type": "anonymous" }, "timeout": 10.0, "deviceNameExpression": "${deviceName}", "attributeNameExpression": "${attribute}" }]To request multiple attributes in a single call, add multiple JSON-path expressions separated
by a comma in attributeNameExpression:
"attributeRequests": [ { "endpoint": "/sharedAttributes", "type": "shared", "HTTPMethods": ["POST"], "security": { "type": "anonymous" }, "timeout": 10.0, "deviceNameExpression": "${deviceName}", "attributeNameExpression": "${pduAttribute}, ${versionAttribute}" }]Subsection “attributeUpdates”
Section titled “Subsection “attributeUpdates””When a shared attribute is updated in ThingsBoard, the gateway sends an HTTP request to an external URL on the device.
| Parameter | Default | Description |
|---|---|---|
HTTPMethod |
POST |
HTTP method for the outgoing request |
SSLVerify |
false |
Verify the server’s SSL certificate |
httpHeaders |
{"CONTENT-TYPE": "application/json"} |
Additional HTTP headers |
security.type |
anonymous |
Security type: basic, oauth2, cert, or anonymous. See Subsection “security” for outgoing requests |
timeout |
0.5 |
Request timeout in seconds |
allowRedirects |
true |
Follow HTTP redirects |
deviceNameFilter |
.*REST$ |
Regex filter on device name |
attributeFilter |
data |
Regex filter on attribute name |
requestUrlExpression |
http://127.0.0.1:5001/sensor/${deviceName}/${attributeKey} |
Full URL of the target request. ${deviceName}, ${attributeKey}, and ${attributeValue} are replaced with the device name, attribute key, and attribute value |
valueExpression |
{"${attributeKey}":"${attributeValue}"} |
Expression to build the request body |
"attributeUpdates": [ { "HTTPMethod": "POST", "SSLVerify": false, "httpHeaders": { "CONTENT-TYPE": "application/json" }, "security": { "type": "basic", "username": "user", "password": "passwd" }, "timeout": 0.5, "allowRedirects": true, "deviceNameFilter": ".*REST$", "attributeFilter": "data", "requestUrlExpression": "http://127.0.0.1:5001/sensor/${deviceName}/${attributeKey}", "valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}" }]Subsection “serverSideRpc”
Section titled “Subsection “serverSideRpc””Maps ThingsBoard RPC commands to outgoing HTTP requests sent to the device.
| Parameter | Default | Description |
|---|---|---|
deviceNameFilter |
.* |
Regex filter on device name |
methodFilter |
echo |
Regex filter on RPC method name |
requestUrlExpression |
http://127.0.0.1:5001/${deviceName} |
Full URL of the target request. ${deviceName}, ${methodName}, ${restId} (RPC request ID), and ${params.<key>} are replaced with values from the RPC request |
responseTimeout |
1 |
Seconds to wait for a response |
HTTPMethod |
GET |
HTTP method for the outgoing request. httpMethod is accepted too |
valueExpression |
${params} |
Expression to build the request body |
timeout |
10 |
Request timeout in seconds |
httpHeaders |
{"CONTENT-TYPE": "application/json"} |
Additional HTTP headers |
security.type |
anonymous |
Security type: basic, oauth2, cert, or anonymous. See Subsection “security” for outgoing requests |
The gateway always waits for the HTTP response and replies to ThingsBoard: {"result": <response body>} on success
or {"error": "<message>"} on failure.
"serverSideRpc": [ { "deviceNameFilter": ".*", "methodFilter": "echo", "requestUrlExpression": "http://127.0.0.1:5001/${deviceName}", "responseTimeout": 1, "HTTPMethod": "GET", "valueExpression": "${params}", "timeout": 0.5, "httpHeaders": { "Content-Type": "application/json" }, "security": { "type": "anonymous" } }, { "deviceNameFilter": ".*", "methodFilter": "no-reply", "requestUrlExpression": "http://127.0.0.1:5001/${deviceName}/no-reply", "HTTPMethod": "POST", "valueExpression": "${params.hum}", "httpHeaders": { "Content-Type": "application/json" } }]The connector also has built-in get and set RPC methods that send an HTTP request to any URL without
configuration. They take requestUrlExpression, an optional HTTPMethod, and, for set, a value in the parameter
string. See the Reserved RPC methods guide for details.
Subsection “security” for outgoing requests
Section titled “Subsection “security” for outgoing requests”Authorization for requests that the connector sends to external servers from attributeUpdates and serverSideRpc
entries. Each entry has its own security object, so different entries can use different servers and credentials.
The connector sends an Authorization: Basic <base64(username:password)> header with the requests of the entry.
| Parameter | Default | Description |
|---|---|---|
type |
basic |
Authorization type |
username |
Username | |
password |
Password |
{ "security": { "type": "basic", "username": "user", "password": "passwd" }}The connector obtains an OAuth 2.0 access token from the authorization server using the client credentials grant and
sends it in the Authorization: Bearer <token> header with the requests of the entry. The token is renewed
automatically before it expires, so the connector keeps working with APIs that issue short-lived tokens.
| Parameter | Default | Description |
|---|---|---|
type |
oauth2 |
Authorization type |
tokenUrl |
URL for obtaining the access token. The token request uses the SSLVerify setting of the entry |
|
clientId |
Client ID from the API provider | |
clientSecret |
Client secret from the API provider | |
scope |
(Optional) Scopes to request, separated by spaces | |
clientAuthentication |
header |
(Optional) How the client ID and secret are sent to tokenUrl: header — in an Authorization: Basic header; body — as client_id and client_secret form fields |
tokenPrefix |
Bearer |
(Optional) Word placed before the token in the Authorization header. To send the token without a prefix, set it to "" |
tokenRefreshRatio |
0.75 |
(Optional) Part of the token lifetime after which the token is renewed, a number greater than 0 and up to 1 |
tokenRequestTimeout |
10 |
(Optional) Token request timeout in seconds |
tokenRetryDelay |
10 |
(Optional) Seconds to wait after a failed token request before the next attempt |
defaultExpiresIn |
(Optional) Token lifetime in seconds, used when the token response has no expires_in field |
{ "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5003/oauth2/token", "clientId": "client_id", "clientSecret": "client_secret", "scope": "read write" }}The clientId and clientSecret parameters support placeholders in the ${VARIABLE_NAME} format. This allows you to
keep the credentials out of the configuration file and provide them as environment variables according to your
system’s setup:
{ "security": { "type": "oauth2", "tokenUrl": "http://127.0.0.1:5003/oauth2/token", "clientId": "${API_CLIENT_ID}", "clientSecret": "${API_CLIENT_SECRET}" }}Can be configured in advanced configuration mode only.
The connector presents a client certificate to the external server during the TLS handshake (mutual TLS).
| Parameter | Default | Description |
|---|---|---|
type |
cert |
Authorization type |
cert |
Path to the client certificate file in PEM format | |
key |
(Optional) Path to the private key file. Not needed if the key is in the cert file |
{ "security": { "type": "cert", "cert": "/path/to/client_cert.pem", "key": "/path/to/client_key.pem" }}No authorization header is sent. Useful for public APIs or testing.
| Parameter | Default | Description |
|---|---|---|
type |
anonymous |
Authorization type |
{ "security": { "type": "anonymous" }}Was this helpful?