Electron Quickstart

Protect your desktop application with license key validation and offline support.

5 minute setup
$ npm install @traffic-orchestrator/client
1

Install the SDK

Add the Traffic Orchestrator client to your Electron project.

bash
npm install @traffic-orchestrator/client
2

Validate in the Main Process

License validation should happen in the main process (not renderer) to prevent tampering. Use IPC to communicate results.

typescript
// main.ts — Electron main process
import { app, BrowserWindow, ipcMain } from 'electron'
import { TrafficOrchestrator } from '@traffic-orchestrator/client'
import { machineIdSync } from 'node-machine-id'

const client = new TrafficOrchestrator({
  baseUrl: 'https://api.trafficorchestrator.com/api/v1'
})

// Handle license check from renderer
ipcMain.handle('validate-license', async (_, key) => {
  const machineId = machineIdSync()

  const result = await client.validate({
    key,
    machineId
  })

  if (result.valid) {
    // Cache the signature for offline use
    store.set('cachedSignature', result.signature)
  }

  return result
})
3

Call from the Renderer

Use Electron IPC to request validation from the renderer process.

typescript
// renderer.ts — preload bridge
const result = await window.electronAPI.validateLicense(licenseKey)

if (result.valid) {
  document.getElementById('status').textContent = '✅ Licensed'
  // Unlock premium features
  enableProFeatures(result.features)
} else {
  document.getElementById('status').textContent = '❌ Trial Mode'
  showTrialBanner(result.trialDaysLeft)
}
4

Offline Fallback

When the machine is offline, verify the cached signature using Ed25519. No network required.

typescript
// Offline fallback in main process
ipcMain.handle('validate-license', async (_, key) => {
  try {
    // Try online first
    const result = await client.validate({ key, machineId })
    store.set('cachedSignature', result.signature)
    return result
  } catch (err) {
    // Network unavailable — verify offline
    const cached = store.get('cachedSignature')
    if (cached) {
      const valid = await client.verifyOffline(key, cached, PUBLIC_KEY)
      return { valid, offline: true }
    }
    return { valid: false, error: 'No cached license' }
  }
})
5

Handle Activation Limits

Enforce how many machines can use a single license key.

typescript
// On first launch — activate the machine
const activation = await client.activate({
  key: licenseKey,
  machineId: machineIdSync()
})

if (activation.error === 'MAX_ACTIVATIONS_REACHED') {
  dialog.showErrorBox(
    'Activation Limit',
    'This license has been activated on the maximum number of devices. ' +
    'Please deactivate another device or upgrade your plan.'
  )
  app.quit()
}

// On app quit — optionally deactivate
app.on('will-quit', async () => {
  await client.deactivate({ key: licenseKey, machineId: machineIdSync() })
})