> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shieldlabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js

> Read and evaluate a stored identification, then verify raw-body webhook signatures in your backend.

Read and evaluate a stored identification, then verify raw-body webhook signatures in your backend. This guide uses the supported ShieldLabs package API, not a standalone generated client.

## Before you start

Node.js 18+ with a Private API Key. The browser must already send a Request ID to your server.

Register and verify the website domain in your [analytics dashboard](https://app.shieldlabs.ai/). Use credentials from the same domain and environment as the browser check. For a fresh account, start with [Quick Start](/quickstart).

## Get your credentials

Copy the domain's **Private API Key** from **Integration > API keys** and store it as `SHIELDLABS_API_KEY` in your server environment. For webhook verification, also set `SHIELDLABS_WEBHOOK_SECRET` from **Integration > Webhooks**. Never expose either secret to the browser.

## Install the SDK

```bash theme={null}
npm install @shieldlabs-ai/node
```

Use `yarn add` or `pnpm add` with the same package names if your project uses that package manager. Framework packages remain the application's responsibility.

## Add the integration

```ts theme={null}
import { ShieldLabs, evaluateIdentification, webhooks } from '@shieldlabs-ai/node';

const shieldlabs = new ShieldLabs({ apiKey: process.env.SHIELDLABS_API_KEY! });

// One identification authorizes one action. This in-memory set keeps the example short; in
// production, claim request IDs atomically in Redis or your database (see "Apply a policy").
const usedRequestIds = new Set<string>();

function claimRequestId(requestId: string): boolean {
  if (usedRequestIds.has(requestId)) return false;
  usedRequestIds.add(requestId);
  return true;
}

// 1. Call this with the requestId the browser sent together with the signup form.
export async function allowSignup(requestId: string): Promise<boolean> {
  // Scoring is asynchronous: this waits (up to 10 s by default) until the verdict is stored.
  const identification = await shieldlabs.identifications.get(requestId);

  // 2. Missing, reused, stale, rate-limited, automated or dangerous: refuse.
  const firstUse = identification !== null && claimRequestId(identification.request_id);
  const verdict = evaluateIdentification(identification, { isReplay: () => !firstUse });
  return verdict.ok;
}

// 3. Call this with the raw body and the X-Shield-Signature header of a webhook delivery.
export function handleWebhook(rawBody: string | Uint8Array, signatureHeader: string | null): void {
  const event = webhooks.constructEvent(
    rawBody,
    signatureHeader,
    process.env.SHIELDLABS_WEBHOOK_SECRET!,
  );
  if (event.event_type === 'identification.scored') {
    console.log(event.data.request_id, event.data.risk_score, event.data.detection_flags.vpn);
  }
}
```

Environment variables used throughout: `SHIELDLABS_API_KEY` (Private API Key, `sec_...`),
`SHIELDLABS_WEBHOOK_SECRET` (endpoint signing secret, `whsec_...`), `SHIELDLABS_SECRET_KEY` and
`SHIELDLABS_DOMAIN` (Management API), and `SHIELDLABS_API_BASE_URL` /
`SHIELDLABS_MANAGEMENT_BASE_URL` to point at another host in development and tests (https, or
plain http on `localhost`). The SDK never reads the environment itself: pass the values to the
constructors. For development and staging, register a separate domain in the analytics dashboard
and use its keys.

A runnable version of this flow is in [`examples/node-http`](https://github.com/ShieldLabs-ai/shieldlabs-node/tree/e67e3f5cab3e04d437781b0cffd597b2b89375a1/examples/node-http).

## Test the complete flow

1. Use the Request ID from a real browser check on the same domain as the Private API Key.
2. Confirm the server retrieves that identification and its Risk Score.
3. Test an invalid or missing ID and an unavailable API: none should be treated as a clean identification.
4. For webhooks, test the original raw body with its signature, then change one byte and confirm rejection.

In-memory replay stores in examples are demonstrations, not shared production storage. Claim accepted IDs atomically in a durable database or cache, enforce freshness, authenticate the action, and check the expected domain and user association. Reading a valid identification alone does not authorize a business action.

## Troubleshooting

* No History row: confirm the registered domain, credential/environment match and that the browser remained open while collectors posted.
* Missing or pending verdict: scoring is asynchronous. The server helper waits within a bounded budget; handle a missing result and API errors explicitly.
* Authentication error: use the Private API Key for History, not a Public Key, Management Secret Key or MCP OAuth token.
* Invalid webhook signature: verify the original raw bytes with the endpoint's full signing secret, before trusting parsed JSON.

## Next steps

* [Runnable example](https://github.com/ShieldLabs-ai/shieldlabs-node/tree/e67e3f5cab3e04d437781b0cffd597b2b89375a1/examples/node-http)
* [SDK reference and changelog](https://github.com/ShieldLabs-ai/shieldlabs-node)
* [Identification flow](/api/identification-flow)
* [Server API](/api/server-api)
* [Webhook setup](/setup/webhooks)
* [Content Security Policy](/setup/csp)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.