Add license keys to an Electron app

To license an Electron app, validate the key in the main process with @licers/sdk and pass only the result to your windows over IPC. The renderer is ordinary web code a user can open in DevTools, so it must never decide whether the app is licensed.

Updated by the Licers team

Set it up

  1. Create a free account and add a product.
  2. Copy your public key and product id from the Integration page. The public key is safe to ship inside your app.
  3. Issue a key: set how many devices it may use, when it expires, and any entitlements.
  4. Add the code below. On the first check the device is bound to the key; past the device limit a new machine is refused.
Terminal
npm install @licers/sdk

Validate in the main process

main.js
const { app, BrowserWindow, ipcMain } = require('electron');const { LicenseClient, SignatureError, NetworkError } = require('@licers/sdk'); const client = new LicenseClient('YOUR_PUBLIC_KEY', {  productId: 'YOUR_PRODUCT_ID',      // rejects keys for your other products  appVersion: app.getVersion(),      // shows in Analytics}); // The window asks; only this process decides.ipcMain.handle('license:activate', async (_event, key) => {  try {    const result = await client.validate(String(key).trim());    return { ok: result.valid, message: result.error || null, features: result.valid ? result.features : {} };  } catch (e) {    if (e instanceof SignatureError) return { ok: false, message: 'The license server could not be verified.' };    if (e instanceof NetworkError) return { ok: false, offline: true, message: 'You appear to be offline.' };    throw e;  }});
preload.js
const { contextBridge, ipcRenderer } = require('electron'); contextBridge.exposeInMainWorld('license', {  activate: key => ipcRenderer.invoke('license:activate', key),});

Keep paid work out of the renderer

  • Open the main window only after the check passes, or load a limited trial window instead.
  • Run paid features in the main process or a utility process and check the license there again.
  • Keep contextIsolation on and nodeIntegration off, as Electron recommends.
  • Store the key with Electron’s safeStorage so the user types it once.

Offline users

Treat “cannot reach the server” as its own state. Save the time of the last good check and allow a grace period you choose. For machines that never go online, issue a signed .lic file and verify it with verifyOfflineLicense.

What happenedWhat your app should do
The key is expired, revoked, blocked or over its device limitShow the message the server sent. You can reword every one of them on the Custom Messages page.
The answer has a bad signature or an old nonceRefuse. Somebody is faking the server.
The server cannot be reached, or returns a 5xx errorRetry, or allow a grace period since the last good check. Being offline is not the same as being unlicensed.

Machines that never go online

Show the device id to the customer, create a .lic file for it under License keys → ⋯ → Offline license file, and verify it in the main process:

offline.mjs
import { SignatureError, getHwid, verifyOfflineLicense } from '@licers/sdk'; // 1. Show this to the customer. They send it to you, and you create the file//    for that device: License keys -> ... -> Offline license file.console.log('Device id:', getHwid()); // 2. The customer saves the file next to your app. No network is used here.let lic;try {  lic = verifyOfflineLicense('YOUR_PUBLIC_KEY', 'license.lic', {    expectedProductId: 'YOUR_PRODUCT_ID',   // your product  });} catch (err) {  if (err instanceof SignatureError) console.error('The license file is missing, damaged or was edited.');  else console.error('License file refused:', err.message);   // expired, other device, OS or product  process.exit(1);} // 3. Use it. It never returns an invalid license: it throws instead.console.log('Licensed to:', lic.customer, 'Expires (unix time):', lic.expires_at ?? 'never');if (lic.features?.pro === true) console.log('Pro features unlocked');

Questions and answers

Why not check the license in the renderer?

Users can open DevTools, change variables and replace functions there. The main process is harder to tamper with and has access to the machine id.

Does it work with electron-builder and Forge?

It is a normal npm dependency of the main process, so both package it as usual.

How do I sell the app?

Connect Stripe, Paddle or Gumroad: a key is created and emailed to each buyer.

How do I ship updates?

client.checkUpdate(key, app.getVersion()) tells a licensed app about a newer version. The answer is signed, including “no update”.