Overview
Audit log tracking must be done server-side because your API key should never be exposed to the browser. This guide covers patterns for Next.js App Router with Server Actions and middleware.
Never use your imk_ API key in client-side code, use client components, or any code that runs in the browser. API keys in client-side bundles are visible to anyone inspecting your page source.
Setup
Install the SDK:
Add your credentials to .env.local:
Create a shared client instance:
Server Actions
Track user actions in Server Actions:
API Route Handlers
Track actions in API route handlers:
Middleware for Session Tracking
Use Next.js middleware to generate consistent session IDs:
Then read the session ID in your Server Actions:
Embeddable Viewer
Generate a viewer token server-side and pass it to a client component: