# Next.js

> Start Pulse from instrumentation.ts in a Next.js app. It reports the heartbeat, the clients you pass it, cron routes, and server logs.

- Section: Packages
- Availability: From the Free plan, logs on Pro and Team. Node 18.17 or later, Next.js 14 or later.
- URL: https://pulse-app.si/en/docs/packages/next

## Install

At the project root, with the key the Pulse app shows you:

**zsh**

```bash
npx @pulse-si/init pk_live_…
```

The command installs `@pulse-si/next`, writes the key to `.env.local` and creates `instrumentation.ts`, in `src/` if your project uses it. If the file already exists, it prints the lines to add. See [what it touches](https://pulse-app.si/en/docs/packages/init#what-it-touches).

### Show Pulse your clients

The command doesn’t know your clients. Without `watch`, only the heartbeat, versions, cron routes and logs are sent. Pass them to `startPulse`:

**instrumentation.ts**

```ts
export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    const { startPulse } = await import('@pulse-si/next');
    const { prisma } = await import('./lib/prisma');
    startPulse({ watch: [prisma] });
  }
}
```

Next.js 14 also needs `experimental.instrumentationHook: true` in `next.config`.

## What `watch` recognizes

| Client | What Pulse does | Name shown |
| --- | --- | --- |
| Prisma, TypeORM | `SELECT 1`, `ping` for Prisma MongoDB | PostgreSQL, MySQL… |
| Mongoose | `ping` | MongoDB |
| ioredis, node-redis | `PING` | Redis |
| BullMQ queue | Reads the number of waiting jobs | `File emails`… |

Objects are recognized by their shape and checked again every 60 seconds, because a client held in a watched object may only be created on the first request. Each check has 5 seconds to respond and only reads.

## Cron routes

A route called by Vercel Cron, an external scheduler or a `curl` on the server reports its failures with `trackCron`:

**app/api/cron/route.ts**

```ts
import { trackCron } from '@pulse-si/next';

export async function GET() {
  await trackCron('renew-subscriptions', () => renew());
  return Response.json({ ok: true });
}
```

The error is cleaned, recorded in Pulse, then thrown again, so your route responds as it did before.

## Where it works

`@pulse-si/next` runs in your server’s Node process. It’s made for `next start` on a VPS, in Docker, or at a host that keeps the process alive.

- Serverless platforms (Vercel, Netlify): an instance can sleep between requests. Monitor the app [by its address](https://pulse-app.si/en/docs/apps/url) instead, and keep `trackCron` for your cron routes.
- Edge runtime: ignored, with no error.
- In development: Next.js reloads `instrumentation.ts` on every change, but only one reporter runs per process.

## Choose what is sent

The heartbeat is always sent. The rest is set by variables, in `.env.local`, or in `startPulse`, which takes priority over them:

**instrumentation.ts**

```ts
startPulse({
  watch: [prisma, redis],
  send: ['checks', 'logs'],
  logLevel: 'WARN',
  probes: [httpProbe('Route /health', 'http://localhost:3000/api/health')],
});
```

> **60 characters for a probe name**
>
> The Node package doesn’t shorten the name of a probe you add. Past 60 characters, the API refuses the whole report and your log shows `Pulse unreachable: HTTP 400`.

## Logs

On Pro and Team, the package copies what the server writes (`console`, pino, Next.js messages), without changing anything in your terminal. Levels are recognized, including Next.js’s warning and error symbols. Colors are removed and secrets masked before anything is sent. Batches go out every 10 seconds, and every second when a device is viewing the app. Logs are kept for 3 days on Pro, 7 days on Team.

## If nothing arrives

| Symptom | What to do |
| --- | --- |
| `PULSE_KEY is missing` | The variable isn’t loaded in this process: check `.env.local` or your production environment. |
| `Pulse key refused` | The key was replaced in the app: copy the new one, then restart. |
| `Pulse unreachable` | Network or firewall: the app keeps running and sending resumes automatically. |
| No message, no report | Next.js 14 without `instrumentationHook`, or `instrumentation.ts` in the wrong place: it goes in `src/` if the project uses it. |
| No database under **What the package sees** | Pass your clients to `watch`. |

- [All variables](https://pulse-app.si/en/docs/packages/variables): Every setting and its default.
- [Exactly what is sent](https://pulse-app.si/en/docs/packages/payload): The precise format of reports and logs.
- [Live logs](https://pulse-app.si/en/docs/apps/logs): Read and filter them in Pulse.
