Add license keys to a Node.js app
To add license keys to a Node.js program, run npm install @licers/sdk, create a LicenseClient with your public key, and await client.validate(key). It returns valid: true only when the server’s Ed25519-signed answer passes verification and the key is allowed on this machine.
Updated by the Licers team
Set it up
- Create a free account and add a product.
- Copy your public key and product id from the Integration page. The public key is safe to ship inside your app.
- Issue a key: set how many devices it may use, when it expires, and any entitlements.
- Add the code below. On the first check the device is bound to the key; past the device limit a new machine is refused.
npm install @licers/sdkimport { LicenseClient, NetworkError } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key let result;try { result = await client.validate(key);} catch (err) { if (err instanceof NetworkError) { // Offline, or the server is down. That is not "unlicensed": try again, or // allow a few days since the last good check. console.error('Could not reach the license server. Please try again.'); } else { // A forged or replayed answer, or a key for another of your products. console.error('License check failed:', err.message); } process.exit(1);} if (!result.valid) { // Expired, revoked, device limit reached... the server says which. console.error('Not licensed:', result.error); process.exit(1);} console.log('Licensed. Expires:', result.expiresAt ?? 'never');Good places to check
- CLI tools: once at start-up, before the first command runs.
- Self-hosted servers: at boot and then on a timer, so a revoked key is noticed without a restart.
- Paid actions: call
recordUsage(key, 1)so “100 exports” or “5,000 AI calls a month” is counted on the server and not in a file the user can edit.
Handle every outcome
validate() resolves with valid: false when the key is refused. It throws SignatureError for a forged answer, and NetworkError or ServerError when the server cannot be reached.
| What happened | What your app should do |
|---|---|
| The key is expired, revoked, blocked or over its device limit | Show 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 nonce | Refuse. Somebody is faking the server. |
| The server cannot be reached, or returns a 5xx error | Retry, or allow a grace period since the last good check. Being offline is not the same as being unlicensed. |
Seats, credits, variables and offline files
Entitlements. Switch features on per key: true/false, numbers or text. They come from the signed answer.
import { LicenseClient } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key const result = await client.validate(key);if (!result.valid) { console.error('Not licensed:', result.error); process.exit(1);} // Entitlements of this key, for example { pro: true, seats: 5, tier: 'gold' }.// They are read from the signed copy, so nobody can edit them on the way.if (result.features.pro === true) console.log('Pro features unlocked'); const seats = result.features.seats ?? 1; // 1 when the key has no "seats"const tier = result.features.tier ?? 'basic';console.log('Seats:', seats, 'Tier:', tier, 'All:', result.features);Offline file. Check a signed .lic file with no internet at all, for air-gapped machines. Signature, product, expiry, device and operating system are all checked on the computer.
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');Usage credits. Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.
import { LicenseClient, UsageExceeded } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key const result = await client.validate(key); // first: this activates the device on the keyif (!result.valid) { console.error('Not licensed:', result.error); process.exit(1);} console.log('Left:', (await client.getUsage(key)).remaining); // reads only; null = no limit try { const usage = await client.recordUsage(key, 1); // spend 1 credit, all or nothing console.log('Done.', usage.remaining, 'credits left');} catch (err) { if (!(err instanceof UsageExceeded)) throw err; console.log('Out of credits. Refill:', err.usage.resets_at ?? 'buy more');}Remote variables. Values you change in the dashboard without shipping an update: a link, a message, a switch.
import { LicenseClient } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key const result = await client.validate(key); // first: this activates the device on the keyif (!result.valid) { console.error('Not licensed:', result.error); process.exit(1);} // Values you set on the Remote Variables page. Signed, and only for valid keys.const url = await client.getVariable(key, 'download_url'); // null when it isn't setconst theme = await client.getVariable(key, 'theme', 'dark'); // or your own defaultconst everything = await client.getVariables(key); // all this key may see console.log(url, theme, everything);Floating seats. Let only N people run the app at the same time, on any computers.
import { LicenseClient, SeatUnavailable } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key try { // Takes a seat and keeps it with heartbeats in the background. const seat = await client.session(key, { onLost: reason => console.error('Seat lost:', reason), // save the user's work }); console.log('Seat taken:', seat.seats); // { used: 1, max: 5 } try { // ... your app runs here. Call this at safe points (before a save, between // jobs). It throws SeatUnavailable once the seat is gone. seat.ensureActive(); } finally { await seat.stop(); // frees the seat for someone else }} catch (err) { if (!(err instanceof SeatUnavailable)) throw err; console.error('No seat:', err.message);}Updates. Tell the customer about a new version. Only a valid, active key is given the download link.
import { LicenseClient } from '@licers/sdk'; const client = new LicenseClient('YOUR_PUBLIC_KEY', { // safe to ship inside your app productId: 'YOUR_PRODUCT_ID', // your product: refuses keys for your other products appVersion: '2.4.1', // shows in Analytics -> App versions});const key = 'XXXX-XXXX-XXXX-XXXX'; // the customer's license key const update = await client.checkUpdate(key, '2.4.1', 'stable'); // or 'beta' if (update.updateAvailable) { console.log('New version:', update.version); console.log("What's new:", update.notes); // The answer is signed, but downloadUrl and notes are not: check the URL is // on your own host, and verify the file against a hash you publish. console.log('Download:', update.downloadUrl);} else { console.log('You have the latest version.');}Questions and answers
Does it work with both import and require?
Yes. The package supports ES modules and CommonJS.
Can I use it in a browser or a React page?
No. It needs Node’s crypto and the machine id. For desktop apps with a web interface, use it in the Electron main process.
Does it stop people editing my JavaScript?
No library can. It makes the answer impossible to fake; put valuable logic behind your own server or compile it.
What does it cost?
Nothing today. Every feature is free and unlimited: products, keys, devices, validations, offline files and floating seats. See pricing.