> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bugbybug.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect a Node.js application to BugByBug

> Learn how to connect BugByBug to a NodeJS project

Connect your Node.js application to BugByBug using OpenTelemetry. This guide uses OpenTelemetry's Node.js auto-instrumentation to collect traces from supported libraries and frameworks and export them to BugByBug.

The setup uses OTLP/HTTP with JSON encoding and your BugByBug project API key.

## What you'll need

Before you begin, make sure you have:

* A Node.js application.
* Node.js and npm installed.
* A BugByBug account and project.
* Your BugByBug project API key.

> [!NOTE]
>
> Instrumentation coverage depends on the libraries and versions used by your web app or software.

## 1. Get your BugByBug API key

1. Sign in to your [BugByBug dashboard](https://dashboard.bugbybug.com/signup).
2. Create a project or open an existing one.
3. Copy the project's API key.

> [!TIP]
>
> Keep your API key private. Store it in an environment variable rather than hardcoding it into your application.

## 2. Install OpenTelemetry packages

Install the OpenTelemetry Node.js SDK and auto-instrumentation package:

```bash theme={null}
npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node
```

This package lets OpenTelemetry talk to your project if they are supported Node.js libraries and frameworks.

## 3. Configure your environment variables

Add the following variables to your application's `.env` file:

```env theme={null}
OTEL_EXPORTER_OTLP_ENDPOINT=https://nexus.bugbybug.com
OTEL_EXPORTER_OTLP_PROTOCOL=http/json
OTEL_EXPORTER_OTLP_HEADERS=x-api-key=YOUR_BUGBYBUG_API_KEY
OTEL_SERVICE_NAME=your-service-name
OTEL_TRACES_EXPORTER=otlp
```

Replace the placeholders:

* `YOUR_BUGBYBUG_API_KEY`: Your actual BugByBug API key.
* `your-service-name`: A descriptive name for your application or service.

**What these variables do:**

| Variable | Purpose |
| - | - |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | Sets the BugByBug telemetry ingestion endpoint. |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | Configures OTLP over HTTP with JSON encoding. |
| `OTEL_EXPORTER_OTLP_HEADERS` | Supplies your BugByBug API key for authentication. |
| `OTEL_SERVICE_NAME` | Identifies your application in the telemetry data. |
| `OTEL_TRACES_EXPORTER` | Enables the OTLP trace exporter. |

Keep these values in your `.env` file. Never expose your project API key in frontend code.

## 4. Enable auto-instrumentation

OpenTelemetry must initialize before your application loads the libraries you want to instrument.

You can do this by starting your Node.js application with the OpenTelemetry registration module.

**For CommonJS applications:**

```bash theme={null}
node --require @opentelemetry/auto-instrumentations-node/register app.js
```

**For ESM applications:**

```bash theme={null}
node --import @opentelemetry/auto-instrumentations-node/register app.js
```

Replace `app.js` above with your actual application's entry point.

### Add it to your npm scripts

Instead of typing the command every time, add the script below to your `package.json`:

```json theme={null}
{
  "scripts": {
    "start": "node --require @opentelemetry/auto-instrumentations-node/register app.js"
  }
}
```

You can then start your application with:

```bash theme={null}
npm start
```

If your project uses a different module system or build setup,
check the [OpenTelemetry JavaScript documentation](https://opentelemetry.io/docs/languages/js/instrumentation/) for the correct configuration.

## 5. Run your application

Start your application using the configured command.

OpenTelemetry will initialize before your application code and instrument supported libraries.

As your application handles requests and performs supported operations, OpenTelemetry generates trace data and exports it to BugByBug.

The data collected depends on the instrumentation available for your application's dependencies and how they are configured.

## 6. Verify your connection

After starting your application:

1. Send a request to an endpoint in your application.
2. Check your application logs for OpenTelemetry export or authentication errors.
3. Open your BugByBug project dashboard.
4. Check whether telemetry from the configured service is appearing.

If telemetry does not appear, review the troubleshooting section below.

## Troubleshooting

| Problem | What to check |
| - | - |
| No telemetry appears | Confirm that the instrumentation module loads before your application. |
| Authentication errors | Check that `OTEL_EXPORTER_OTLP_HEADERS` contains the correct project API key. |
| Export requests fail | Confirm the BugByBug endpoint and network connectivity. |
| Incorrect service name | Check the `OTEL_SERVICE_NAME` environment variable. |
| Framework activity is missing | Confirm that the relevant library has supported OpenTelemetry instrumentation and that it is enabled. |
| Environment variables are not applied | Confirm that they are available to the Node.js process when it starts. |

## What's next?

Your Node.js application is now configured to export OpenTelemetry traces to BugByBug.

You may also check out our [Python guide](https://docs.bugbybug.com/python-guide.md) to connect your Django, Flask or FastAPI app.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.