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

# Python

> Send errors and logs from Python and Django applications to Squasher.

## Recommended: OpenTelemetry SDK

The fastest way to instrument Python apps. Auto-instruments popular frameworks (Django, Flask, FastAPI, SQLAlchemy) with zero code changes.

### 1. Install

```bash theme={null}
pip install opentelemetry-sdk \
  opentelemetry-exporter-otlp-proto-http \
  opentelemetry-instrumentation
```

### 2. Configure

Set environment variables:

```bash theme={null}
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.squasher.ai
export OTEL_EXPORTER_OTLP_HEADERS="x-squasher-key=sq_pk_your_api_key"
export OTEL_SERVICE_NAME=your-service
```

### 3. Run

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

Squasher automatically extracts errors from your traces and logs. All exception spans and error-level log records are captured.

## Django

Install Django-specific auto-instrumentation:

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

Then run your app with the OTel instrumentor:

```bash theme={null}
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.squasher.ai
export OTEL_EXPORTER_OTLP_HEADERS="x-squasher-key=sq_pk_your_api_key"
export OTEL_SERVICE_NAME=my-django-app

opentelemetry-instrument python manage.py runserver
```

## Flask

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

```bash theme={null}
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.squasher.ai
export OTEL_EXPORTER_OTLP_HEADERS="x-squasher-key=sq_pk_your_api_key"
export OTEL_SERVICE_NAME=my-flask-app

opentelemetry-instrument python app.py
```

## FastAPI

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

```bash theme={null}
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.squasher.ai
export OTEL_EXPORTER_OTLP_HEADERS="x-squasher-key=sq_pk_your_api_key"
export OTEL_SERVICE_NAME=my-fastapi-app

opentelemetry-instrument uvicorn main:app
```

## Alternative: Direct HTTP API

If you prefer not to use OpenTelemetry, send events directly via our HTTP API:

```python theme={null}
import logging
import requests
import traceback

SQUASHER_URL = "https://ingest.squasher.ai/v1/ingest/YOUR_PROJECT_ID"
SQUASHER_KEY = "sq_pk_your_api_key"

def report_error(exc, **extra_tags):
    """Report an exception to Squasher."""
    requests.post(
        SQUASHER_URL,
        headers={
            "Content-Type": "application/json",
            "x-squasher-key": SQUASHER_KEY,
        },
        json={
            "message": str(exc),
            "type": type(exc).__name__,
            "level": "error",
            "stack": traceback.format_exc(),
            "environment": "production",
            "tags": extra_tags,
        },
        timeout=5,
    )

# Usage
try:
    result = do_something_risky()
except Exception as e:
    report_error(e, endpoint="/api/process", user_id="user_42")
    raise
```

### Structured Logging via HTTP

```python theme={null}
import requests
import json
from datetime import datetime, timezone

def squasher_log(level, message, **tags):
    """Send a structured log to Squasher."""
    requests.post(
        "https://ingest.squasher.ai/v1/ingest/YOUR_PROJECT_ID",
        headers={
            "Content-Type": "application/json",
            "x-squasher-key": "sq_pk_your_api_key",
        },
        json={
            "message": message,
            "level": level,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "tags": tags,
        },
        timeout=5,
    )

# Usage
squasher_log("info", "User signed up", user_id="u_123", plan="pro")
squasher_log("error", "Payment failed", user_id="u_123", amount=49.99)
```

## AWS Lambda

For Python Lambda functions, use environment variables with the OTel Lambda layer:

```bash theme={null}
# Add the OTel Lambda layer to your function, then set:
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.squasher.ai
OTEL_EXPORTER_OTLP_HEADERS=x-squasher-key=sq_pk_your_api_key
OTEL_SERVICE_NAME=my-lambda-function
```

Or use the direct HTTP API approach above — it works without any layers.

## First-party SDK

If your app already uses Python's `logging` module and you want a smaller integration, use the [Python SDK](/sdks/python).

## Agent handoff

```text theme={null}
For Python, prefer squasher-python when the app uses logging and OpenTelemetry when it already needs traces. Keep SQUASHER_API_KEY and SQUASHER_PROJECT_ID in env, verify with one logger.exception or error span, and avoid logging secrets.
```
