/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:
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.
4. Create a config helper
Next, you would need to create a function that lets BugByBug read your project settingssrc/app/utils/otel.ts
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:
- 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.
- 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
- It returns BugByBug’s status code to the browser, with an empty body, so nothing about BugByBug or your key leaks back.
- If BugByBug can’t be reached, it answers 502 instead of crashing.
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:
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.
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. ImportBrowserTracing into your root layout and render it inside the body element.
src/app/layout.tsx
Update the layout:
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
src/instrumentation.node.ts
What does the code above do?
Next, report uncaught server errors.
Add this tosrc/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
- Restart your development server. Environment variable changes require a restart.
- Open your application in the browser.
-
Open the browser developer console and run:
-
Open the browser’s Network tab and look for a
POSTrequest to/api/traces. -
Confirm that the request returns a
200response and that the test error appears in your BugByBug dashboard.
/api/traces endpoint.
Server errors and request traces
You could add a temporary route that always fails: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.
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.localand your hosting provider’s server-side environment settings. Never expose it through aNEXT_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.