# Next.js

> One setup of @sypnia/telemetry/next covers the browser and the server of a Next.js app, in the App Router and the Pages Router, and uploads source maps when you build.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://telemetry.docs.sypnia.com/sdks/nextjs/

This page is for whoever runs a Next.js app. `@sypnia/telemetry/next` is the [browser SDK](https://telemetry.docs.sypnia.com/sdks/browser) and the [Node SDK](https://telemetry.docs.sypnia.com/sdks/node) set up together, plus what is specific to Next.js: navigation in the App Router, errors thrown on the server, and source maps. It is open source (MIT) and identical for the open-source engine and for Sypnia Cloud.

The server side is Sypnia's own tracer, as in the Node SDK: it does not depend on OpenTelemetry, and it sends its spans in OpenTelemetry's protocol (OTLP/HTTP, protobuf) to the engine's [OTLP endpoint](https://telemetry.docs.sypnia.com/api/otlp). The browser side sends its own compact events. One trace id joins the two.

**At the end you will have:** sessions, replays and page performance from the browser, every server request traced, server errors linked to the page that showed them, and stacks that point at your own files.

**You need:** Next.js 15.3 or newer.

## Install

**npm**

```bash
npm install @sypnia/telemetry
```

**pnpm**

```bash
pnpm add @sypnia/telemetry
```

**yarn**

```bash
yarn add @sypnia/telemetry
```

**bun**

```bash
bun add @sypnia/telemetry
```

## Set it up

Three small files. The configuration lives in the first one; the other two only switch Sypnia on, in the server and in the browser. Put the two instrumentation files next to `app/` or `pages/`: in the project root, or in `src/` if you use it.

### 1. Wrap your Next.js config

**Sypnia Cloud**

`next.config.ts`

```ts
import type { NextConfig } from 'next';
import { withSypnia } from '@sypnia/telemetry/next';

const nextConfig: NextConfig = {};

export default withSypnia(nextConfig, {
  project: process.env.SYPNIA_PROJECT_KEY,
  service: 'shop-web',
});
```

**Self-hosted**

`next.config.ts`

```ts
import type { NextConfig } from 'next';
import { withSypnia } from '@sypnia/telemetry/next';

const nextConfig: NextConfig = {};

export default withSypnia(nextConfig, {
  project: process.env.SYPNIA_PROJECT_KEY,
  endpoint: 'https://sypnia.example.com',
  service: 'shop-web',
});
```

`.env.local`

```bash
SYPNIA_PROJECT_KEY=spk_7d1f0c9e4b8a2f6d3c5e1a9b0f4d8c2e
```

`withSypnia` reads these values when Next.js builds and gives them to both sides of your app, so you write them once. The ingest key is public: it is meant to end up in the browser bundle.

### 2. Switch on the server

`instrumentation.ts`

```ts
export { register, onRequestError } from '@sypnia/telemetry/next/server';
```

### 3. Switch on the browser

`instrumentation-client.ts`

```ts
import { init } from '@sypnia/telemetry/next/client';

init();

export { onRouterTransitionStart } from '@sypnia/telemetry/next/client';
```

`init()` takes its values from `withSypnia`. It also accepts any [browser option](https://telemetry.docs.sypnia.com/sdks/browser#initoptions), for example `init({ maskAllText: true })`.

### 4. Run it and look

Run `next dev`, open a page and click a link. In the browser's Network tab, filter by `ingest`.

**You should see:**A `POST` to `…/v1/ingest/events` answered `200`, within 10 seconds. With `debug: true` in `withSypnia`, the terminal also prints each request the server handled as a tree of spans, and `exported 3 spans (200)` when they leave for `…/v1/otlp/v1/traces`.

## What you get

| Side                 | Covered                                                                                                                                           | Notes                                                                                                                                                                                                                                                                                                                       |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Browser              | Everything the [browser SDK](https://telemetry.docs.sypnia.com/sdks/browser#what-is-captured-without-any-code) captures.                          | Navigations of the App Router are reported by Next.js itself through `onRouterTransitionStart`, which announces each one to the browser SDK before the address changes; the Pages Router is followed through the History API. Both are page views. Routes are named as Next.js names them: see [Route names](#route-names). |
| Server, App Router   | Page and layout renders, Route Handlers, Server Actions.                                                                                          | One server span each, with `http.route` as Next.js names it (`/orders/[id]`).                                                                                                                                                                                                                                               |
| Server, Pages Router | Pages with `getServerSideProps`, API routes under `pages/api`.                                                                                    | The same.                                                                                                                                                                                                                                                                                                                   |
| Outgoing calls       | `fetch`, `http`, `pg`, Supabase and PostgREST, as in the [Node SDK](https://telemetry.docs.sypnia.com/sdks/node#what-is-traced-without-any-code). | Spans under the request. A failed database call keeps the database's code and full message, values included, unless you set `server: { captureDbErrors: false }`: see [Database error details](https://telemetry.docs.sypnia.com/sdks/node#database-error-details).                                                         |
| Server errors        | Every error Next.js catches on the server, through `onRequestError`.                                                                              | With the digest Next.js gives it.                                                                                                                                                                                                                                                                                           |
| Trace                | The browser's call and the server's request share one trace id.                                                                                   | Nothing to configure for calls to your own app.                                                                                                                                                                                                                                                                             |

Next.js' own paths are never traced: `/_next/static`, `/_next/image`, `/_next/webpack-hmr` and `/__nextjs_original-stack-frame`.

**Not covered:** code that runs on the Edge runtime (routes with `export const runtime = 'edge'`, and middleware). It is neither traced nor are its errors reported.

If your app already registers OpenTelemetry in `instrumentation.ts` (for example with `@vercel/otel`), do not add `register` from Sypnia on top: two tracers would trace every request twice. Keep your setup and add the [OpenTelemetry bridge](https://telemetry.docs.sypnia.com/sdks/otel-bridge#set-it-up-in-nextjs) to it; use `@sypnia/telemetry/next` for the browser side and for `onRequestError`. You then also get the spans Next.js itself emits for OpenTelemetry (renders, route resolution, `fetch` caching), at OpenTelemetry's cost.

## Route names

In a Next.js app the browser and the server name a route the same way, the way Next.js does: `/orders/[id]`, `/api/checkout`. When Next.js builds, `withSypnia` reads your app's route patterns from its folders, every `page` and `route` file under `app/` and `src/app/` and every file under `pages/`, and passes them to the browser side with its other values. Route groups, parallel slots, private folders and intercepting routes are not addresses, so they are not in the list. In the browser, `@sypnia/telemetry/next/client` matches each path against the list with Next.js' own order (plain segments first, then `[id]`, then `[...slug]`, then `[[...slug]]`); a path that matches no route gets the browser SDK's built-in rule. So a page, the API calls it makes and the server requests that answer them read the same in problems, traces and Performance. Outside Next.js the browser SDK names routes with its own rule (`/orders/:id`); see [`routeFor`](https://telemetry.docs.sypnia.com/sdks/browser#initoptions).

> **Note: Your route list is in the browser bundle**
>
> To name routes the Next.js way, the browser needs the patterns of your routes, so they are part of your app's browser bundle (not of `sdk.js`): every page and API route, such as `/orders/[id]` or `/api/admin/refunds`. Only patterns: no real ids, no code. Next.js already sends most of your page routes to the browser for its own routing; with Sypnia the API routes go too. Anyone who opens your app can read that list.

## On serverless

On Vercel and other platforms that run each request in a function, the SDK exports a request's spans after the response, with the platform's `waitUntil`, so the person is not kept waiting. A function that is frozen before the export finishes can lose that request's spans; it never loses the answer.

A cold start pays for whatever the instrumentation does before the first request. The server SDK installs no module loader and patches no file as it loads: measured on 2026-11-16, importing it takes about 20 ms and `init` about 2 ms, against a target of **under 50 ms** added to a cold start. For comparison, the OpenTelemetry Node SDK, measured on 2026-11-15, added 600–800 ms before a server could listen; the method and every number are on [Node, What it costs](https://telemetry.docs.sypnia.com/sdks/node#what-it-costs).

## On AWS Lambda, with OpenNext

OpenNext (`@opennextjs/aws`, which SST's `Nextjs` deploys to AWS Lambda) calls Next.js' handler without a Node HTTP server, so Node never announces a request and, without help, the server side traces nothing. `@sypnia/telemetry/next/opennext` is the help: it wraps the OpenNext wrapper your app uses, so each request is one traced request. The browser side needs nothing more.

Keep the setup above (`withSypnia`, `instrumentation.ts`, `instrumentation-client.ts`) and add three steps:

### 1. Install OpenNext in your app

```bash
npm install --save-dev @opennextjs/aws
```

Install the same version your build runs. OpenNext bundles `open-next.config.ts` with esbuild, and the wrapper below is imported as a value, so it must resolve from your app: running `npx @opennextjs/aws build` alone does not install it.

### 2. Wrap the wrapper

`open-next.config.ts`

```ts
import awsLambda from '@opennextjs/aws/overrides/wrappers/aws-lambda.js';
import { traceOpenNext } from '@sypnia/telemetry/next/opennext';

export default {
  default: { override: { wrapper: async () => traceOpenNext(awsLambda) } },
};
```

If your app uses OpenNext's streaming wrapper, wrap that one instead.

### 3. Build, deploy and look

Build and deploy as you do today, with `npx @opennextjs/aws build` or with SST. Then open a page that calls your API.

**You should see:**Within a few seconds the request appears in the trace of the browser's call, with its spans under it, and the cold request is traced too. OpenNext's warmer pings are not traced.

How it works: the Node SDK starts at the cold start, before Next.js loads `instrumentation.ts`, from the values `withSypnia` wrote into `.next/required-server-files.json`, so the first request is not missed. The `aws-lambda` wrapper offers no `waitUntil`, so the spans leave before the function returns, waiting at most `flushBudgetMs` (50 ms); with the streaming wrapper they leave with its `waitUntil`, after the answer. Tested with OpenNext 4.1.11.

Your own Lambda handlers, written without Next.js, are another case: see [Queues and functions](https://telemetry.docs.sypnia.com/sdks/queues-and-functions).

## Errors shown by an error boundary

When a Server Component throws, Next.js shows your `error.tsx` and hides the real message from the browser. It gives both sides the same `digest`. Report the error from the boundary with that digest, and Sypnia joins the page the person saw to the server error with its stack.

`app/error.tsx`

```tsx
'use client';

import { useEffect } from 'react';
import { reportError } from '@sypnia/telemetry/next/client';

export default function Error({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
  useEffect(() => {
    reportError(error, { source: 'boundary', digest: error.digest });
  }, [error]);

  return (
    <div>
      <p>Something went wrong.</p>
      <button onClick={reset}>Try again</button>
    </div>
  );
}
```

Do the same in `app/global-error.tsx` if you have one. In the Pages Router there is nothing to add: errors while rendering in the browser reach the window and are reported.

## Identifying people

In the browser, from a Client Component that knows the signed-in person:

`app/identify.tsx`

```tsx
'use client';

import { useEffect } from 'react';
import { identify } from '@sypnia/telemetry/next/client';

export function Identify({ id, name, email }: { id: string; name?: string; email?: string }) {
  useEffect(() => {
    identify({ person: { id, name, email } });
  }, [id, name, email]);
  return null;
}
```

Render it in your root layout when someone is signed in. On the server you do not need to identify requests that come from that browser. See [Identifying people](https://telemetry.docs.sypnia.com/sdks/identifying-people).

## `withSypnia(nextConfig, options)`

| Option        | Type                          | Default                                                                                                                                        | What it does                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `project`     | `string`                      | `process.env.SYPNIA_PROJECT_KEY`                                                                                                               | The ingest key, `spk_…`. Without it the build prints one warning and Sypnia stays off.                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `endpoint`    | `string`                      | `process.env.SYPNIA_ENDPOINT`, else `'https://telemetry.ingest.sypnia.com'`                                                                    | Where to send. A self-hosted engine's base URL.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `service`     | `string`, up to 80 characters | `process.env.SYPNIA_SERVICE`, else `'next'`                                                                                                    | The name of the server side in traces.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `environment` | `string`, 1 to 40 characters  | `process.env.SYPNIA_ENVIRONMENT`, else `process.env.VERCEL_ENV`, else `'development'` under `next dev` and `'production'` otherwise            | The environment, the same for both sides.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `release`     | `string`, up to 40 characters | `process.env.SYPNIA_RELEASE`, else the first 7 characters of `process.env.VERCEL_GIT_COMMIT_SHA`, else `git rev-parse --short HEAD`, else none | The release, the same for both sides and for the source maps.                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `debug`       | `boolean`                     | `false`                                                                                                                                        | Prints every event, in the browser console and in the server's output.                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `sourceMaps`  | `boolean`                     | `true`                                                                                                                                         | Uploads source maps when you build. See below.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `apiUrl`      | `string`                      | `process.env.SYPNIA_BASE_URL`, else `'https://telemetry.api.sypnia.com'`                                                                       | Where source maps are uploaded. A self-hosted engine's base URL.                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `server`      | Node SDK options              | `{}`                                                                                                                                           | Any option of the [Node SDK](https://telemetry.docs.sypnia.com/sdks/node#initoptions) except the ones above, for example `{ ignoreRoutes: ['/api/health'] }`, `{ allowRoutes: ['/dashboard', '/api'] }`, `{ anonymousRoutes: ['/', '/signup'] }` (0.3.0, see [Anonymous visitors](https://telemetry.docs.sypnia.com/sdks/anonymous-visitors)) or `{ captureDbErrors: false }`. These values are written into the build, so functions cannot cross: `routeFor` and `beforeSend` given here are dropped, with a warning when you build. |

Browser-only options go to `init()` in `instrumentation-client.ts`, among them the browser's own `allowRoutes`.

## Source maps at build

A production build with `SYPNIA_API_TOKEN` in its environment does four things, in order:

1. Turns on source maps for the browser bundles.
2. Registers the release, with the commit when git can tell it.
3. Uploads every source map for that release.
4. Deletes the `.map` files it turned on from the build output, so they are never published. Source maps your app already built on its own are left where they are.

`Build environment`

```bash
SYPNIA_PROJECT_KEY=spk_7d1f0c9e4b8a2f6d3c5e1a9b0f4d8c2e
SYPNIA_API_TOKEN=sps_4Kq9Zt2MxV7bN1cR5wY8pL3dF6hJ0sA2gE4uT9iO
```

The token is an API token with the `ci` scope, which can only register releases and upload source maps. It is a secret: set it in your CI or your host's build settings, never in the code and never in a variable that starts with `NEXT_PUBLIC_`.

**You should see:**At the end of `next build`: `Sypnia: release 4f2a91c registered, 312 source maps uploaded, 312 removed from the build output.`

The upload never fails your build. Without the token it prints `Sypnia: source maps not uploaded (SYPNIA_API_TOKEN is not set)` and goes on; without a release, `Sypnia: source maps not uploaded (no release: set SYPNIA_RELEASE)`; if the upload fails it says why and goes on. The count of uploaded maps includes maps the engine already had for that release. Under `next dev` nothing is uploaded.

Source maps of the server bundles are not read: a server error's stack shows the built file. Its route, its trace and the page that showed it are there either way.

## Next
