Skip to content
Stand with Ukraine flag

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.

The connector reads its settings from a JSON file. Below is a full example with two mapping entries — the first uses a JSON converter with basic auth, the second uses a custom converter with anonymous auth:

{
"server": {
"host": "127.0.0.1",
"port": 5000,
"SSL": false
},
"mapping": [
{
"endpoint": "/test_device",
"HTTPMethods": ["POST"],
"security": {
"type": "basic",
"username": "username",
"password": "password"
},
"converter": {
"type": "json",
"deviceInfo": {
"deviceNameExpression": "Device ${name}",
"deviceNameExpressionSource": "request",
"deviceProfileExpressionSource": "constant",
"deviceProfileExpression": "default"
},
"attributes": [
{ "key": "model", "type": "string", "value": "${sensorModel}" }
],
"timeseries": [
{ "key": "temperature", "type": "double", "value": "${temp}" },
{ "key": "humidity", "type": "double", "value": "${hum}" },
{ "key": "combine", "type": "string", "value": "${hum}:${temp}" }
]
}
},
{
"endpoint": "/anon2",
"HTTPMethods": ["POST"],
"security": { "type": "anonymous" },
"converter": {
"type": "custom",
"deviceInfo": {
"deviceNameExpression": "SuperAnonDevice",
"deviceNameExpressionSource": "constant",
"deviceProfileExpressionSource": "constant",
"deviceProfileExpression": "default"
},
"extension": "CustomRestUplinkConverter",
"extensionConfig": {
"key": "Totaliser",
"datatype": "float",
"fromByte": 0,
"toByte": 4,
"byteorder": "big",
"signed": true,
"multiplier": 1
}
}
}
],
"requestsMapping": {
"attributeRequests": [],
"attributeUpdates": [],
"serverSideRpc": []
}
}

Configures the HTTP(S) server the connector starts on the gateway.

ParameterDefaultDescription
host127.0.0.1IP address or hostname of the server
port5000Port to listen on
SSLtrueEnable TLS
security.certPath to the PEM certificate file
security.keyPath to the private key file
{
"server": {
"host": "127.0.0.1",
"port": 5000,
"SSL": true,
"security": {
"cert": "~/ssl/cert.pem",
"key": "~/ssl/key.pem"
}
}
}

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.

ParameterDefaultDescription
endpoint/test_deviceURL path of the endpoint
HTTPMethods["GET"]Allowed HTTP methods (GET, POST, etc.)

Per-endpoint authorization for incoming client requests.

The connector expects an Authorization: Basic <base64(username:password)> header on every request.

ParameterDefaultDescription
typebasicAuthorization type
usernameusernameUsername
passwordpasswordPassword
{
"security": {
"type": "basic",
"username": "username",
"password": "password"
}
}

Example curl request with Basic auth:

Terminal window
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"}'

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:

ParameterDefaultDescription
deviceNameExpressionDevice ${name}Expression to build the device name; ${field} reads from the request payload
deviceNameExpressionSourcerequestrequest — read from payload; constant — treat expression as a fixed value
deviceProfileExpressionSourcerequestSame as above, but for the device profile
deviceProfileExpressiondefaultExpression to resolve the device profile name

Attributes and timeseries parameters:

Both sections share the same field structure:

ParameterDefaultDescription
keymodel / temperatureKey name in ThingsBoard
typestring / doubleData 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
dayfirstfalse(Optional) When parsing tsField, treat the first number as the day (true) or month (false)
yearfirstfalse(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:

application/json
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"
}'
multipart/form-data
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}" }

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:

ParameterDefaultDescription
response.successResponseOKBody returned when the status is 200
response.unsuccessfulResponseErrorBody 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.

ParameterDefaultDescription
response.responseExpectedtrueEnable advanced response mode
response.timeout120Seconds to wait for the ThingsBoard response
response.responseAttributeresultShared 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:

Optional. Configures attribute requests, attribute updates, and server-side RPC handling.

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.

ParameterDefaultDescription
endpoint/sharedAttributesURL path of the attribute request endpoint
typesharedAttribute type: shared or client
HTTPMethods["POST"]Allowed HTTP methods
security.typebasicAuthorization type for this endpoint
security.usernameuserUsername (for basic type)
security.passwordpasswdPassword (for basic type)
timeout10.0Request 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}"
}
]

When a shared attribute is updated in ThingsBoard, the gateway sends an HTTP request to an external URL on the device.

ParameterDefaultDescription
HTTPMethodPOSTHTTP method for the outgoing request
SSLVerifyfalseVerify the server’s SSL certificate
httpHeaders{"CONTENT-TYPE": "application/json"}Additional HTTP headers
security.typebasicSecurity type: basic or anonymous
security.usernameuserUsername (for basic type)
security.passwordpasswdPassword (for basic type)
timeout0.5Request timeout in seconds
tries3Number of send retries
allowRedirectstrueFollow HTTP redirects
deviceNameFilter.*REST$Regex filter on device name
attributeFilterdataRegex filter on attribute name
requestUrlExpressionsensor/${deviceName}/${attributeKey}Expression to build the target URL
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,
"tries": 3,
"allowRedirects": true,
"deviceNameFilter": ".*REST$",
"attributeFilter": "data",
"requestUrlExpression": "sensor/${deviceName}/${attributeKey}",
"valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}"
}
]

Maps ThingsBoard RPC commands to outgoing HTTP requests sent to the device.

ParameterDefaultDescription
deviceNameFilter.*Regex filter on device name
methodFilterechoRegex filter on RPC method name
requestUrlExpressionhttp://127.0.0.1:5001/${deviceName}Expression to build the target URL
responseTimeout1Seconds to wait for a response
HTTPMethodGETHTTP method for the outgoing request
valueExpression${params}Expression to build the request body
timeout0.5Request timeout in seconds
tries3Number of send retries
httpHeaders{"CONTENT-TYPE": "application/json"}Additional HTTP headers
security.typeanonymousSecurity type: basic or anonymous
"serverSideRpc": [
{
"deviceNameFilter": ".*",
"methodFilter": "echo",
"requestUrlExpression": "http://127.0.0.1:5001/${deviceName}",
"responseTimeout": 1,
"HTTPMethod": "GET",
"valueExpression": "${params}",
"timeout": 0.5,
"tries": 3,
"httpHeaders": {
"Content-Type": "application/json"
},
"security": {
"type": "anonymous"
}
},
{
"deviceNameFilter": ".*",
"methodFilter": "no-reply",
"requestUrlExpression": "sensor/${deviceName}/request/${methodName}/${requestId}",
"HTTPMethod": "POST",
"valueExpression": "${params.hum}",
"httpHeaders": {
"Content-Type": "application/json"
}
}
]

Every telemetry and attribute key also has a built-in GET and SET RPC method available automatically. The required parameters are requestUrlExpression, responseTimeout, HTTPMethod, and valueExpression. See the Reserved RPC methods guide for details.