Connect FastAPI to Bugsink
When a FastAPI endpoint fails, Bugsink shows you the exception, the application frame that raised it, and the ASGI request that reached the endpoint.
This guide covers the initial setup and FastAPI’s automatic capture of unhandled endpoint 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 FastAPI support 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[fastapi]"python -m pip install --upgrade "sentry-sdk[fastapi]"
Configure FastAPI
Import the SDK and its FastAPI and Starlette integrations where you create the application. FastAPI is built on Starlette, so both take part in request error capture:
import os import sentry_sdk from fastapi import FastAPI from sentry_sdk.integrations.fastapi import FastApiIntegration from sentry_sdk.integrations.starlette import StarletteIntegrationimport os import sentry_sdk from fastapi import FastAPI from sentry_sdk.integrations.fastapi import FastApiIntegration from sentry_sdk.integrations.starlette import StarletteIntegration
Initialize the SDK before creating the FastAPI application. Reading the DSN
from the environment keeps it out of source control:
sentry_sdk.init( dsn=os.environ["SENTRY_DSN"], integrations=[StarletteIntegration(), FastApiIntegration()], )sentry_sdk.init( dsn=os.environ["SENTRY_DSN"], integrations=[StarletteIntegration(), FastApiIntegration()], )
Set SENTRY_DSN in the environment where FastAPI runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Trigger an unhandled exception
If a FastAPI endpoint raises an exception and does not catch it, the integrations report it automatically. There is no SDK call inside the endpoint. To verify the setup, this checkout endpoint raises an exception when a product is missing:
@app.get("/checkout/{sku}") async def checkout(sku: str): if sku not in inventory: raise ProductNotFound(f"Product {sku} is not in inventory") return {"reserved": sku}@app.get("/checkout/{sku}") async def checkout(sku: str): if sku not in inventory: raise ProductNotFound(f"Product {sku} is not in inventory") return {"reserved": sku}
Start the application as usual, then request a product that does not exist through that URL:
uvicorn app:app :::bash curl http://127.0.0.1:8000/checkout/fastapi-missinguvicorn app:app :::bash curl http://127.0.0.1:8000/checkout/fastapi-missing
FastAPI 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 endpoint and includes the GET request.
ProductNotFound
That’s it: unhandled FastAPI endpoint exceptions will now be reported to Bugsink.
The integrations run around FastAPI’s request handling, so your endpoints stay ordinary application code. An unhandled exception includes its Python stack, the application source frame, and the ASGI request that reached the endpoint.
If your application catches an exception itself, report it explicitly with
capture_exception().
