BellaClient.fromEnv constructor
BellaClient.fromEnv()
Creates a BellaClient from environment variables injected by bella exec.
Reads (in priority order):
BELLA_BAXTER_URL/BELLA_API_URL(deprecated alias)BELLA_BAXTER_API_KEY/BELLA_API_KEY(deprecated alias) — HMAC modeBELLA_BAXTER_ACCESS_TOKEN— Bearer mode (SSO / OAuth)
Throws StateError if neither auth var is set or the URL is missing.
Implementation
factory BellaClient.fromEnv() {
final baseUrl = Platform.environment['BELLA_BAXTER_URL'] ??
Platform.environment['BELLA_API_URL'];
if (baseUrl == null || baseUrl.isEmpty) {
throw StateError(
'BELLA_BAXTER_URL is not set.\n'
' Run your app via: bella exec -- dart main.dart',
);
}
final apiKey = Platform.environment['BELLA_BAXTER_API_KEY'] ??
Platform.environment['BELLA_API_KEY'];
final accessToken = Platform.environment['BELLA_BAXTER_ACCESS_TOKEN'];
final appClient = Platform.environment['BELLA_BAXTER_APP_CLIENT'];
// ZKE: auto-load device private key from BELLA_BAXTER_PRIVATE_KEY env var.
// This var is injected by `bella sdk run` when the device has been set up via `bella auth setup`.
Uint8List? privateKey;
final privateKeyB64 = Platform.environment['BELLA_BAXTER_PRIVATE_KEY'];
if (privateKeyB64 != null && privateKeyB64.isNotEmpty) {
try {
privateKey = base64Decode(privateKeyB64);
} catch (_) {
// Invalid base64 — ignore and fall back to ephemeral key
}
}
if (apiKey != null && apiKey.isNotEmpty) {
return BellaClient(BellaClientOptions(baseUrl: baseUrl, apiKey: apiKey, appClient: appClient, privateKey: privateKey));
}
if (accessToken != null && accessToken.isNotEmpty) {
return BellaClient(
BellaClientOptions(baseUrl: baseUrl, accessToken: accessToken, appClient: appClient, privateKey: privateKey));
}
throw StateError(
'No Bella auth credentials found.\n'
' Set BELLA_BAXTER_API_KEY or BELLA_BAXTER_ACCESS_TOKEN,\n'
' or run your app via: bella exec -- dart main.dart',
);
}