> For the complete documentation index, see [llms.txt](https://vampauth.gitbook.io/vampauth-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vampauth.gitbook.io/vampauth-docs/development/key-check.md).

# Node.js key-check client

Save as check-key.mjs and run with Node.js 18 or later. Set VAMPAUTH\_PROJECT\_ID, VAMPAUTH\_AUTH\_SECRET, VAMPAUTH\_KEY, and APP\_DEVICE\_ID in your backend environment.

This is a real validation call: success activates a pending first-use key. Use management lookup for stock inspection.

```javascript
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";

const projectId = process.env.VAMPAUTH_PROJECT_ID;
const secret = process.env.VAMPAUTH_AUTH_SECRET;
const key = process.env.VAMPAUTH_KEY;
const deviceId = process.env.APP_DEVICE_ID;

if (!projectId || !secret || !key || !deviceId) {
  throw new Error("Set the project ID, auth secret, key, and device ID.");
}

const hmac = (parts) => createHmac("sha1", secret)
  .update(parts.join("|"), "utf8")
  .digest("hex");

async function checkKey() {
  const hwid = createHash("sha256").update(deviceId, "utf8").digest("hex");
  const nonce = randomBytes(16).toString("hex");
  const response = await fetch("https://vampauth.com/api/v1/key/check", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    signal: AbortSignal.timeout(10_000),
    body: JSON.stringify({ project_id: projectId, key, hwid, nonce,
      signature: hmac([nonce, key, hwid]) }),
  });
  const data = await response.json();
  if (!response.ok) throw new Error(data?.error || "HTTP " + response.status);
  if (!data || data.status !== "valid" || data.project_id !== projectId ||
      data.nonce_echo !== nonce || !/^[0-9a-f]{40}$/.test(data.signature)) {
    throw new Error("Invalid key-check response.");
  }
  const expiryMs = data.expires_at === null ? null :
    (typeof data.expires_at === "string" ? Date.parse(data.expires_at) : NaN);
  if (expiryMs !== null && !Number.isFinite(expiryMs)) {
    throw new Error("Invalid expiry in key-check response.");
  }
  const expirySeconds = expiryMs === null ? 0 : Math.floor(expiryMs / 1000);
  const expected = Buffer.from(hmac([nonce, data.status, expirySeconds, projectId]), "hex");
  const received = Buffer.from(data.signature, "hex");
  if (!timingSafeEqual(expected, received)) throw new Error("Response signature did not match.");
  if (expiryMs !== null && expiryMs <= Date.now()) throw new Error("Key expired.");
  return data;
}

try {
  const result = await checkKey();
  console.log("Key accepted. Expires:", result.expires_at ?? "never");
  // Continue with the part of your app that needs access.
} catch (error) {
  console.error("Key check failed:", error.message);
  process.exitCode = 1;
}
```

The request signature uses nonce, key, and HWID. The response signature uses nonce, status, expiry in whole seconds, and the exact project ID sent. Don't interchange those messages or accept a response just because its HTTP status is 200.

## Next steps

* [Standalone key check](/vampauth-docs/api-reference/key-check.md)
* [Keys and expiry](/vampauth-docs/features/keys-and-expiry.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://vampauth.gitbook.io/vampauth-docs/development/key-check.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
