Documentation

TypeScript SDK

@chitmark/sdk: thin fetch client for Node, bundlers, and Workers.

On this page

Package: @chitmark/sdk on npm. Edge entry: @chitmark/sdk/edge. Helpers: chitmarkGate (Express), verifyNextRequest (Next.js).

npm install @chitmark/sdk

PII modes

hashed (default): SHA-256 email, truncated IP. raw: opt-in only. none: derived features only.

Resolve a visiting agent

Use a server key with attribution:read and configure CHITMARK_SITE with your public HTTPS origin. The handler reads the incoming proof, distinguishes an unknown identity from a failed lookup, and handles nullable email. See the Attribution quickstart for disclosure requirements.

import { createServer } from "node:http";
import { Chitmark, ChitmarkError } from "@chitmark/sdk";

const site = process.env.CHITMARK_SITE;
const apiKey = process.env.CHITMARK_API_KEY;
if (!site || !apiKey) throw new Error("Configure CHITMARK_SITE and CHITMARK_API_KEY");
const piiMode = process.env.CHITMARK_ATTRIBUTION_PII_MODE === "raw" ? "raw" : "hashed";
const chitmark = new Chitmark({ apiKey, piiMode });

createServer(async (incoming, response) => {
  if (incoming.url !== "/agent-task") {
    response.writeHead(404).end();
    return;
  }
  response.setHeader("Cache-Control", "no-store");
  try {
    const result = await chitmark.attribution.resolve({
      site,
      request: {
        url: incoming.url ?? "/",
        headers: incoming.headers as Record<string, string>,
      },
    });

    if (process.env.CHITMARK_FIXTURE_LOG === "1") {
      console.log(result.identity.recognized);
      console.log(result.principal.email);
    }

    if (!result.identity.recognized) {
      response.end("Unknown visitor");
      return;
    }
    if (result.principal.status === "previously_verified") {
      const email = result.principal.email;
      response.end(email === null
        ? "Recognized agent; verified principal; email unavailable"
        : "Recognized agent; verified principal; email authorized");
      return;
    }
    response.end("Recognized agent; principal unavailable");
  } catch (error) {
    if (!(error instanceof ChitmarkError)) throw error;
    response.writeHead(503).end("Identity evidence unavailable");
  }
}).listen(3000, "127.0.0.1");