All posts

How to setup license keys in an Electron application

Nicholas Affonso

November 19, 2025

How to setup license keys in an Electron application

Paid Electron apps need a secure way to block unauthorized access, often with license keys. In this guide, you'll integrate Keyforge into an Electron application for reliable licensing.

You'll also see how to set up payments via Stripe and how to support offline license verifications using the Keyforge client SDK.

Before you start

Make sure you've created the basics inside your Keyforge account:

Activating a license

When a user first opens your app, prompt them to enter their license key. Then, send that key to Keyforge to activate it on the user's device.

import { machineId } from 'node-machine-id';
import os from 'os';

await fetch('https://keyforge.dev/api/v1/public/licenses/activate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    licenseKey: 'ABCDE-ABCDE-ABCDE-ABCDE-ABCDE',
    deviceIdentifier: await machineId(),
    deviceName: os.hostname(),
    productId: 'p_123456',
  }),
});

console.log('License activated');

Device identifier

A common way to get a unique device identifier in Electron apps is to use the node-machine-id package. You can also use a persistently stored UUID, or any other unique identifier for the device.

After activation, store the license key locally so it can be used for future verifications.

Validating a license

Your app should validate the license key on each launch to ensure it's still valid. Use the Keyforge API to check the license status.

import { machineId } from 'node-machine-id';

const res = await fetch(
  'https://keyforge.dev/api/v1/public/licenses/validate',
  {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' }
    body: JSON.stringify({
      licenseKey: 'ABCDE-ABCDE-ABCDE-ABCDE-ABCDE',
      deviceIdentifier: await machineId(),
      productId: 'p_123456',
    }),
  }
);

const data = await res.json();
if (data.isValid) {
  console.log('License is valid');
} else {
  console.log('License invalid');
}

If Keyforge indicates the license is invalid, prompt the user to re-enter their license key. This call can be made periodically, such as every few hours.


Payments and customer portal

When someone purchases your app, Keyforge can automatically generate and deliver a license key. Follow the Stripe integration guide to connect your product.

Customers can also view and manage their licenses through the Keyforge portal. This includes the ability to:

  • View licenses and activations
  • Reset active devices
  • Download invoices and manage their subscriptions

Offline licensing

If your Electron app needs to run in environments without constant internet access, you can use the license tokens feature. These are signed JWTs that your app can verify locally. We also offer a JavaScript SDK to simplify verifications.

import { validateAndRefreshToken } from '@keyforge/client/token';

const { isValid, token, data } = await validateAndRefreshToken({
  token: getStoredToken(),
  publicKeyJwk: '...',
  deviceIdentifier: 'some_device_id',
  productId: 'p_123456',
});

if (isValid) {
  storeToken(token);

  console.log('License token is valid!', data.license.key);
}

When license tokens are enabled for a product, you also get a token property in the activation response. For a full guide, check out the docs.


Conclusion

You now have a full licensing setup to protect your commercial Electron app. Keyforge helps protect your application while providing a smooth experience for users, whether they're online or offline.

For a more detailed look, visit the Keyforge documentation.

Simplify your licensing process

Focus on building your product and let us handle licensing. Generate license keys via Stripe and offer your customers a smooth self-serve experience.