Skip to content

Cookie preferences

We use cookies for our own analytics and to see how our campaigns perform. We never sell your data. Necessary cookies keep the site working and cannot be switched off. See our Cookie Policy for details.

Security, load balancing and remembering your cookie choice.

Show us which pages people read and how they find the site, so we can improve it (Google Analytics).

Show us which of our ad campaigns bring visitors to the site (Google Ads).

© 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

Java REST Client (Legacy)

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.1.6</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(),
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);
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));

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