Skip to Content
NexPDF

License

Activate a NexPDF license key in Node.js, check which modules it unlocks, and handle license errors.

English

Call activateLicense once per process, before other NexPDF calls. It accepts the license XML file's text or the bare NEX2. key. See Licensing for how keys are issued and bound.

Activate

import { readFile } from 'node:fs/promises';
import { activateLicense } from '@nexdevit/nexpdf-node';

const result = activateLicense(await readFile('nexpdf_license.xml', 'utf8'), 'com.acme.invoicing');
console.log(result === 1 ? 'trial' : 'licensed');
ParameterValue
keyOrXmlThe license XML or the bare key
appIdThe application id the key was issued for; defaults to nexpdf-node

It returns 2 for a commercial key and 1 for a trial key, and throws an Error with the reason when the key is rejected.

Which key

The Node.js SDK runs the WebAssembly engine, which checks keys for platform web. Ask for a web key whose host is your app id (for example com.acme.invoicing), and pass that same id as appId. A server key for the native SDKs is rejected here.

Reading the key from the environment

On servers, keep the key out of the image and read it from a secret:

import { activateLicense } from '@nexdevit/nexpdf-node';

activateLicense(process.env.NEXPDF_LICENSE_KEY, process.env.NEXPDF_APP_ID ?? 'com.acme.invoicing');

Each worker_threads worker loads its own engine, so activate the license in every worker too.

Check the status

import { licenseStatus } from '@nexdevit/nexpdf-node';

const status = licenseStatus();
console.log(status.state, status.tier, status.days_left);

if (status.modules.includes('signatures')) {
  console.log('signing is available');
}

licenseStatus() returns the parsed JSON: state (unlicensed, trial, licensed), licensee, id, tier, modules, expires_at, days_left and watermark.

Handle errors

A rejected key throws an Error whose message gives the reason, for example an expired key or a key for another app id:

try {
  activateLicense(key, 'com.acme.invoicing');
} catch (e) {
  console.error(`NexPDF license rejected: ${e.message}`);
}

A call that needs a module your key does not include throws too:

try {
  doc.encrypt('user', 'owner');
} catch (e) {
  console.log(e.message); // the active license does not include the 'security' module
}

Without a license, or after a rejected key, the SDK stays in viewer mode: it opens, renders, extracts text and searches, and rendered pages carry the "NexPDF • Unlicensed" watermark.

On this page