Connect Sanic to Bugsink

When a Sanic request fails, Bugsink shows you the exception, the application frame that raised it, and the HTTP request that reached the handler.

This guide covers the initial setup and Sanic’s automatic capture of unhandled request-handler exceptions. You’ll connect an existing application, then make one request fail to confirm that it reaches Bugsink.

To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.

Install the SDK

Install the open-source Sentry SDK with its Sanic integration in the environment your application uses. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project.

python -m pip install --upgrade "sentry-sdk[sanic]"

Configure Sanic

Import the SDK and integrations where you create the application:

import sentry_sdk
from sanic import Sanic
from sanic.response import json
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.sanic import SanicIntegration

Initialize the SDK before Sanic starts. The Sanic integration adds request context, while the asyncio integration also captures failures in background tasks. Reading the DSN from the environment keeps it out of source control:

sentry_sdk.init(
    dsn=os.environ["SENTRY_DSN"],
    integrations=[AsyncioIntegration(), SanicIntegration()],
)

Do not move sentry_sdk.init() into a before_server_start listener. Sanic’s integration hook runs earlier; initializing later can omit request and route context from the event.

Set SENTRY_DSN in the environment where the application runs:

export SENTRY_DSN=https://key@bugsink.example/1

Trigger an unhandled exception

If a Sanic handler raises an exception and does not catch it, the integration reports it automatically. There is no SDK call inside the handler. To verify the setup, this checkout handler raises an exception for an unknown product:

@app.get("/checkout/<sku:str>")
async def checkout(request, sku):
    try:
        product = inventory[sku]
    except KeyError:
        raise ProductNotFound(f"Product {sku} is not in inventory") from None
    return json({"reserved": product["name"]})

Start the application as usual, then request a product that does not exist:

python app.py
:::bash
curl http://127.0.0.1:8080/checkout/sanic-missing

Sanic returns an HTTP 500 response and the integration reports the exception.

See the error in Bugsink

Open your Bugsink project and select the new ProductNotFound issue. Its event points to the failing line in the handler and includes the GET request.

Bugsink preview

That’s it: unhandled Sanic request-handler exceptions will now be reported to Bugsink.

The integration runs around Sanic’s request handling, so your handlers stay ordinary application code. An unhandled exception includes its Python stack, the application source frame, and the HTTP request that reached the handler.

If your application catches an exception itself, report it explicitly with capture_exception().