Light Docs

Integrating in Node.js

A guide integrating the licensing system in your Node.js app.

Introduction

This guide will help you integrate the OmegaLicensing system into your Node.js application.

This guide assumes the usage of latest Node.js, as of writing that is LTS 20.

It also assumes the prior setup of the OmegaLicensing web API. That implies that it's available on a public URL and you have a license key to validate.

Code

Add the following code in the initialization of your Node.js application.

async function validateLicense(apiUrl, licenseKey, productId) {
	try {
		const response = await fetch(`${apiUrl}/licenses/validate?key=${licenseKey}&product=${productId}`);
		const data = await response.json();

		if (data.valid) {
			console.log("License key is valid.");
		} else {
			console.log("Invalid license key. Exiting program.");
			process.exit(1);
		}
	} catch (error) {
		console.error("Error validating license:", error);
		process.exit(1);
	}
}

// Replace this API URL with your own api url.
const API_URL = "http://your-api-url";
// Replace this license key with the one you want to validate.
const LICENSE_KEY = "user-submitted-key";
// Replace this with the product ID of the product you want to validate the license for.
const PRODUCT_ID = 1;

validateLicense(API_URL, LICENSE_KEY, PRODUCT_ID);

This code will validate the license key against the OmegaLicensing web API and exit the app if the key is invalid.

Note about security

Though JavaScript code can be minified, obfuscated and otherwise hidden, it will never be completely uncrackable, such as any other piece of software.

Therefore, if you're sharing the source code of your app, always make sure to implement additional security measures to protect your software from unauthorized use.

TypeScript

If you're using TypeScript, you can use the following code, which is an adaptation of the above snippet:

async function validateLicense(apiUrl: string, licenseKey: string, productId: string): Promise<void> {
	try {
		const response = await fetch(`${apiUrl}/licenses/validate?key=${licenseKey}&product=${productId}`);
		const data = await response.json();

		if (data.valid) {
			console.log("License key is valid.");
		} else {
			console.log("Invalid license key. Exiting program.");
			process.exit(1);
		}
	} catch (error) {
		console.error("Error validating license:", error);
		process.exit(1);
	}
}

// Replace this API URL with your own api url.
const API_URL = "http://your-api-url";
// Replace this license key with the one you want to validate.
const LICENSE_KEY = "user-submitted-key";
// Replace this with the product ID of the product you want to validate the license for.
const PRODUCT_ID = "1";

validateLicense(API_URL, LICENSE_KEY, PRODUCT_ID);