Skip to content

Node backend SDK

The Node SDK (sdks/node) is for vendors with their own backend. It verifies license tokens and webhooks on your server and calls the Management API. It is independent of the Rust core and small: three runtime dependencies (@noble/hashes for the key ID, ajv for the protocol schemas, openapi-fetch for the API client). It speaks protocol version 1.

Pre-release. Versions 0.x.y-alpha.n are test versions. The package name is provisional until the product name is settled; the package is not published yet.

Node 20.19 or later. ES modules and CommonJS, with TypeScript types.

Verify a license token

Your app sends its license token (the string the client SDK stores) to your backend, for example in a header. The backend verifies it with the product's public key and reads the entitlements:

import express from "express";
import { Verifier, VerifyError } from "license-sdk-node";

const verifier = new Verifier({
  productId: "prod_01J...",
  environment: "live",
  keys: [process.env.LICENSE_PUBLIC_KEY!], // "k4.public....", from the dashboard
});

const app = express();

app.get("/api/export", (req, res) => {
  try {
    const { state, claims } = verifier.verifyLicense(req.header("x-license-token") ?? "");
    if (claims.ent.pro !== true) return res.status(403).json({ error: "pro_required" });
    res.setHeader("x-license-state", state); // "fresh" or "grace"
    res.json({ ok: true });
  } catch (e) {
    if (e instanceof VerifyError) return res.status(401).json({ error: e.code });
    throw e;
  }
});

verifyLicense runs the checks of protocol section 4 in order: structure, footer, key, signature with the product ID as implicit assertion, schema, product and environment, clock, offline end. It throws a VerifyError whose code is the same as in every SDK (token_expired, signature_invalid, ...). Once the license term has ended and the plan's grace period runs, the result also has licenseGraceUntil, its end in Unix seconds. claims.chn names the release channel, for example beta.

  • Key rotation: pass two keys during a rotation, the old and the new one.
  • Device binding: the backend cannot measure the client's device, so it skips the device check unless you pass check.device.
  • Time: check.now and check.lastServerTime default to the current time and no stored server time. Pass them in tests.

verifyResponse(body, op, check) verifies a signed Client API response the same way the client SDKs do; backends rarely need it.

Call the Management API

import { createManagementClient } from "license-sdk-node";

const api = createManagementClient({
  baseUrl: "https://...",              // your Management API base URL
  apiKey: process.env.MANAGEMENT_KEY!, // mk_live_..., keep it on the server
});

const { data, error } = await api.GET("/v1/api-keys", { params: { query: { limit: 20 } } });

The client is typed from the OpenAPI description (docs/api/management-v1.openapi.json): paths, parameters, bodies and responses are checked by TypeScript. Write calls take an Idempotency-Key header where the API offers one.

Sign user tokens

If your users sign in to your own account system, your backend can let your app activate without a license key (protocol section 6.5). Create a key pair once, register the public key and keep the private key on the server:

import { generateUserTokenKey, signUserToken } from "license-sdk-node";

const { privateKeyPem, publicKey, kid } = generateUserTokenKey(); // once; store privateKeyPem as a secret
await api.POST("/v1/products/{id}/user-token-keys", {
  params: { path: { id: productId } },
  body: { public_key: publicKey, name: "Production backend" },
});

// After your own sign-in, for the app:
const userToken = signUserToken({
  productId,
  environment: "live",
  userId: user.id,              // the customer's external_user_id
  privateKey: process.env.USER_TOKEN_KEY!,
  expiresInS: 300,              // at most 3600
});
  • The app sends the token with activate_with_user_token; the server finds the customer whose external_user_id is userId and activates their license.
  • A product holds up to five keys. To rotate, add the new key, switch the backend over, then remove the old one (DELETE /v1/products/{id}/user-token-keys/{key_id}).
  • userTokenPublicKey(privateKey) gives the public key and key ID of an existing private key.

Verify a webhook

The platform signs every webhook it sends to your endpoint (format: webhooks v1, compatible with Standard Webhooks). Verify the signature over the raw body before you act on it:

import express from "express";
import { WebhookError, WebhookVerifier } from "license-sdk-node";

const webhooks = new WebhookVerifier(process.env.WEBHOOK_SECRET!); // "whsec_...", from the dashboard

app.post("/webhooks/licenses", express.raw({ type: "application/json" }), (req, res) => {
  let event;
  try {
    event = webhooks.verify(req.headers, req.body); // raw bytes, not parsed JSON
  } catch (e) {
    if (e instanceof WebhookError) return res.status(400).json({ error: e.code });
    throw e;
  }
  if (alreadyProcessed(event.id)) return res.sendStatus(200); // deliveries can repeat
  if (event.type === "license.revoked") disableAccess(event.subject_id);
  res.sendStatus(200);
});
  • verify checks the headers, that webhook-timestamp is within 5 minutes of your clock, the HMAC-SHA256 signature and that the body's id matches webhook-id. It throws a WebhookError with code webhook_headers_missing, webhook_timestamp_invalid, webhook_signature_invalid or webhook_payload_invalid.
  • It takes Node's req.headers or a fetch Headers object, and the body as string or bytes. With frameworks that parse JSON for you, read the raw body instead: re-serialized JSON breaks the signature.
  • Secret rotation: pass both secrets, new WebhookVerifier([oldSecret, newSecret]), during the 24 hours in which both sign.
  • Answer with a 2xx status within 15 seconds and do slow work afterwards; anything else counts as failure and is retried for about a day.

For maintainers

  • pnpm sync embeds the JSON schemas from docs/protocol/schema; pnpm gen:api regenerates the API types from docs/api. A test fails while either copy is stale.
  • pnpm test runs all 35 protocol test vectors and the webhook vectors (docs/api/webhooks-v1.vectors.json, from scripts/webhook_vectors.py); pnpm test:package builds both module formats and loads each once.