Building a desktop app with Electron? You've probably asked yourself: "How do I monetize this?" The answer is software licensing. In this guide, we'll add professional license key validation to your Electron app in under 10 minutes.
Why License Your Electron App?
Electron apps are essentially web apps bundled with Chromium. This makes them easy to develop, but also easy to distribute without authorization. Without proper licensing, anyone can copy your app and use it for free.
A good licensing solution should:
- Validate license keys - Check if the user has paid
- Manage activations - Limit how many devices can use one license
- Work offline - Allow grace periods when there's no internet
- Be hard to bypass - Not just a simple flag in localStorage
Step 1: Install the SDK
First, install the Traffic Orchestrator SDK:
npm install @traffic-orchestrator/client
Step 2: Create a License Validation Module
Create a new file called license.js in your main process:
// license.js
const { TrafficOrchestrator } = require('@traffic-orchestrator/client');
const Store = require('electron-store');
const { machineIdSync } = require('node-machine-id');
const store = new Store();
const to = new TrafficOrchestrator({ apiKey: process.env.TO_API_KEY });
async function validateLicense() {
const licenseKey = store.get('license_key');
if (!licenseKey) return null;
try {
const result = await to.validate({
licenseKey,
machineId: machineIdSync()
});
if (result.valid) {
// Cache the result for offline use
store.set('license_validated', {
valid: true,
expiresAt: result.license?.expiresAt,
validatedAt: new Date().toISOString()
});
return result;
}
} catch (error) {
// Check cached validation for offline support
const cached = store.get('license_validated');
if (cached && isWithinGracePeriod(cached.validatedAt)) {
return { valid: true, cached: true };
}
}
return { valid: false };
}
function isWithinGracePeriod(lastValidated, days = 7) {
const lastDate = new Date(lastValidated);
const now = new Date();
const diffDays = (now - lastDate) / (1000 * 60 * 60 * 24);
return diffDays < days;
}
module.exports = { validateLicense };
Step 3: Create an Activation Dialog
When the app starts without a valid license, show an activation dialog:
// main.js
const { app, BrowserWindow, ipcMain } = require('electron');
const { validateLicense } = require('./license');
let mainWindow;
app.on('ready', async () => {
const license = await validateLicense();
if (!license?.valid) {
// Show activation window
const activationWindow = new BrowserWindow({
width: 400,
height: 300,
webPreferences: { nodeIntegration: true }
});
activationWindow.loadFile('activation.html');
} else {
// Show main app
mainWindow = new BrowserWindow({ width: 1200, height: 800 });
mainWindow.loadFile('index.html');
}
});
Step 4: Handle Activation
Create the activation UI and handler:
// activation.html
<form id="activationForm">
<input type="text" id="licenseKey" placeholder="XXXX-XXXX-XXXX-XXXX" />
<button type="submit">Activate</button>
</form>
<script>
const { ipcRenderer } = require('electron');
document.getElementById('activationForm').onsubmit = async (e) => {
e.preventDefault();
const key = document.getElementById('licenseKey').value;
const result = await ipcRenderer.invoke('activate-license', key);
if (result.success) {
alert('License activated! Restarting...');
ipcRenderer.send('restart-app');
} else {
alert('Activation failed: ' + result.error);
}
};
</script>
Best Practices
- Never expose your API key - Use environment variables
- Implement offline grace periods - Don't lock users out immediately
- Store license data securely - Use electron-store with encryption
- Handle edge cases - What happens when validation fails?
- Provide good UX - Clear error messages and easy activation
Conclusion
Adding license key validation to your Electron app doesn't have to be complicated. With Traffic Orchestrator, you can implement professional licensing in minutes, not days. Our SDK handles the hard parts—validation, activation, offline support—so you can focus on building your app.
Traffic Orchestrator handles the hard parts — validation, activation, offline support — so you can focus on building your app.
Ship licensing in your next release
5 licenses, 500 validations/month, full API access. Set up in under 5 minutes — no credit card required.