crypto and os modules for HWID generation, keeping your dependency footprint minimal.
Installation
If you are running Node.js 18 or later, the globalfetch API is available natively and you do not need any additional packages. For older Node.js versions, install node-fetch:
If you are targeting Node 18+, remove the
import fetch from "node-fetch" line from the example below — the global fetch is available automatically. The rest of the code is identical.Full Example
Code Walkthrough
HWID generation (generateHWID)
generateHWID concatenates the system hostname, CPU architecture, and the model name of the first CPU into a single string. This combination is stable across reboots but unique enough to distinguish machines in a fleet. The string is then hashed with SHA-256 using Node’s built-in crypto module, converted to uppercase hex, and sliced to 32 characters.
ValidationResponse interface
Casting the parsed JSON to ValidationResponse gives you type-safe access to the valid boolean. The optional error field captures any diagnostic message the server may include without causing a runtime error if it is absent.
Rate-limit and error handling
The function checks for a 429 status before calling res.ok, so you can log a specific warning and fall back to your cached grace period rather than treating it as a generic failure. Any other non-OK status returns false, and the catch block handles all transport-level errors (DNS failures, timeouts, SSL issues) in the same way.
