@n8n_io/license-sdk
v4.4.0
Published
License SDK
Keywords
Readme
SDK
The SDK provides a convenient way to work with license keys issued by the n8n license-server.
Usage
import {
LicenseManager,
TLicenseBlock,
} from '@n8n_io/license-sdk/src/LicenseManager'
import pino from 'pino'
// bring your own logger, e.g. Pino, Winston etc.
const myLogger = pino()
// ------ INITIALIZATION ------
const license = new LicenseManager({
// `server` also selects the trusted CA: only the staging CA for license-staging.n8n.io, otherwise production.
// No option or environment variable can supply another CA.
server: 'https://license.your-server.com',
tenantId: 1, // referencing to resp. license-server entity
productIdentifier: 'Demo Product v1.2.3', // up to 255 characters; must match product.identifierPattern on activation
autoRenewEnabled: true,
autoRenewTimer: true, // optional, set to false to schedule `renewIfDue()` yourself instead of the built-in timer
renewOnInit: false,
autoRenewOffset: 60 * 60 * 48, // = 48 hours
renewalNotFoundGracePeriodMs: 24 * 60 * 60 * 1000, // optional, defaults to 24 hours
logger: myLogger,
loadCertStr: async () => {
// code that returns a stored cert string from DB
return '...'
},
saveCertStr: async (cert: TLicenseBlock) => {
// code that persists a cert string into the DB
// ...
},
deviceFingerprint: () => 'a-unique-instance-id', //optional! If omitted, a machine-ID fingerprint will be generated
installId: 'example-install-id', // optional; use a stable ID for this installation
onFeatureChange: () =>
console.log('availability of some features has just changed'), //optional
onLicenseRenewed: () => console.log('license was successfully renewed'), //optional
onExpirySoon: () => console.log('license is about to expire'), //optional
expirySoonOffsetMins: 180, //optional
})
// important! Attempts to load and initialize the cert:
await license.initialize()
// without a valid cert, no feature will be available:
if (license.hasFeatureEnabled('a-special-feature')) {
// this code will never execute!
}
license.isValid() // -> false
license.getFeatureValue('another-feature') // -> undefined
license.getFeatures() // -> []
// ------ ACTIVATION ------
try {
// download a license key by 'activating' a reservation.
// Reservations are typically configured to be activatable only once.
await license.activate('3fa85f64-5717-4562-b3fc-2c963f66afa6')
license.isValid() // -> true
license.getFeatureValue('foo') // -> 'bar'
if (license.hasFeatureEnabled('a-feature-that-exists')) {
//do stuff
}
const currentConsumption = getUsageFromSomewhere()
if (license.hasQuotaLeft('quota:demoQuota', currentConsumption)) {
//do stuff
}
// print a human-readable string representation.
console.log(`${license}`)
} catch (e) {
// handle error...
}
// ------ RENEWAL ------
// renewals happen automatically based on the config options `autoRenewEnabled` and `autoRenewOffset`
// but can also be enforced, as long as the current cert is not yet terminated:
await license.renew()
// with `autoRenewTimer: false`, run the periodic check from your own scheduler.
// It reloads the cert if the store changed, renews only when due, and throws on failure:
await license.renewIfDue()
// when another process renewed and saved a new cert, load it without a renewal.
// It is a no-op if the store is unchanged, and throws when the store cannot be read:
await license.reloadStoredCert()Set productIdentifier to a string of at most 255 characters. The server
rejects a longer one on activation and renewal (HTTP 400).
installId accepts a string or a sync or async function that returns a string.
The SDK resolves it once per initialization and sends it on activation and
renewal. Use a stable ID of 1 to 128 characters (ASCII letters, digits, _,
or -). If the value doesn't match or the function throws, the SDK logs a
warning and leaves the ID out. The server records it for usage attribution;
license binding still uses the device fingerprint.
License details
Use these methods to read license details without parsing the human-readable
toString() output:
isEphemeral(): booleanreturns the loaded certificate'sisEphemeralflag. It returnsfalsewhen no certificate is loaded, including beforeinitialize().toString()printsisEphemeral: <n/a>in that case, so code that parsed the text and treated a missingisEphemeral: falseline as ephemeral will getfalsefrom this method instead.getDeviceFingerprint(): string | undefinedreturns the fingerprint computed for this instance duringinitialize(), using the configureddeviceFingerprintcallback or the default machine ID. It returnsundefinedbeforeinitialize()and afterreset(), and is set even without a certificate. This is the instance fingerprint, which can differ from the certificate'sdeviceFingerprint.
Request correlation
The SDK sends an x-request-id header on every activation and renewal request,
including renewals that return floating entitlements to the pool. Each call uses a
fresh UUID generated by crypto.randomUUID(). The ID is random and is not derived
from the license key, instance ID, device fingerprint, or request payload.
This ID helps correlate SDK requests with license-server logs when troubleshooting.
The SDK uses the response's x-request-id if present, otherwise the ID it sent.
Request failures include the ID in the error message and warning log through the
configured logger. Successful requests log the ID at debug level.
Renewal errors
When /renew returns NOT_FOUND, the SDK keeps the local certificate. It deletes
the certificate only after renewalNotFoundGracePeriodMs (24 hours by default)
of uninterrupted NOT_FOUND replies. CONSUMER_TERMINATED deletes the
certificate at once.
