To license an Electron app, check the key in the main process and open your real window only when the check passes. Never decide it in the renderer: that is a web page, and anyone can open DevTools on it. Verify a signature on the server's answer so a fake license server fails, save the key with safeStorage, and allow a few days offline for customers who were checked before.
Here is the whole thing, then an honest list of what it stops.
Why the main process
An Electron app has two kinds of code. The renderer is a web page. A user can change its variables, replace its functions and edit its storage while it runs. The main process has no DevTools window by default, owns the windows, and is the only place that should know the answer to "is this licensed?".
A common mistake is to check in the renderer and set isLicensed = true. One line in the console undoes it.
Step 1: Install
npm install @licers/sdk
It has no dependencies of its own. Create a free Licers account, add your app as a product, and copy the public key and product id from the Integration page.
Step 2: The main process
const { app, BrowserWindow, ipcMain, safeStorage } = require('electron');
const { LicenseClient, NetworkError, LicenseError } = require('@licers/sdk');
const fs = require('node:fs');
const path = require('node:path');
const GRACE_DAYS = 7; // how long a checked customer may stay offline
const client = new LicenseClient('YOUR_PUBLIC_KEY', {
productId: 'YOUR_PRODUCT_ID', // refuses keys for your other products
appVersion: app.getVersion(), // shows in Analytics
});
const file = () => path.join(app.getPath('userData'), 'license.dat');
function load() {
try {
return JSON.parse(safeStorage.decryptString(fs.readFileSync(file())));
} catch {
return null; // not saved yet, or copied from another computer
}
}
function save(data) {
if (safeStorage.isEncryptionAvailable()) fs.writeFileSync(file(), safeStorage.encryptString(JSON.stringify(data)));
}
async function check(key) {
key = String(key || '').trim();
if (!key) return { ok: false, message: 'Enter your license key.' };
try {
const result = await client.validate(key);
if (!result.valid) return { ok: false, message: result.error };
save({ key, checked: Date.now() });
return { ok: true, features: result.features };
} catch (err) {
if (err instanceof NetworkError) {
// Offline, or the server is down. That is not "unlicensed".
const saved = load();
const age = saved && saved.key === key ? Date.now() - saved.checked : Infinity;
if (age >= 0 && age < GRACE_DAYS * 86400000) return { ok: true, offline: true, features: {} };
return { ok: false, message: 'Could not reach the license server. Check your connection.' };
}
// A forged answer, or a key for another of your products.
if (err instanceof LicenseError) return { ok: false, message: err.message };
throw err;
}
}
function openWindow(page) {
const win = new BrowserWindow({
width: 1000, height: 700,
webPreferences: { preload: path.join(__dirname, 'preload.js') },
});
win.loadFile(page);
return win;
}
app.whenReady().then(async () => {
const saved = load();
const result = await check(saved && saved.key);
if (result.ok) return openWindow('app.html');
const activation = openWindow('activate.html');
ipcMain.handle('license:activate', async (_event, key) => {
const answer = await check(key);
if (answer.ok) {
openWindow('app.html');
activation.close();
}
return { ok: answer.ok, message: answer.message || '' };
});
});
app.on('window-all-closed', () => app.quit());
The renderer gets one function and one answer. It never sees the client, the saved key or the features:
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('license', {
activate: key => ipcRenderer.invoke('license:activate', key),
});
In activate.html, call window.license.activate(input.value) and show the returned message.
Step 3: Put paid work behind the check, not only the window
Opening the window is the first gate. If a paid feature does its work in the main process or a utility process, check the license there too, so a patched page can't just call it.
Entitlements help here. A key can carry values such as {"pro": true}, and they come from the signed answer:
if (result.features.pro === true) enableProExport();
The offline branch above returns no features on purpose. Decide what your app allows when it can't ask.
Step 4: Turn on the fuses
Electron can be packaged with fuses, switches that are fixed in the binary. These make tampering harder:
EnableEmbeddedAsarIntegrityValidationandOnlyLoadAppFromAsar: refuse an edited or unpackedapp.asar. Supported on macOS and Windows.RunAsNodeoff: your app can't be started as a plain Node.js.EnableNodeOptionsEnvironmentVariableandEnableNodeCliInspectArgumentsoff: nobody can attach a debugger with a command-line flag.
Set them with @electron/fuses, which Electron Forge and electron-builder both support, and sign your builds.
What this stops, and what it does not
- Stopped: shared keys beyond the device limit, key generators, a fake license server, edited answers, replayed answers.
- Made harder: editing
app.asar, attaching a debugger. - Not stopped: somebody who rebuilds your app without the check. Electron ships JavaScript, and minified code can still be read. If one part of your product is worth stealing, run it on a server.
That is true of every licensing system. The aim is to make paying easier than cracking.
Sell it
Connect Stripe, Paddle or Gumroad: each buyer gets a key by email, renewals extend it and refunds revoke it. For machines with no internet, there are signed license files.
More examples, including update checks, are on the Electron page.
Frequently asked questions
Where should an Electron app store a license key?
In the app's userData folder, encrypted with Electron's safeStorage, which uses the operating system's own key store. Don't keep it in localStorage, where any script in the page can read it.
Can users bypass a license check in an Electron app?
A skilled person can, by rebuilding the app without it. Signed answers, a check in the main process and ASAR integrity stop the easy ways: DevTools, a fake server, and an edited app.asar.
Does the Mac App Store or Microsoft Store need license keys?
Apps sold only there use the store's own purchase system. License keys are for apps you sell yourself or through Stripe, Paddle or Gumroad.
How do I give customers a free trial?
Issue a key that expires a set number of days after its first use. The server keeps the date, so changing the computer's clock does nothing. When the customer buys, give them a full key.