TypeScript SDK
TypeScript SDK
Section titled “TypeScript SDK”The official TypeScript SDK for the UnboundBytes API provides a type-safe, ergonomic way to interact with all platform features.
Installation
Section titled “Installation”npm install @unboundbytes/sdkyarn add @unboundbytes/sdkpnpm add @unboundbytes/sdkQuick Start
Section titled “Quick Start”import { UnboundBytesClient } from '@unboundbytes/sdk';
const client = new UnboundBytesClient({ auth: { type: 'bearer', token: 'your-auth-token' }});
// List devicesconst devices = await client.listDevices('ten_xxx');Client Configuration
Section titled “Client Configuration”Constructor Options
Section titled “Constructor Options”interface ClientConfig { baseUrl?: string; // API base URL (default: https://api.unboundbytes.com) auth: AuthConfig; // Authentication configuration timeout?: number; // Request timeout in ms (default: 30000) maxRetries?: number; // Max retry attempts (default: 3) retryDelay?: number; // Initial retry delay in ms (default: 1000)}Authentication Types
Section titled “Authentication Types”// Bearer token (web applications)type BearerAuth = { type: 'bearer'; token: string | (() => Promise<string>); expiresAt?: number; refreshHandler?: () => Promise<{ token: string; expiresAt?: number }>;};
// HMAC (agents)type HMACAuth = { type: 'hmac'; agentId: string; tenantId: string; sharedSecret: string;};
// API Key (scripts)type APIKeyAuth = { type: 'apikey'; apiKey: string; tenantId: string;};API Methods
Section titled “API Methods”Health & Status
Section titled “Health & Status”// Health checkconst health = await client.healthCheck();// Returns: { status: 'healthy' | 'degraded' | 'unhealthy', timestamp: string }Device Pairing
Section titled “Device Pairing”// Initiate pairingconst pairing = await client.initiatePairing({ device_fingerprint: 'unique-device-id', device_name: 'My Server', platform: 'linux'});// Returns: { pairing_code: string, expires_at: string, portal_url: string }
// Prepare activation (portal only)const activation = await client.preparePairingActivation( 'ABC123', // pairing code 'ten_xxx' // tenant ID);// Returns: { activation_token: string, expires_at: string }
// Activate pairingconst result = await client.activatePairing({ pairing_code: 'ABC123', activation_token: 'token_xxx', user_email: 'user@example.com'});// Returns: { device_id, agent_id, shared_secret, tenant_id, fingerprint }
// Check pairing statusconst status = await client.checkPairingStatus('device-fingerprint');// Returns: { status: 'pending' | 'activated' | 'paired' | 'expired' }Devices
Section titled “Devices”// List devicesconst devices = await client.listDevices('ten_xxx', { page: 1, pageSize: 20});// Returns: Device[]
// Get device detailsconst device = await client.getDevice('dev_xxx');// Returns: Device
// Delete deviceawait client.deleteDevice('dev_xxx');
// Send heartbeatawait client.sendHeartbeat('dev_xxx', { cpu: 25.5, memory: 4096, disk: 50000, timestamp: new Date().toISOString()});Commands
Section titled “Commands”// Poll command queueconst commands = await client.pollCommands('dev_xxx');// Returns: Command[]
// Queue a commandconst result = await client.queueCommand('dev_xxx', { commandId: 'cmd_xxx', type: 'restart-app', issuedAt: new Date().toISOString(), payload: { appId: 'app_xxx' }, requiresAck: true}, 'idempotency-key');// Returns: { commandId: string, status: string }
// Acknowledge commandawait client.acknowledgeCommand('dev_xxx', 'cmd_xxx', { status: 'completed', output: 'App restarted successfully'});Deployments
Section titled “Deployments”// List deploymentsconst deployments = await client.listDeployments('ten_xxx', { page: 1, pageSize: 20});// Returns: Deployment[]
// Create deploymentconst deployment = await client.createDeployment('ten_xxx', { appId: 'app_xxx', deviceId: 'dev_xxx', configuration: { port: 8080 }});// Returns: Deployment
// Get deploymentconst deployment = await client.getDeployment('ten_xxx', 'dep_xxx');// Returns: DeploymentBackups
Section titled “Backups”// List all backups for tenantconst backups = await client.listBackups('ten_xxx');// Returns: Backup[]
// List backups for deviceconst deviceBackups = await client.listDeviceBackups('dev_xxx');// Returns: Backup[]
// Create backup metadataawait client.createBackup('dev_xxx', { backupId: 'bak_xxx', appId: 'app_xxx', status: 'completed', size: 1024000, checksum: 'sha256:xxx'});
// Restore backupawait client.restoreBackup('dev_xxx', 'bak_xxx', { restoreType: 'full', overwriteExisting: true});Tunnels
Section titled “Tunnels”// List tunnelsconst tunnels = await client.listTunnels('dev_xxx');// Returns: Tunnel[]
// Create tunnelconst tunnel = await client.createTunnel('dev_xxx', { appId: 'app_xxx', hostname: 'myapp.example.com'});// Returns: Tunnel
// Get tunnel credentialsconst credentials = await client.getTunnelCredentials('dev_xxx', 'tun_xxx');// Returns: TunnelCredentials
// Delete tunnelawait client.deleteTunnel('dev_xxx', 'tun_xxx');// List appsconst apps = await client.listApps('ten_xxx');// Returns: App[]
// Create appconst app = await client.createApp({ appId: 'app_xxx', appName: 'My Application', tenantId: 'ten_xxx', configuration: { image: 'nginx:latest' }});// Returns: App
// Get app detailsconst app = await client.getApp('app_xxx');// Returns: AppTenants
Section titled “Tenants”// Get tenant dashboard summaryconst summary = await client.getTenantDashboard('ten_xxx');// Returns: { devices: number, apps: number, deployments: number, backups: number }WebSocket Client
Section titled “WebSocket Client”For real-time command communication:
import { UnboundBytesClient } from '@unboundbytes/sdk';
const client = new UnboundBytesClient({ auth: { type: 'hmac', agentId: 'agt_xxx', tenantId: 'ten_xxx', sharedSecret: 'secret' }});
const ws = client.createWebSocketClient( client.config.auth as HMACAuth, 'dev_xxx', { onCommand: async (command) => { console.log('Received:', command); // Process command... await client.acknowledgeCommand('dev_xxx', command.commandId, { status: 'completed' }); }, onConnect: () => console.log('Connected'), onDisconnect: () => console.log('Disconnected'), onError: (error) => console.error('Error:', error) });
ws.connect();Error Handling
Section titled “Error Handling”import { APIError, NetworkError, RateLimitError, AuthenticationError, ValidationError} from '@unboundbytes/sdk';
try { await client.getDevice('dev_xxx');} catch (error) { if (error instanceof APIError) { console.error('API Error:', error.code, error.message); console.error('Status:', error.statusCode); console.error('Details:', error.details); console.error('Request ID:', error.requestId); } else if (error instanceof RateLimitError) { console.error('Rate limited. Retry after:', error.retryAfter); } else if (error instanceof NetworkError) { console.error('Network error:', error.message); console.error('Cause:', error.cause); } else if (error instanceof AuthenticationError) { console.error('Auth error:', error.message); } else if (error instanceof ValidationError) { console.error('Validation error:', error.message); console.error('Fields:', error.fields); }}TypeScript Types
Section titled “TypeScript Types”Importing Types
Section titled “Importing Types”import type { Device, Deployment, Backup, Tunnel, Command, App, ClientConfig, BearerAuth, HMACAuth, APIKeyAuth} from '@unboundbytes/sdk';Auto-Generated OpenAPI Types
Section titled “Auto-Generated OpenAPI Types”For advanced use cases, import the auto-generated types:
import type { paths, components, operations } from '@unboundbytes/sdk';
// Path-level typestype DevicesPath = paths['/v1/devices'];type GetDevicesOp = DevicesPath['get'];
// Component schemastype DeviceSchema = components['schemas']['Device'];
// Operation typestype InitiatePairingOp = operations['initiatePairing'];type InitiatePairingRequest = InitiatePairingOp['requestBody'];type InitiatePairingResponse = InitiatePairingOp['responses']['201'];Retry Behavior
Section titled “Retry Behavior”The SDK automatically retries failed requests with exponential backoff:
- Retryable errors: 5xx errors, 429 (rate limit), network errors
- Non-retryable errors: 4xx errors (except 429)
- Backoff: Exponential with jitter (1s, 2s, 4s, … up to 10s max)
- Rate limits: Respects
Retry-Afterheader
Configure retry behavior:
const client = new UnboundBytesClient({ auth: { type: 'bearer', token: 'xxx' }, maxRetries: 5, // Default: 3 retryDelay: 2000 // Default: 1000ms});Next Steps
Section titled “Next Steps”- Quick Start Guide - Get started quickly
- API Authentication - Deep dive into auth
- API Explorer - Interactive documentation