Installation
Install @sophonz/nextjs, wire register() into instrumentation.ts, and mount SophonzProvider in the App Router root layout.
Install @sophonz/nextjs, wire register() into instrumentation.ts, and mount SophonzProvider in the root layout.
Quick start
Enter the collector URL, project, app name, version and app key below to generate the snippet from those values. Advanced options are behind the "Advanced options" toggle.
// app/layout.tsx
import { SophonzProvider } from '@sophonz/nextjs/client';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<SophonzProvider
collectorUrl="https://in.sophonz.ai"
project="sophonz"
appName="next-sample"
appVersion="1.0.0"
appKey="sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz"
>
{children}
</SophonzProvider>
</body>
</html>
);
}
// instrumentation.ts (server side, same values)
export async function register() {
const { register } = await import('@sophonz/nextjs/server');
await register();
}Opens a detail screen in a sample app. The browser SDK captures an action, its duration, Fetch and XHR, console and application logs, and a handled exception.
Both modes deliberately record one handled exception. Navigation is recorded in the session. Web Vitals depend on what the browser observes. Replay and sockets are outside this sample.
Requirements
- Next.js
>= 13.4.0, App Router - React
>= 18
NOTE — instrumentation.ts on Next.js < 15
instrumentation.ts became stable — no flag required — in Next.js 15. On earlier versions, enable it first:
// next.config.js
module.exports = {
experimental: { instrumentationHook: true },
};Install
bun add @sophonz/nextjs
# or
pnpm add @sophonz/nextjs
# or
npm install @sophonz/nextjsServer: wire up register()
Create instrumentation.ts at the project root:
// instrumentation.ts
export { register } from '@sophonz/nextjs/server';Next calls register() once per server process, before any request is handled — the only point early enough for the module patching auto-instrumentation depends on.
To configure in code instead of the environment, wrap it:
// instrumentation.ts
import { register as sophonz } from '@sophonz/nextjs/server';
export const register = () =>
sophonz({
appName: 'my-app',
server: { advancedNetworkCapture: true },
});register() is a no-op on the Edge runtime — the check is written against process.env.NEXT_RUNTIME directly, so the bundler folds the branch and drops the Node.js SDK from the Edge bundle entirely.
Client: mount SophonzProvider
Add it once in the root layout so every route is covered:
// app/layout.tsx
import { SophonzProvider } from '@sophonz/nextjs/client';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<SophonzProvider>{children}</SophonzProvider>
</body>
</html>
);
}SophonzProvider is a Client Component; the layout around it stays a Server Component. It initializes once on mount and dynamically imports @sophonz/browser-sdk, so it never blocks the initial render.
Environment variables
Both halves read the same variables — set them once in .env:
NEXT_PUBLIC_SOPHONZ_COLLECTOR_URL=https://in.sophonz.ai
NEXT_PUBLIC_SOPHONZ_APP_NAME=my-app
NEXT_PUBLIC_SOPHONZ_APP_VERSION=1.0.0
NEXT_PUBLIC_SOPHONZ_APP_KEY=your-app-key
NEXT_PUBLIC_SOPHONZ_PROJECT=my-project
NEXT_PUBLIC_SOPHONZ_ENVIRONMENT=productionCAUTION — NEXT_PUBLIC_ reaches the browser
Next inlines only the NEXT_PUBLIC_ prefix into the client bundle, so every shared field uses it, appKey included. appKey is an ingestion key, not an access token, and is safe to ship to the browser.
Keep instrumented packages external
OpenTelemetry patches modules through Node's require hook. A bundled module is never required at runtime, so the hook misses it and the spans for the database and HTTP clients it wraps disappear.
// next.config.js
module.exports = {
serverExternalPackages: ['@sophonz/nextjs', '@sophonz/node-sdk'],
};NOTE — Older Next.js
serverExternalPackages moved out of experimental in Next.js 15. On earlier versions, use experimental.serverComponentsExternalPackages instead.
Next steps
- Configuration — the full
SophonzNextConfigreference and environment variables. - Node.js SDK and Web SDK — the two SDKs this package configures under the hood.