Skip to content
Stand with Ukraine flag

Java REST Client

The ThingsBoard Java REST Client wraps the REST API and lets Java applications manage devices, assets, dashboards, customers, and other platform entities programmatically. It is built on Spring RestTemplate.

Add the Maven dependency:

<dependencies>
<dependency>
<groupId>org.thingsboard</groupId>
<artifactId>rest-client</artifactId>
<version>4.3.0.1</version>
</dependency>
</dependencies>

Add the ThingsBoard repository:

<repositories>
<repository>
<id>thingsboard</id>
<url>https://repo.thingsboard.io/artifactory/libs-release-public</url>
</repository>
</repositories>
String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
client.getUser().ifPresent(System.out::println);
client.close();
String url = "https://eu.thingsboard.cloud";
String username = "[email protected]";
String password = "tenant";
RestClient client = new RestClient(url);
client.login(username, password);
client.getUser().ifPresent(System.out::println);
client.logout();
client.close();

Create a device, save and read shared attributes, then delete:

String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
String newDeviceName = "Test Device";
Device newDevice = new Device();
newDevice.setName(newDeviceName);
ObjectMapper mapper = new ObjectMapper();
ObjectNode additionalInfoNode = mapper.createObjectNode()
.put("description", "My brand new device");
newDevice.setAdditionalInfo(additionalInfoNode);
Device savedDevice = client.saveDevice(newDevice);
System.out.println("Saved device: " + savedDevice);
DeviceId deviceId = savedDevice.getId();
Optional<DeviceInfo> optionalDevice = client.getDeviceInfoById(deviceId);
DeviceInfo foundDevice = optionalDevice
.orElseThrow(() -> new IllegalArgumentException(
"Device with id " + deviceId.getId() + " not found"));
ObjectNode requestNode = mapper.createObjectNode()
.put("temperature", 22.4)
.put("humidity", 57.4);
boolean isSuccessful = client.saveEntityAttributesV2(
foundDevice.getId(), "SHARED_SCOPE", requestNode);
System.out.println("Attributes saved: " + isSuccessful);
var attributes = client.getAttributesByScope(foundDevice.getId(),
"SHARED_SCOPE", List.of("temperature", "humidity"));
System.out.println("Found attributes: ");
attributes.forEach(System.out::println);
client.deleteDevice(savedDevice.getId());
client.close();
String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
PageData<Device> tenantDevices;
PageLink pageLink = new PageLink(10);
do {
tenantDevices = client.getTenantDevices("", pageLink);
tenantDevices.getData().forEach(System.out::println);
pageLink = pageLink.nextPageLink();
} while (tenantDevices.hasNext());
client.close();
String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
PageData<DashboardInfo> pageData;
PageLink pageLink = new PageLink(10);
do {
pageData = client.getTenantDashboards(pageLink);
pageData.getData().forEach(System.out::println);
pageLink = pageLink.nextPageLink();
} while (pageData.hasNext());
client.close();
String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
EntityTypeFilter typeFilter = new EntityTypeFilter();
typeFilter.setEntityType(EntityType.DEVICE);
EntityCountQuery totalDevicesQuery = new EntityCountQuery(typeFilter);
Long totalDevicesCount = client.countEntitiesByQuery(totalDevicesQuery);
System.out.println("Total devices: " + totalDevicesCount);
KeyFilter keyFilter = new KeyFilter();
keyFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "active"));
keyFilter.setValueType(EntityKeyValueType.BOOLEAN);
BooleanFilterPredicate filterPredicate = new BooleanFilterPredicate();
filterPredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
filterPredicate.setValue(new FilterPredicateValue<>(true));
keyFilter.setPredicate(filterPredicate);
EntityCountQuery totalActiveDevicesQuery =
new EntityCountQuery(typeFilter, List.of(keyFilter));
Long totalActiveDevicesCount = client.countEntitiesByQuery(totalActiveDevicesQuery);
System.out.println("Total active devices: " + totalActiveDevicesCount);
client.close();
String url = "https://eu.thingsboard.cloud";
String apiKey = "YOUR_API_KEY_VALUE";
RestClient client = RestClient.withApiKey(url, apiKey);
EntityTypeFilter typeFilter = new EntityTypeFilter();
typeFilter.setEntityType(EntityType.DEVICE);
KeyFilter keyFilter = new KeyFilter();
keyFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "active"));
keyFilter.setValueType(EntityKeyValueType.BOOLEAN);
BooleanFilterPredicate filterPredicate = new BooleanFilterPredicate();
filterPredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
filterPredicate.setValue(new FilterPredicateValue<>(true));
keyFilter.setPredicate(filterPredicate);
List<EntityKey> fields = List.of(
new EntityKey(EntityKeyType.ENTITY_FIELD, "name"),
new EntityKey(EntityKeyType.ENTITY_FIELD, "type"),
new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime")
);
List<EntityKey> attributes = List.of(
new EntityKey(EntityKeyType.ATTRIBUTE, "active")
);
EntityDataSortOrder sortOrder = new EntityDataSortOrder();
sortOrder.setKey(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"));
sortOrder.setDirection(EntityDataSortOrder.Direction.DESC);
EntityDataPageLink entityDataPageLink =
new EntityDataPageLink(10, 0, "", sortOrder);
EntityDataQuery dataQuery =
new EntityDataQuery(typeFilter, entityDataPageLink, fields, attributes,
List.of(keyFilter));
PageData<EntityData> entityPageData;
do {
entityPageData = client.findEntityDataByQuery(dataQuery);
entityPageData.getData().forEach(System.out::println);
dataQuery = dataQuery.next();
} while (entityPageData.hasNext());
client.close();

See the tb-ce-rest-client-example repository for complete runnable examples.