Skip to main content
This guide walks you through connecting a Next.js application to BugByBug using OpenTelemetry. The integration captures browser errors and sends them to BugByBug through a Next.js API route. Your API key stays on the server, while the browser communicates only with your application’s /api/traces endpoint. This approach uses the Next.js App Router and keeps authentication out of the client-side code. The request flow looks like this:
You’ll create a server-side route to forward telemetry and a client component to capture browser errors.

1. Get your BugByBug API key

First, create a project in BugByBug and retrieve its API key. You’ll use this key to authenticate requests sent from your Next.js application to BugByBug. Keep the key private. Unlike a browser-based integration that exposes its key in the frontend bundle, this setup stores the key in a server-side environment variable.

2. Install the OpenTelemetry packages

Install the packages needed to initialize browser tracing and export traces using OTLP/HTTP. Run the following command from your Next.js project directory:
  • @opentelemetry/api: this is the shared opentelemetry interface.
  • @opentelemetry/sdk-trace: this creates spans and passes them to the exporter.
  • @opentelemetry/exporter-trace-otlp-http: this sends spans over HTTP in the format BugByBug needs to analyze the bugs.
  • @opentelemetry/sdk-node: this is the official OpenTelemetry SDK for Node.js that collects the trace data Next.js creates for every request.

3. Configure environment variables

Next, configure the BugByBug endpoint and API key in your project’s .env.local file.
Replace the placeholder with your actual BugByBug API key.
Do not prefix these variables with NEXT_PUBLIC_. Next.js exposes variables with that prefix to browser code. Keeping these variables server-side ensures that your private API key is not included in the frontend bundle.
If you’re using a staging environment, configure the corresponding staging endpoint instead. Add the same two variables to your hosting provider’s environment settings when deploying the application.

4. Create a config helper

Next, you would need to create a function that lets BugByBug read your project settings src/app/utils/otel.ts
The files needed for proxy route, server tracing and server error reporting each have their own vars and could easily read through each vars hence the helper to keep them in sync.

5. Create the API proxy route

The browser tracing component will send telemetry to /api/traces. You need a Next.js API route to receive that request and forward it to BugByBug. With the App Router, create the following file: src/app/api/traces/route.ts Add the following code:
This route handles incoming trace requests from the browser and forwards them to BugByBug. Here’s what happens when a request reaches the endpoint:
  1. If the endpoint or key is missing, the route answers 500 and logs which setting is missing. It never calls BugByBug with a broken URL.
  2. Otherwise it forwards the body unchanged, keeps the original content type and adds x-api-key. Connect a Next.js application to BugByBug Page 4
  3. It returns BugByBug’s status code to the browser, with an empty body, so nothing about BugByBug or your key leaks back.
  4. If BugByBug can’t be reached, it answers 502 instead of crashing.
Check your authentication middleware If your application uses authentication middleware in middleware.ts or proxy.ts, make sure /api/traces is not redirected to your login page. This endpoint needs to accept telemetry requests from the browser, including requests made on public pages. Redirecting it to login prevents those requests from reaching BugByBug.

6. Create the browser tracing component

With the API route in place, you can configure the browser to capture errors and send them to your Next.js endpoint. Create the following file: src/components/BrowserTracing.tsx Add the tracing implementation:
The component uses React’s useEffect hook to initialize OpenTelemetry when the component mounts. It registers two browser event listeners:
  • error: Captures uncaught JavaScript errors.
  • unhandledrejection: Captures rejected promises that do not have a rejection handler.
Why must this be a Client Component? The code above uses window and useEffect, which only exist in the browser. App Router components are Server Components by default, and putting this code directly in layout.tsx fails with ReferenceError: window is not defined. The use client directive makes it run in the right place.

7. Mount the tracing component in the root layout

Creating the tracing component isn’t enough; it also needs to be included in your application. Import BrowserTracing into your root layout and render it inside the body element. src/app/layout.tsx Update the layout:
Placing BrowserTracing in the root layout makes it available across your application. You don’t need to add the component to individual pages. The tracing component returns null, so it doesn’t render any visible UI. Its purpose is to initialize error monitoring and register the browser event listeners.

8. Create a server-side tracing and error reporting

Next, you would need to create a server-side tracing and error reporting route. You can do this through src/instrumentation.ts because next.js automatically loads this file when a server starts. Two exports in that file cover the server side, plus one helper file for the Node SDK.

First, trace every request

Start with register(). It loads the tracing setup, but only on the Node.js runtime: src/instrumentation.ts
Then create the file that starts the SDK. src/instrumentation.node.ts

What does the code above do?

Next, report uncaught server errors.

Add this to src/instrumentation.ts, below register(). Next.js calls onRequestError whenever a Server Component, Route Handler, Server Action or your middleware throws an error that nothing caught:

9. Verify the connection

After completing the setup, run your Next.js application and generate a test error to confirm that telemetry reaches BugByBug.

Browser Errors

  1. Restart your development server. Environment variable changes require a restart.
  2. Open your application in the browser.
  3. Open the browser developer console and run:
  4. Open the browser’s Network tab and look for a POST request to /api/traces.
  5. Confirm that the request returns a 200 response and that the test error appears in your BugByBug dashboard.
Also check that the browser request does not contain your API key or the BugByBug ingestion URL. The browser should communicate only with your own /api/traces endpoint.

Server errors and request traces

You could add a temporary route that always fails:
This code would live within your src/app/api/boom/route.ts file. Once added, go ahead and test it:
  • Open http://localhost:3000/api/boom. It returns 500, and the terminal shows Error: server test.
  • Wait about 5 seconds. Server traces are sent in batches, not one by one.
  • In BugByBug, filter by the service web-server.
You should see spans like:
  • server-error: this will be an onRequestError with a message “server test” with its stack trace.
  • GET /api/boom: this will be the request trace, marked as an error.
  • executing api route (app) /api/boom: this will be the Route Handler that threw, with the exception attached.
  • middleware GET: this will be your middleware or proxy.ts, if you have one.

Troubleshooting

If the test error doesn’t appear in BugByBug, check the request in your browser’s Network tab and review the following common issues.

Security considerations

The proxy route keeps your BugByBug API key on the server, but the /api/traces endpoint is publicly reachable by nature. Keep the following precautions in place:
  • Keep your API key server-side. Store it in .env.local and your hosting provider’s server-side environment settings. Never expose it through a NEXT_PUBLIC_ variable.
  • Rotate exposed keys. If a key has ever been committed to source control or included in a client bundle, rotate it.
  • Protect the proxy endpoint. Consider checking the request origin, limiting request body size, and applying rate limiting to reduce unwanted traffic.
Once you’ve verified that errors are arriving in BugByBug, your Next.js application is connected.

What’s next?