Skip to content

Getting Started

Installation

bash
npm install @appzgatenz/label-print-topbridge-js

Prerequisites

#RequirementDetails
1Modern browser with WebSocket supportChrome 16+, Firefox 11+, Safari 7+, Edge 12+
2TopBridge App >= 1.0.45 installedDownload
3TopBridge App is runningHealth check returns pong
4User is logged in to TopBridge Appdata.isLoggedIn === true
5Print entitlement is validBenefits check passes
6At least one printer configured with protocol (TSPL/ZPL)Printer list is non-empty
7CSP allows topsale: protocol (if using launch)See CSP Configuration

Don't want to write code?

Try the TOPSALE label printing solution — no integration needed.

Initialization

typescript
import { TopBridgeClient } from '@appzgatenz/label-print-topbridge-js'

const client = new TopBridgeClient()

The SDK communicates with TopBridge App via local WebSocket. No configuration required.

Complete Print Workflow

typescript
// 0. Optional: ensure TopBridge App is running
const { printers } = await client.launch.ensureRunning(
  () => client.preflight.run({
    onStepChange: (step) => console.log(`Checking ${step}...`)
  })
)

// Or run preflight directly (without auto-launching)
// const { printers } = await client.preflight.run()

// 1. Get available templates
const templates = await client.templates.list()

// 2. Optional: get template field definitions
const schema = await client.templates.schema('PRICE_LABEL')

// 3. Execute print
const result = await client.print.execute({
  template: 'PRICE_LABEL',       // Template ID or Code
  printer: 'TSC DA220',          // Printer name
  products: [
    { name: 'Apple', price: { value: 3.99, currency: '$', unit: '/kg' }, copies: 2 },
    { name: 'Banana', price: { value: 1.99, currency: '$' }, copies: 1 },
  ],
})

console.log(`Printed ${result.data.printedCopies} copies`)

The SDK automatically fetches the template schema and transforms product data. No need to manually specify field types.

Configuration Options

typescript
const client = new TopBridgeClient({
  debug: true,                     // Enable console logging
  logger: customLogger,            // Custom logger implementation
  timeouts: {
    health: 3000,                  // Health check timeout (ms)
    preflight: 10000,              // Preflight timeout (ms)
    print: 60000,                  // Print timeout (ms)
  },
})
FieldTypeDefaultDescription
sourcestring'(internal)'SDK source identifier (set internally, do not configure manually)
debugbooleanfalseEnable console logging (prefix: [TopBridge])
loggerLoggerSilent (no-op)Custom logger implementation
timeouts.healthnumber (ms)3000Health check timeout
timeouts.preflightnumber (ms)10000Preflight / template query timeout
timeouts.printnumber (ms)60000Print execution timeout

Logger

You can provide a custom logger to integrate with monitoring services (e.g., Sentry, Datadog):

typescript
interface Logger {
  debug: (...args: unknown[]) => void
  info: (...args: unknown[]) => void
  warn: (...args: unknown[]) => void
  error: (...args: unknown[]) => void
}

Logger priority: custom logger > debug: true console logs > silent (default).

Error Handling

typescript
import {
  TopBridgeConnectionError,
  TopBridgeAuthError,
  TopBridgeQuotaError,
  TopBridgePrintError,
  TopBridgePrinterError,
  TopBridgeTemplateError,
  TopBridgeNetworkError,
  TopBridgeSourceError,
} from '@appzgatenz/label-print-topbridge-js'

try {
  await client.print.execute({ /* ... */ })
} catch (err) {
  if (err instanceof TopBridgeConnectionError) {
    // TopBridge App is not running or connection timed out
  } else if (err instanceof TopBridgeAuthError) {
    // Not logged in or update required
    if (err.code === 'UPDATE_REQUIRED') {
      window.open(err.storeUrl) // Guide user to update
    }
  } else if (err instanceof TopBridgeQuotaError) {
    // Benefit invalid or quota exhausted
  } else if (err instanceof TopBridgePrinterError) {
    // Printer offline or protocol not configured
  } else if (err instanceof TopBridgeTemplateError) {
    // Template does not exist or no permission
  } else if (err instanceof TopBridgeNetworkError) {
    // TopBridge App is online, but cloud network is disconnected
  } else if (err instanceof TopBridgeSourceError) {
    // Origin verification failed
  } else if (err instanceof TopBridgePrintError) {
    // Print failed
  }
}

See Error Handling for the complete reference.