JavaScript SDK

The Unlocktopus JavaScript SDK is a lightweight library that allows you to easily integrate the Unlocktopus API into your JavaScript or NodeJS-based applications. It provides a simple and intuitive interface for interacting with the Unlocktopus API, making it easy to create and manage Licenses.

Installation

To install the Unlocktopus JavaScript SDK, you can use npm or yarn:

Installation

npm install unlocktopus --save

Usage

To use the Unlocktopus JavaScript SDK, you need to import the Unlocktopus class from the SDK and create an instance of it with your API key:

import Unlocktopus from 'unlocktopus'

const unlocktopus = new Unlocktopus(API_KEY)

Once you have an instance of the Unlocktopus class, you can use it to create and manage Licenses. Here's an example of how to create a new License:

const license = await unlocktopus.licenses.create({
  productId: '4e269bf8-e9a1-4049-b423-3abda9947f9e',
  maxActivations: 5,
  expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
  enableOfflineValidation: true,
})

In this example, we create a new License for a product with the ID 4e269bf8-e9a1-4049-b423-3abda9947f9e. The License will have a maximum of 5 activations, an expiration date of 24 hours from now, and can be validated offline.

You can also retrieve a list of all your Licenses:

const licenses = await unlocktopus.licenses.list()

This will return an array of all your Licenses.

The Node.js supports all endpoints marked as Developer, see e.g. the Licenses endpoint.

Error handling

The Unlocktopus JavaScript SDK uses fetch to make HTTP requests to the Unlocktopus API. If an error occurs during a request, the SDK will throw an error with the appropriate details.

Here's an example of how to handle errors:

try {
  const license = await unlocktopus.licenses.create({
    productId: '4e269bf8-e9a1-4049-b423-3abda9947f9e',
    maxActivations: 5,
    expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
    enableOfflineValidation: true,
  })
  console.log('License created:', license)
} catch (error) {
  console.error('Error creating License:', error)
}