Skip to main content
@snaplog/next is the official Next.js SDK. Send events from server-side code, query stored logs, stream live events into client components, and verify alert webhooks.

Requirements

  • A Next.js app using the App Router
  • A SnapLog API key
  • A runtime with native fetch, Request, Response, and ReadableStream

Installation

Quick start

1. Add your API key

Add the key to .env.local. Keep it server-side — never prefix it with NEXT_PUBLIC_.

2. Create a shared logger

Create the logger in a server-only module such as lib/logs.ts. Do not import it into a client component.

3. Send an event

Use the logger in Route Handlers, Server Actions, or any server-side module.
Events are buffered in memory and flushed in the background, so your handler is not waiting on the network.

Logging

The logger is explicit by design. Nothing is logged automatically — you call it at each site you want to capture.
Every method takes a single payload object. Note this differs from the Express and NestJS SDKs, which take a message and an optional metadata object.

Fields

Field names are snake_case. Both metrics fields are required, so if you pass metrics you must pass both. subsystem accepts "db", "queue", or "network".

Configuration

Delivery

Logs are buffered in memory and flushed every 2 seconds, or earlier once the buffer reaches its batch size. Pending logs are flushed automatically on SIGINT, SIGTERM, and beforeExit when running on Node.

Reading logs in the browser

getLogs and getStream are React hooks for client components. Both go through a route handler in your own app, so your API key is never sent to the browser.

Historical logs

Live events

getStream opens an SSE connection and keeps up to 5,000 events in memory.

Proxy route handlers

You only need these if you use getLogs or getStream from a client component. If you only send logs, or fetch data on the server, you can skip this section.
The route folder is app/api/snapLogs — plural, to match the paths the hooks request.

Verify webhooks

If you receive SnapLog alert webhooks, verify the signature before acting on the event.

Exports