Guide
How to Add License Keys to an Electron App (Offline-Ready)
Selling an Electron desktop application needs a licensing layer that meets two requirements. It must not disrupt paying customers, and it must not trust a client-side check that can be faked in seconds.
- It must not disrupt legitimate paying customers. The app must launch instantly even when the user is offline, on a train, or behind an unstable connection.
- It must not trust naive client-side validation. The check cannot rely on a plain HTTP response that can be intercepted and mocked in seconds.
This guide implements node-locked, tamper-proof license verification in Electron with asymmetric Ed25519 signatures and offline leases. For the full API reference see the Keyfern documentation.
The fatal flaw in basic HTTP license checks
Many developers implement licensing with a plain API request:
// INSECURE PATTERN - DO NOT USE
const response = await fetch("https://api.myapp.com/verify?key=" + userKey);
const data = await response.json();
if (data.valid === true) {
launchApp();
}
This has two major flaws:
- Trivial bypass. Anyone can edit their local
hostsfile (/etc/hosts, orC:\Windows\System32\drivers\etc\hosts), pointapi.myapp.comto127.0.0.1, and run a five-line local server that returns{ "valid": true }. The app stays unlocked forever. - Zero offline capability. With no internet the check fails and locks out paying customers.
The secure pattern: Ed25519 cryptographic leases
To secure client validation without intrusive DRM or kernel drivers, verification must rely on asymmetric cryptography:
- The server signs the payload. When a key is validated, the server builds a payload with the key, the machine identifier, feature flags and an expiration timestamp (
lease_until), and signs it with its Ed25519 private key. - The Electron app embeds only the public key. The public key is safe to expose in client code.
- Local mathematical validation. The app verifies the signature against the public key and checks that the nonce and machine match.
- Offline caching. The verified, signed answer is cached on disk. The app runs offline until
lease_until. A local mock server cannot forge a valid signature because it lacks the private key.
Step-by-step implementation
You can follow along with the open-source boilerplate: electron-license-key-example on GitHub.
Step 1: Install the Keyfern SDK
The JavaScript SDK runs in Node.js 20+ and Electron with zero dependencies and ships TypeScript types:
npm install keyfern
No access to the npm registry? npm install https://keyfern.com/dl/keyfern-1.1.0.tgz
Step 2: Initialize the licensing client
In the Electron main process, create the client with your product ID and the product's Ed25519 public key. Both are on the product's Integrate tab in the dashboard. fileStorage keeps the last signed answer on disk for offline use.
import { Keyfern, fileStorage, machineId } from 'keyfern';
import { app } from 'electron';
import path from 'node:path';
const licensing = new Keyfern({
product: 'prod_your_product_id',
publicKey: 'YOUR_ED25519_PUBLIC_KEY_BASE64URL',
storage: fileStorage(path.join(app.getPath('userData'), 'license.cache')),
});
Offline leases are off by default. A new product has lease_days = 0, so lease_until equals the time of the answer and the app must be online. To let paying users launch offline, set the lease length (for example 30 days) in your product settings in the dashboard.
Step 3: Handle the initial activation
When a customer enters their key for the first time, activate it. The SDK verifies the signature and stores the signed answer for you.
async function activateKey(licenseKey) {
try {
const lic = await licensing.activate(licenseKey, await machineId(), 'User Desktop');
return { success: true, leaseUntil: lic.lease_until, features: lic.features };
} catch (error) {
return { success: false, error: error.code ?? error.message }; // e.g. machine_limit, expired, invalid_key
}
}
Step 4: Verify the lease on startup
On every launch check the cached license first, without the network. cached() verifies the Ed25519 signature, the machine, the expiry, the lease and clock rollback.
async function checkLicenseOnLaunch() {
try {
const lic = await licensing.cached(await machineId());
return { licensed: true, offline: true, leaseUntil: lic.lease_until };
} catch (err) {
// lease_expired, clock_rollback, expired, blocked, invalid signature, nothing cached ...
return { licensed: false, reason: err.code ?? 'CORRUPTED_CACHE' };
}
}
If the cached lease is valid, open the app window immediately. When a connection is available, call activateOrCached(key, machine) in the background to refresh the lease: it checks online when it can and falls back to the verified cache when offline.
Preventing machine abuse and piracy
1. Node-locking
Keyfern tracks activations by machine. Set a strict limit (for example 2 machines per license) on the product or on an individual key. Once the limit is reached, activation on another machine fails with a clear machine_limit error.
2. Floating licenses
If customers share workstations or use temporary virtual environments, set a seat timeout on the product: a machine that has been silent for that long stops counting. Call licensing.deactivate(key, machine) when the user quits to free the seat at once. Heartbeat code and settings: floating licenses guide.
3. Clock rollback protection
Users often try to extend a time-limited license by setting their calendar back. The SDK remembers the latest time it has seen and refuses the license with clock_rollback if the system clock moves back by more than one hour.
Automating key delivery with Stripe
Skip hand-built key-generation webhooks and merchant-of-record commissions:
- Create a Payment Link or Checkout Session in your own Stripe account.
- Add the product's Keyfern webhook endpoint in your Stripe dashboard (it needs a paid plan).
- When a customer completes checkout, Keyfern creates the license key and emails it to the customer.
- For subscriptions, Stripe renewal events (
invoice.paid) extend the key's expiration date.
Setup details are in Stripe webhook setup.
Comparison: Keyfern vs. other licensing platforms
Entry-level public pricing, checked against each vendor's pricing page in November 2026. Vendors change plans, so confirm on their sites.
| Feature | Keyfern | Devolens (Cryptolens) | LicenseSpring |
|---|---|---|---|
| Free tier | 50 keys, no credit card | 10 keys (after 30-day trial) | 100 licenses (1,000 API requests/month) |
| Entry paid plan | $10/month (Starter, 1,000 keys) | €299/month (Business) | $199/month (Business Starter) |
| API requests | No per-call fees | 250,000/month on Business | 10,000/month on Business Starter |
| Offline verification | Ed25519 signed leases | See each vendor's documentation | |
Getting started
Inspect the full working implementation in the GitHub repository: https://github.com/velopre/electron-license-key-example.
To create your keys and configure your first product for free, create an account on keyfern.com, then read the general documentation. Shipping with Tauri instead? See the Tauri and Rust licensing guide.