# Javascript

## Javascript Example

### Headers

| Name          | Value            |
| ------------- | ---------------- |
| Content-Type  | application/json |
| Authorization | Key \<token>     |

### Body

<table><thead><tr><th valign="middle">Name</th><th>Description</th></tr></thead><tbody><tr><td valign="middle">licenseKey</td><td>License key provided by the bot.</td></tr><tr><td valign="middle">productName</td><td>Name of the product.</td></tr><tr><td valign="middle">version</td><td>Current product version.</td></tr><tr><td valign="middle">userId</td><td>Customer discord user Id.</td></tr><tr><td valign="middle">hwid</td><td>Hardware Id of the user.</td></tr></tbody></table>

<pre class="language-javascript"><code class="lang-javascript">const licenseKey = "";
const productName = "";
const version = "";
const userId = "";
const token = "";
const hwid = getHwId();

const res = await fetch("http://&#x3C;DOMAIN>:&#x3C;PORT>/api/v1/key", {
    method: "POST",
    headers: {
        "Authorization": "Key " + token,
        "Content-Type": "application/json"
    },
    body: "{\"licenseKey\": \"" + licenseKey + "\",\"product\": \"" + productName + "\",\"version\": \"" + version + "\",\"hwid\": \"" + hwid + "\",\"userId\": \"" + userId + "\"}"
});

const response = await res.text();
const parsedText = JSON.parse(response);

<strong>if (parsedText &#x26;&#x26; [400, 403, 404].includes(parsedText.statusCode)) {
</strong>    setTimeout(() => {
        console.execute(`Validation failed ⟶  ` + "\x1b[0m" + "( " + "\x1b[31m" + licenseKey + "\x1b[0m" + " )");
        console.log(" ");
        console.execute("Reason: " + "\x1b[0m" + "( " + "\x1b[31m" + `❌ ${parsedText.message}` + "\x1b[0m" + " )");
        return process.exit(1);
    }, 3500);
} else if (parsedText &#x26;&#x26; [200].includes(parsedText.statusCode)) {
    setTimeout(() => {
        console.execute(`License validated ⟶  ` + "\x1b[0m" + "( " + "\x1b[38;5;36m" + licenseKey + "\x1b[0m" + " )");
        console.log(" ");
        console.execute("Message: " + "\x1b[0m" + "( " + "\x1b[38;5;36m" + `✅ You can now use: ${client.user.username}` + "\x1b[0m" + " )");
    }, 3500);
} else {
    setTimeout(() => {
        console.execute(`Validation failed ⟶  ` + "\x1b[0m" + "( " + "\x1b[31m" + licenseKey + "\x1b[0m" + " )");
        console.log(" ");
        console.execute("Reason: " + "\x1b[0m" + "( " + "\x1b[31m" + `❌ ${parsedText.message}` + "\x1b[0m" + " )");
        return process.exit(1);
    }, 3500);
}
</code></pre>

## Get HwId

### Dependencies

```bash
npm install child_process
```

```javascript
const { execSync } = require("child_process");

function getHwId() {
    const platforms = {
        win32: [
            "REG QUERY HKLM\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid",
            /MachineGuid\s+REG_SZ\s+(.*?)\s/
        ],
        darwin: [
            "ioreg -rd1 -c IOPlatformExpertDevice",
            /"IOPlatformUUID" = "(.*?)"/
        ],
        linux: [
            "cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || true",
            /^([\da-f]+)/
        ]
    }

    const getPlatform = platforms[process.platform];
    if (!getPlatform) return;
    const result = getPlatform[1].exec(execSync(getPlatform[0]).toString());
    if (!result) return;
    return result[1] || undefined;
};
```
