Offline licensing

It's common to have apps that need to work even without an internet connection. Keyforge makes it easy to validate licenses oflline.

An internet connection is only required to activate the license and to occasionally refresh the license token.

To make this possible, Keyforge can issue a signed license token that can be verified on the client. The token is a JWT and contains information about the license.

Get started

There is a client SDK available for JavaScript, but license tokens can be used in any programming language that supports JWTs.

Configure license tokens for a product

Go to license tokens and add a new product. You can edit how much time a token will be valid for, and other options after creating the new configuration.

For setups with more than one product, you can duplicate the signing key pair from another product inside the edit menu. You can also import an external key pair.

Install the client SDK

Install the Keyforge client SDK in your project:

npm install @keyforge/client

Retrieve initial token

The simplest way to get and store the first license token for a device is after activating a license. The SDK provides activateLicense.

import { activateLicense } from '@keyforge/client';
 
const { isValid, token } = await activateLicense({
  licenseKey: 'ABCDE-ABCDE-ABCDE-ABCDE-ABCDE',
  deviceIdentifier: 'some_device_id',
  deviceName: 'My device',
  productId: 'p_123456',
});
 
if (isValid && token) {
  storeToken(token); // Store the token on the device
 
  console.log('License activated successfully!');
}

Validate and refresh tokens

The SDK provides a simple way to validate and automatically refresh license tokens. You should call validateAndRefreshToken when your app starts.

import { validateAndRefreshToken } from '@keyforge/client/token';
 
const PUBLIC_KEY = '...'; // Copied from the dashboard. In JSON string or object format
 
const { isValid, token, data, isValidButExpired } =
  await validateAndRefreshToken({
    token: getStoredToken(), // The current license token
    publicKeyJwk: PUBLIC_KEY,
    deviceIdentifier: 'some_device_id',
    productId: 'p_123456',
  });
 
if (isValid) {
  storeToken(token); // Store the new token if it was refreshed
 
  console.log('License token is valid!', data.license.key);
} else if (!isValidButExpired) {
  // A network error probably occurred. The token is expired, but was valid
  // You should NOT prompt the user to activate a license
}
 
// If the token is not valid, you should prompt the user to activate a license

Learn more

Example

An example desktop app built with Electron using the Keyforge client SDK and license tokens is available here.

API Reference

On this page