Skip to main content

Error Explorer FastAPI SDK

Official FastAPI integration for Error Explorer error tracking.

Installation

pip install error-explorer-fastapi

Quick Start

from fastapi import FastAPI
from error_explorer import ErrorExplorer
from error_explorer_fastapi import ErrorExplorerMiddleware

# Initialize Error Explorer
ErrorExplorer.init({
    "token": "your-project-token",
    "environment": "production",
})

# Create FastAPI app with middleware
app = FastAPI()
app.add_middleware(ErrorExplorerMiddleware)

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

Using setup_error_explorer

For convenience, you can use the setup function:

from fastapi import FastAPI
from error_explorer import ErrorExplorer
from error_explorer_fastapi.middleware import setup_error_explorer

app = FastAPI()
ErrorExplorer.init({"token": "your-token"})
setup_error_explorer(app)

Configuration

The middleware accepts several configuration options:

app.add_middleware(
    ErrorExplorerMiddleware,
    # Capture user info from request.state.user
    capture_user=True,
    # Send PII (email, IP address)
    send_default_pii=False,
    # Capture 404 errors
    capture_404=False,
    # Capture 403 errors
    capture_403=False,
)

Features

Automatic Error Capture

All unhandled exceptions are automatically captured:

@app.get("/error")
async def trigger_error():
    raise ValueError("Something went wrong")
    # This is automatically captured

Request Breadcrumbs

HTTP requests are logged as breadcrumbs:

→ GET /api/users
← 200 OK

User Context

Set user context via request state (typically done by auth middleware):

@app.middleware("http")
async def auth_middleware(request: Request, call_next):
    user = await get_current_user(request)
    request.state.user = user  # User context captured automatically
    return await call_next(request)

Logging Handler

Use the logging handler to capture log messages:

import logging
from error_explorer_fastapi import ErrorExplorerHandler

# Add handler to root logger
handler = ErrorExplorerHandler(level=logging.WARNING)
logging.getLogger().addHandler(handler)

# Now WARNING and above are captured
logging.warning("This will be a breadcrumb")
logging.error("This will be captured as an event")

Event Filtering with before_send

Use the before_send callback to filter or modify events before they're sent:

def before_send(event):
    # Drop events from health check endpoints
    if event.get("request", {}).get("url", "").startswith("/health"):
        return None  # Drop the event

    # Add custom tags
    event["tags"] = event.get("tags", {})
    event["tags"]["service"] = "api"

    return event

ErrorExplorer.init({
    "token": "your-token",
    "before_send": before_send,
})

Manual Error Capture

from error_explorer import ErrorExplorer

@app.get("/process")
async def process():
    try:
        await risky_operation()
    except Exception as e:
        ErrorExplorer.get_client().capture_exception(e)
        raise HTTPException(status_code=500, detail="Error occurred")

Adding Context

from error_explorer import ErrorExplorer, Breadcrumb

@app.get("/checkout")
async def checkout():
    client = ErrorExplorer.get_client()

    # Add custom breadcrumb
    client.add_breadcrumb(Breadcrumb(
        message="User started checkout",
        category="checkout",
        data={"cart_items": 5}
    ))

    # Set additional context
    client.set_context("order", {
        "total": 99.99,
        "currency": "USD"
    })

    return await process_checkout()

Async Support

The middleware is fully async-compatible:

@app.get("/async-operation")
async def async_operation():
    result = await some_async_function()
    return {"result": result}

License

MIT

Release files for error-explorer-fastapi 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for error-explorer-fastapi 1.2.0
File Size Uploaded
error_explorer_fastapi-1.2.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for error-explorer-fastapi 1.2.0
File Interpreter ABI Platform
error_explorer_fastapi-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / error_explorer_fastapi-1.2.0.tar.gz

Download URL error_explorer_fastapi-1.2.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0b42380cb25ce07ab88c7f07fffa1d30b52c39a1cc967c38404475464eb987fe
BLAKE2b-256 checksum
How to use checksums
067d8ca6e2c5090395cc9243b6b0a9b9f648075e5ea42762d36af42030197b55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / error_explorer_fastapi-1.2.0-py3-none-any.whl

Download URL error_explorer_fastapi-1.2.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52bd2d7e3d8bd4be26f80279b019cf6160e1b1b6922dd09a31a80e099951f3a8
BLAKE2b-256 checksum
How to use checksums
506d456fc8b25b767d29e1162366b629413cfde787c98ba91bd4807e6de6793a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page