@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, andReadableStream
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 aslib/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.Logging
The logger is explicit by design. Nothing is logged automatically — you call it at each site you want to capture.- Methods
- Examples
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 onSIGINT, 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 usegetLogs 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.