Skip to main content
All recipes

Next.js

CodeApp RouterNode.js

Initialize CheckUpstream in the Node.js branch of instrumentation.ts to measure outbound fetch calls. This App Router example uses after() to flush telemetry after the response; it does not configure Edge runtime or browser monitoring.

1. Install

Bash
npm install @checkupstream/sdk

2. Register the instrumentation hook

Create instrumentation.ts beside your app directory, or inside src if you use it. Next.js calls this hook in each runtime, so import the Node.js SDK conditionally. The helper reads CHECKUPSTREAM_SDK_KEY from your server environment. See Next.js instrumentation.

instrumentation.ts
export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const { register: registerCheckUpstream } = await import(
      "@checkupstream/sdk/integrations/next"
    );
    registerCheckUpstream();
  }
}

Explicit configuration

Pass configuration explicitly (optional)

Use initCheckUpstream instead and pass the same options the Node.js SDK accepts.

instrumentation.ts
export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const { initCheckUpstream } = await import(
      "@checkupstream/sdk/integrations/next"
    );
    const sdkKey = process.env.CHECKUPSTREAM_SDK_KEY;
    if (!sdkKey) throw new Error("Set CHECKUPSTREAM_SDK_KEY before starting the app");

    initCheckUpstream({
      sdkKey,
      environment: process.env.VERCEL_ENV ?? "development",
      services: ["api.github.com", "api.stripe.com"],
    });
  }
}

3. Flush before the function shuts down

Serverless runtimes can freeze a request as soon as the response ships. after() runs once the response is sent, so the SDK has time to drain its buffer.

app/api/users/route.ts
import { after } from "next/server";
import { flushTelemetry } from "@checkupstream/sdk/integrations/next";

export const runtime = "nodejs";

export async function GET() {
  const response = await fetch("https://api.github.com/zen");
  const data = await response.text();
  after(flushTelemetry);
  return Response.json(data);
}

Continue your setup

Check runtime availability and credentials before deploying an integration.