Node.js SDK

Node.js integration using the native fetch API. Zero dependencies, works with Node.js 18+.

Zero dependencies

Uses native fetch() available in Node.js 18+. No packages to install.

Installation

No packages needed. Just create the SDK file and import it. For Node.js 16 or earlier, install node-fetch.

Project structure
text
my-app/
├── authon.js       ← SDK module
├── index.js        ← Your app
└── package.json

SDK Implementation

authon.js
JavaScript
class Authon {
  constructor(appId, apiKey, apiUrl = "https://api.authon.pro") {
    this.appId = appId;
    this.apiKey = apiKey;
    this.apiUrl = apiUrl;
    this.sessionToken = "";
    this.user = {};
    this.app = {};
    this.lastError = "";
    this.initialized = false;
  }

  async _post(data) {
    try {
      const body = { ...data, appId: this.appId, apiKey: this.apiKey };
      const res = await fetch(`${this.apiUrl}/v1`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(body),
      });
      const result = await res.json();
      if (!result.success) {
        this.lastError = result.message || "Unknown error";
      }
      return result;
    } catch (err) {
      this.lastError = err.message;
      return { success: false, message: err.message };
    }
  }

  async init() {
    const result = await this._post({ type: "init" });
    if (result.success) {
      this.app = result.data;
      this.initialized = true;
      return true;
    }
    return false;
  }

  async login(username, password, hwid = "") {
    const data = { type: "login", username, password };
    if (hwid) data.hwid = hwid;
    const result = await this._post(data);
    if (result.success) {
      this.user = result.data;
      this.sessionToken = this.user.sessionToken || "";
      return true;
    }
    return false;
  }

  async register(username, password, licenseKey, hwid = "") {
    const data = { type: "register", username, password, licenseKey };
    if (hwid) data.hwid = hwid;
    const result = await this._post(data);
    if (result.success) {
      this.user = result.data;
      return true;
    }
    return false;
  }

  async license(licenseKey, hwid = "") {
    const data = { type: "license", licenseKey };
    if (hwid) data.hwid = hwid;
    const result = await this._post(data);
    if (result.success) {
      this.user = result.data;
      this.sessionToken = this.user.sessionToken || "";
      return true;
    }
    return false;
  }

  async check() {
    const result = await this._post({ type: "check", sessionToken: this.sessionToken });
    return result.success;
  }

  async getVar(key) {
    const result = await this._post({ type: "var", sessionToken: this.sessionToken, key });
    if (result.success) return result.data.value;
    return "";
  }

  async setVar(key, value) {
    const result = await this._post({ type: "setvar", sessionToken: this.sessionToken, key, value });
    return result.success;
  }

  async downloadFile(fileId) {
    const result = await this._post({ type: "file", sessionToken: this.sessionToken, fileId });
    if (result.success) {
      const url = `${this.apiUrl}${result.data.downloadUrl}`;
      const res = await fetch(url);
      return Buffer.from(await res.arrayBuffer());
    }
    return null;
  }

  async log(message) {
    const result = await this._post({ type: "log", sessionToken: this.sessionToken, message });
    return result.success;
  }

  static getHWID() {
    const os = require("os");
    const crypto = require("crypto");
    const id = os.hostname() + os.cpus()[0]?.model + os.totalmem();
    return "HWID-" + crypto.createHash("md5").update(id).digest("hex").slice(0, 12).toUpperCase();
  }
}

module.exports = { Authon };

Quick Start

index.js
JavaScript
const { Authon } = require("./authon");
const readline = require("readline");

const APP_ID = "your-app-id";
const API_KEY = "your-api-key";

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const ask = (q) => new Promise((resolve) => rl.question(q, resolve));

async function main() {
  const auth = new Authon(APP_ID, API_KEY);

  // Initialize
  console.log("[*] Connecting...");
  if (!(await auth.init())) {
    console.log("[!] Failed:", auth.lastError);
    process.exit(1);
  }
  console.log(`[+] Connected to ${auth.app.name} v${auth.app.version}`);

  // Login
  const hwid = Authon.getHWID();
  const username = await ask("Username: ");
  const password = await ask("Password: ");

  if (!(await auth.login(username, password, hwid))) {
    console.log("[!] Login failed:", auth.lastError);
    process.exit(1);
  }

  console.log(`[+] Welcome ${auth.user.username}!`);
  console.log(`    Level: ${auth.user.level}`);
  console.log(`    Expires: ${auth.user.expiresAt}`);

  // Verify session
  if (await auth.check()) {
    console.log("[+] Session verified.");
  }

  // Log activity
  await auth.log("User logged in from Node.js app");

  console.log("\nApplication ready!");
  rl.close();
}

main();

License-Only Authentication

License key validation
JavaScript
const { Authon } = require("./authon");

async function main() {
  const auth = new Authon("your-app-id", "your-api-key");
  await auth.init();

  const key = "AUTH-XXXX-XXXX-XXXX"; // from user input
  const hwid = Authon.getHWID();

  if (await auth.license(key, hwid)) {
    console.log(`[+] License valid! Level: ${auth.user.level}`);
    console.log(`    Expires: ${auth.user.expiresAt}`);
  } else {
    console.log("[!] Invalid:", auth.lastError);
  }
}

main();

Server Variables

Working with variables
JavaScript
// Get app-level variable
const downloadUrl = await auth.getVar("download_url");
const latestVersion = await auth.getVar("app_version");

// Set user-level variable
await auth.setVar("theme", "dark");
await auth.setVar("last_used", new Date().toISOString());

console.log("Download URL:", downloadUrl);
console.log("Latest version:", latestVersion);

File Download

Download a protected file
JavaScript
const fs = require("fs");

// Download file after authentication
const fileBuffer = await auth.downloadFile("your-file-id");

if (fileBuffer) {
  fs.writeFileSync("downloaded_module.bin", fileBuffer);
  console.log(`[+] File downloaded (${fileBuffer.length} bytes)`);
} else {
  console.log("[!] Download failed:", auth.lastError);
}

Error Handling

Error handling pattern
JavaScript
if (!(await auth.login(username, password, hwid))) {
  const error = auth.lastError;

  if (error.includes("Invalid credentials")) {
    console.log("Wrong username or password");
  } else if (error.includes("Hardware ID mismatch")) {
    console.log("This account is locked to another device");
  } else if (error.includes("Account banned")) {
    console.log("Your account has been suspended");
  } else if (error.includes("Subscription expired")) {
    console.log("Your subscription has expired");
  } else {
    console.log("Error:", error);
  }
}

Full Working Example

app.js - Complete application
JavaScript
const { Authon } = require("./authon");
const readline = require("readline");

const APP_ID = "your-app-id";
const API_KEY = "your-api-key";

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const ask = (q) => new Promise((resolve) => rl.question(q, resolve));

async function main() {
  const auth = new Authon(APP_ID, API_KEY);

  console.log("[*] Connecting to server...");
  if (!(await auth.init())) {
    console.log("[!] Connection failed:", auth.lastError);
    process.exit(1);
  }
  console.log(`[+] ${auth.app.name} v${auth.app.version}\n`);

  const hwid = Authon.getHWID();
  console.log(`[*] HWID: ${hwid}\n`);

  console.log("[1] Login");
  console.log("[2] Register");
  console.log("[3] License Key\n");

  const choice = await ask("Choice: ");
  console.log();

  let success = false;

  if (choice === "1") {
    const user = await ask("Username: ");
    const pass = await ask("Password: ");
    success = await auth.login(user, pass, hwid);
    if (!success) console.log("[!] Login failed:", auth.lastError);
  } else if (choice === "2") {
    const user = await ask("Username: ");
    const pass = await ask("Password: ");
    const key = await ask("License Key: ");
    success = await auth.register(user, pass, key, hwid);
    if (!success) console.log("[!] Registration failed:", auth.lastError);
  } else if (choice === "3") {
    const key = await ask("License Key: ");
    success = await auth.license(key, hwid);
    if (!success) console.log("[!] Invalid license:", auth.lastError);
  }

  if (!success) {
    rl.close();
    process.exit(1);
  }

  console.log("\n[+] Authenticated!");
  console.log(`    Level: ${auth.user.level}`);
  console.log(`    Expires: ${auth.user.expiresAt}`);

  await auth.log("Application loaded successfully");

  const version = await auth.getVar("app_version");
  if (version) console.log(`    Server version: ${version}`);

  console.log("\nApplication ready!");
  rl.close();
}

main();

Notes

Node.js Version

Requires Node.js 18+ for native fetch. For older versions, install node-fetch and import it.

ESM Support

To use ES modules, rename to .mjs or add "type": "module" to package.json and use import/export syntax.

Electron / NW.js

This SDK works in Electron renderer and main processes. For desktop apps, use Electron to create a native-feeling application.

TypeScript

For TypeScript projects, add type annotations to the class or create a .d.ts declaration file.