Quickstart: mount, record, type your actions
Quickstart
Section titled “Quickstart”Standalone (Docker)
docker run -p 3000:3000 -e STORAGE_DRIVER=memory ghcr.io/xantiagoma/xtandard-audit# open http://localhost:3000CLI (npx / bunx)
bunx @xtandard/audit serve # STORAGE_DRIVER=file by default → ./.auditHono
import { Hono } from "hono";import { auditPanel } from "@xtandard/audit/hono";import { createMemoryStorage } from "@xtandard/audit/storage/memory";
const audit = auditPanel({ storage: createMemoryStorage(), basePath: "/audit" });const app = new Hono();app.route("/audit", audit);Elysia
import { Elysia } from "elysia";import { auditPanel } from "@xtandard/audit/elysia";import { createMemoryStorage } from "@xtandard/audit/storage/memory";
new Elysia() .mount("/audit", auditPanel({ storage: createMemoryStorage(), basePath: "/audit" })) .listen(3000);Express
import express from "express";import { auditPanel } from "@xtandard/audit/express";import { createMemoryStorage } from "@xtandard/audit/storage/memory";
const app = express();app.use("/audit", auditPanel({ storage: createMemoryStorage(), basePath: "/audit" })); // before body-parserapp.listen(3000);Bun
import { auditPanel } from "@xtandard/audit/bun";import { createMemoryStorage } from "@xtandard/audit/storage/memory";
const audit = auditPanel({ storage: createMemoryStorage(), basePath: "/audit" });Bun.serve({ port: 3000, fetch: audit.fetch });Record your first events
Section titled “Record your first events”import { createAuditCore } from "@xtandard/audit";import { createPostgresStorage } from "@xtandard/audit/storage/postgres";
const audit = createAuditCore({ storage: createPostgresStorage({ url: process.env.DATABASE_URL! }),});
// In the handler where the thing actually happens:await audit.record({ tenant: org.id, // the customer organization action: "member.role_changed", actor: { id: user.id, name: user.name, email: user.email }, targets: [{ type: "user", id: member.id, name: member.name }], context: { ip, userAgent, requestId }, metadata: { from: "member", to: "admin" },});One line of middleware fills tenant, actor, ip, userAgent and requestId for every event in a request (auditContext() in each framework adapter, or runWithAuditContext from @xtandard/audit/context).
Typed actions
Section titled “Typed actions”Declare your catalog once with any Standard Schema validator — valibot, zod, arktype — and record() is type-checked and validated:
import * as v from "valibot";import { createAuditCore, defineActions } from "@xtandard/audit";
const actions = defineActions({ "invoice.paid": { metadata: v.object({ invoiceId: v.string(), amount: v.number() }), targets: ["invoice"], description: "An invoice was paid", group: "Billing", }, "member.removed": { targets: ["user"] },});
const audit = createAuditCore({ storage, actions });await audit.record({ tenant, action: "invoice.paid", actor, metadata: { invoiceId: "inv_1", amount: 4200 },});// ^ autocompletes; typos and wrong metadata are compile errors