Next.js — Lescopr SDK

Integrate the Lescopr SDK into your Next.js application using the instrumentation API for automatic error capture and real-time monitoring.

Introduction

The Lescopr SDK integrates with Next.js through the official instrumentation.ts hook (App Router, Next.js 13.4+) and a withLescopr wrapper for Pages Router API routes.

All server-side errors, unhandled rejections and API route exceptions are captured automatically and streamed to your Lescopr dashboard.

The package is available on npm and is compatible with Next.js 13+.

Requirements

  • Node.js >= 16.0.0
  • Next.js >= 13.0
  • A valid LESCOPR_SDK_KEY and LESCOPR_API_KEY

For the instrumentation.ts hook, enable experimental.instrumentationHook: true in next.config.js on Next.js 13.x (enabled by default from 14.x).

Installation

Install via your package manager:

Terminal
bash
npm install lescopr

Configuration

App Router — instrumentation.ts:

The register() function is called once at server startup — the ideal place to initialise the SDK.

Pages Router — API route wrapper:

Wrap individual API routes with withLescopr to capture handler errors.

Add your keys to .env.local:

Available options:

  • sdkKey — Your Lescopr SDK key (required)
  • apiKey — Your Lescopr API key (required)
  • environment — development (default) or production
instrumentation.ts
typescript
import lescopr from 'lescopr';

export function register() {
  lescopr.init({
    sdkKey:      process.env.LESCOPR_SDK_KEY,
    apiKey:      process.env.LESCOPR_API_KEY,
    environment: process.env.NODE_ENV,
  });
}
pages/api/orders.ts
typescript
import { withLescopr } from 'lescopr/nextjs';

export default withLescopr(async function handler(req, res) {
  res.json({ ok: true });
});
.env.local
dotenv
LESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
LESCOPR_ENVIRONMENT=production

Usage

Once initialised via instrumentation.ts, all uncaught server-side errors are captured automatically.

You can also log and capture exceptions manually from any server component, route handler or API route:

app/actions.ts
typescript
'use server';
import lescopr from 'lescopr';

export async function placeOrder(data: OrderData) {
  try {
    const result = await processOrder(data);
    lescopr.log('info', 'Order placed', { orderId: result.id });
    return result;
  } catch (err) {
    lescopr.captureException(err as Error);
    throw err;
  }
}

Advanced

CLI initialisation

Run the CLI at the project root to detect the framework and write .lescopr.json:

Environment variables

  • LESCOPR_SDK_KEY — SDK key
  • LESCOPR_API_KEY — API key
  • LESCOPR_ENVIRONMENT — development or production
  • LESCOPR_DEBUG=true — Verbose output to .lescopr.log
Terminal
bash
npx lescopr init --sdk-key YOUR_SDK_KEY