thingsboard_client
ThingsBoard REST API client for Dart and Flutter.
Generated from the ThingsBoard OpenAPI spec, with a handwritten runtime facade for JWT authentication, automatic token refresh, WebSocket telemetry subscriptions and typed error handling.
Upgrading to 5.0? See MIGRATION.md: from
thingsboard_client4.x (services → generated API classes, with a table of every 4.x service method) and fromthingsboard_pe_client4.4.x, which this package replaces now that ThingsBoard is a single product.
Features
- Full REST API coverage: 94 API classes and 1071 model types.
- JWT authentication: login, automatic refresh, concurrent request handling.
- WebSocket telemetry: real-time subscriptions with automatic reconnect.
- Typed errors:
ThingsboardErrorwith theThingsBoardErrorCodeenum and retry logic.ThingsboardError.errorCodeis this facade enum, not the generatedThingsboardErrorCode(number2, ...), which only describes the REST model. - Tree-shakable: each operation registers only the serializers it needs, so models of operations an application does not call are dropped from its build. Referencing
standardSerializers(for example passing it to an API class constructor) keeps every model in the build. To serialize a single model, useapiSerializers(register<Model>)(for exampleapiSerializers(registerAuditLog).toJson(AuditLog.serializer, log)), which only includes that model and what it references. - Flutter Web and mobile: no bare
dart:ioimports. - Requires ThingsBoard 4.4.0 or later, and Dart 3.10 / Flutter 3.38 or later.
Installation
dependencies:
thingsboard_client: ^5.0.0
Quick start
import 'package:thingsboard_client/thingsboard_client.dart';
void main() async {
final tbClient = ThingsboardClient('https://your-tb-instance.com');
await tbClient.login(LoginRequest((b) => b
..username = 'tenant@thingsboard.org'
..password = 'tenant'));
print('Logged in: ${tbClient.isAuthenticated()}');
final response = await tbClient
.getDeviceControllerApi()
.getTenantDevices(pageSize: 10, page: 0);
print('Devices: ${response.data?.totalElements}');
await tbClient.logout();
}
Telemetry WebSocket
The WebSocket API (subscriptions, their commands and updates) is part of the main library. Entity ids are the generated ones:
import 'package:thingsboard_client/thingsboard_client.dart';
final subscriber = TelemetrySubscriber.createEntityAttributesSubscription(
telemetryService: tbClient.getTelemetryService(),
entityId: DeviceId((b) => b..id = deviceUuid),
attributeScope: LatestTelemetry.LATEST_TELEMETRY.toShortString(),
);
subscriber.attributeDataStream.listen((data) => print(data));
subscriber.subscribe();
A client created with an API key (ThingsboardClient(url, apiKey: key)) authenticates both the REST requests and the WebSocket with it.
See example/main.dart for a complete example.
Documentation
- ThingsBoard documentation
- REST API reference
- Generated API docs: doc/
Development
The REST API classes and models are generated from spec/openapi.json by tool/generate-client.sh; the handwritten facade lives in common/. See CONTRIBUTING.md for how to regenerate the client and run the checks.
Features and bugs
Please file feature requests and bugs at the issue tracker. Report security vulnerabilities privately, see SECURITY.md.
Contributors
The client is developed by ThingsBoard, Inc. common/lib/src/model/telemetry/telemetry_models.dart (copied to lib/src/model/telemetry/) also keeps contributions to the 4.x client by Meric Koc and Vincent Långström; its copyright line reads "ThingsBoard, Inc. and contributors".
License
BSD-3-Clause, see LICENSE.