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_KEYandLESCOPR_API_KEY
For the
instrumentation.tshook, enableexperimental.instrumentationHook: trueinnext.config.json Next.js 13.x (enabled by default from 14.x).
Installation
Install via your package manager:
npm install lescoprConfiguration
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) orproduction
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,
});
}import { withLescopr } from 'lescopr/nextjs';
export default withLescopr(async function handler(req, res) {
res.json({ ok: true });
});LESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
LESCOPR_ENVIRONMENT=productionUsage
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:
'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 keyLESCOPR_API_KEY— API keyLESCOPR_ENVIRONMENT—developmentorproductionLESCOPR_DEBUG=true— Verbose output to.lescopr.log
npx lescopr init --sdk-key YOUR_SDK_KEY