Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.fact0.io/llms.txt

Use this file to discover all available pages before exploring further.

Express

Install

npm install @fact0/sdk

Audit middleware

import express from "express";
import { Fact0Client } from "@fact0/sdk";

const lf = new Fact0Client({ apiKey: process.env.FACT0_API_KEY! });
const app = express();

app.use(async (req, res, next) => {
  res.on("finish", () => {
    void lf.audit.log({
      actor: { id: (req as any).user?.id ?? "anonymous", type: "human" },
      action: `api.${req.method.toLowerCase()}`,
      resource: { id: req.path, type: "endpoint" },
      outcome: res.statusCode < 400 ? "success" : "failure",
      metadata: { status: res.statusCode },
    });
  });
  next();
});

Manual instrumentation

app.post("/refund/:orderId", async (req, res) => {
  await lf.audit.log({
    actor: { id: req.user.id, type: "human" },
    action: "order.refund",
    resource: { id: req.params.orderId, type: "order" },
    outcome: "success",
  });
  res.json({ ok: true });
});