> ## 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 Python application to BugByBug

> Learn how to connect BugByBug to a Python project

Connect your Python application to BugByBug using OpenTelemetry (OTel) auto-instrumentation. Once configured, your application can export trace data to BugByBug for continuous monitoring and AI-assisted error investigation.

This guide uses the [OpenTelemetry Python agent](https://opentelemetry.io/docs/zero-code/python/configuration/), so you can start collecting telemetry without adding instrumentation code throughout your application.

## What you'll need

Before you begin, make sure you have:

* A Python application,
* Python 3.10+ and pip installed,
* A BugByBug account and project, and
* Your BugByBug project API key.

This guide covers frameworks like FastAPI, Flask, Django, Celery, and standalone Python applications. Instrumentation coverage depends on the libraries and versions used by your application.

## 1. Get your API key

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

> [!TIP]
>
> Keep this key private. Do not commit it or expose it in frontend code.

## 2. Install the OpenTelemetry packages

Install the OpenTelemetry distribution and instrumentation tools:

```bash theme={null}
pip install opentelemetry-distro opentelemetry-instrumentation
```

Next, install the instrumentation packages for the libraries already used by your application:

```bash theme={null}
opentelemetry-bootstrap -a install
```

The bootstrap command checks your installed Python packages and installs available OpenTelemetry instrumentation packages for supported libraries.

For example, if your application uses Flask, Django, or requests, the command can install the corresponding instrumentation packages when available.

## 3. Configure BugByBug

Configure the OpenTelemetry exporter using environment variables.

Add the following to your `.env` file or application environment:

```env theme={null}
OTEL_SERVICE_NAME=your-service-name
OTEL_TRACES_EXPORTER=otlp
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://nexus.bugbybug.com/v1/traces
OTEL_EXPORTER_OTLP_TRACES_HEADERS=x-api-key=YOUR_BUGBYBUG_API_KEY
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/json
```

Replace:

* `your-service-name` with a recognizable name for your application or service.
* `YOUR_BUGBYBUG_API_KEY` with your actual BugByBug project API key.

**Important:** The trace endpoint and authentication header above are the BugByBug connection details. The Python exporter must support the configured OTLP/HTTP JSON format. Confirm the exporter package and protocol compatibility before using this configuration in production.

## 4. Start your application with OpenTelemetry

Use the `opentelemetry-instrument` command to launch your application.

### FastAPI / Uvicorn

```bash theme={null}
opentelemetry-instrument uvicorn main:app --host 0.0.0.0 --port 8000
```

### Flask

```bash theme={null}
opentelemetry-instrument flask run --host=0.0.0.0 --port=5000
```

### Django / Gunicorn

```bash theme={null}
opentelemetry-instrument gunicorn myproject.wsgi:application
```

Replace `myproject` with your Django project module.

### Celery worker

```bash theme={null}
opentelemetry-instrument celery -A tasks worker
```

Replace `tasks` with the module containing your Celery application.

### Standalone Python script

```bash theme={null}
opentelemetry-instrument python app.py
```

Replace `app.py` with your application's entry-point file.

The instrumentation agent runs before your application and instruments supported libraries at runtime. You do not need to add the command separately to every route or function.

## 5. Verify your connection

After starting your application:

1. Make a request to one of your application's endpoints or run a task that exercises an instrumented library.
2. Check your application logs for OpenTelemetry exporter errors.
3. Open your BugByBug dashboard.
4. Confirm that telemetry associated with your configured service appears.

If telemetry does not appear, check the troubleshooting section below before changing your application code.

## Troubleshooting

| Symptom | Possible cause or fix |
| - | - |
| `opentelemetry-instrument` command not found | Confirm that the OpenTelemetry packages are installed in the active virtual environment. |
| No telemetry appears in BugByBug | Check the API key, endpoint, service name, and exporter configuration. |
| HTTP 401 or 403 | The API key may be missing, incorrect, or invalid. |
| Exporter reports an unsupported protocol | Confirm that the installed Python exporter supports the protocol configured for BugByBug. |
| Framework requests are missing | Check whether an instrumentation package is available for the framework and whether it was installed by the bootstrap command. |
| Some libraries are not instrumented | Not every Python library has automatic instrumentation. Check the [OpenTelemetry instrumentation registry](https://opentelemetry.io/ecosystem/registry/). |
| Telemetry appears under an unexpected service | Check the value of `OTEL_SERVICE_NAME`. |

## What's next?

Once your Python application is connected, BugByBug can begin receiving its configured telemetry for monitoring and investigation.

* [Learn how BugByBug works](https://docs.bugbybug.com/how-it-works.md)
* [Explore how OpenTelemetry and BugByBug work together](https://docs.bugbybug.com/opentelemetry.md)


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