How to Add Audit Logs to Next.js

TL;DR: Integrate Trailbase audit logging into your Next.js application. Works with both App Router and Pages Router, API routes, and Server Actions.

Step 1: Install the SDK

npm install @trailbase/sdk

Step 2: Initialize the Client

import { TrailbaseClient } from '@trailbase/sdk';

const trailbase = new TrailbaseClient({
  apiKey: process.env.TRAILBASE_API_KEY!,
  region: 'us-east-1', // or 'eu-west-1'
});

For Next.js, initialize the Trailbase client in a shared module (e.g., lib/trailbase.ts) and import it in your API routes and Server Actions. Do not expose the API key to the client — only use Trailbase in server-side code.

Step 3: Track Audit Events

// In an API route or Server Action
export async function POST(req: Request) {
  const session = await getSession();

  await trailbase.track('document.create', {
    actor: session.userId,
    target: documentId,
    metadata: { title, teamId },
  });

  // ... your business logic
}

Step 4: Query Audit Logs

// Query audit logs
const events = await trailbase.search({
  actor: userId,
  action: 'document.*',
  from: '2026-01-01',
});

Other Integrations

Express.jsJavaScript / TypeScriptFastifyTypeScriptNestJSTypeScript

Ready to Add Audit Logs to Next.js?

Join the waitlist for early access. Five-minute setup, no infrastructure changes.

Get Early Access