FastAPI
Learn about using Sentry with FastAPI.
The FastAPI integration adds support for the FastAPI Framework.
Install sentry-sdk from PyPI with the fastapi extra:
pip install --upgrade 'sentry-sdk[fastapi]'
If you have the fastapi package in your dependencies, the FastAPI integration will be enabled automatically when you initialize the Sentry SDK.
from fastapi import FastAPI
sentry_sdk.init(...) # same as above
app = FastAPI()
@app.get("/sentry-debug")
async def trigger_error():
division_by_zero = 1 / 0
When you point your browser to http://localhost:8000/sentry-debug a transaction will be created in the Performance section of sentry.io. Additionally, an error event will be sent to sentry.io and will be connected to the transaction.
It takes a couple of moments for the data to appear in sentry.io.
The following information about your FastAPI project will be available to you on Sentry.io:
- By default, all exceptions leading to an Internal Server Error are captured and reported. The HTTP status codes to report on are configurable via the
failed_request_status_codesoption. - Request data such as URL, HTTP method, headers, form data, and JSON payloads is attached to all issues.
- Sentry excludes raw bodies and multipart file uploads.
- Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set
send_default_piitoTrue.
The following parts of your FastAPI project are monitored:
- Middleware stack
- Middleware
sendandreceivecallbacks - Database queries
- Redis commands
The parameter enable_tracing needs to be set when initializing the Sentry SDK for performance measurements to be recorded.
By adding FastApiIntegration to your sentry_sdk.init() call explicitly, you can set options for FastApiIntegration to change its behavior. Because FastAPI is based on the Starlette framework, both integrations, StarletteIntegration and FastApiIntegration, must be instantiated.
from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.fastapi import FastApiIntegration
sentry_sdk.init(
# same as above
integrations=[
StarletteIntegration(
transaction_style="endpoint",
failed_request_status_codes=[403, range(500, 599)],
),
FastApiIntegration(
transaction_style="endpoint",
failed_request_status_codes=[403, range(500, 599)],
),
]
)
You can pass the following keyword arguments to StarletteIntegration() and FastApiIntegration():
transaction_style:This option lets you influence how the transactions are named in Sentry. For example:
Copiedimport sentry_sdk from sentry_sdk.integrations.starlette import StarletteIntegration from sentry_sdk.integrations.fastapi import FastApiIntegration sentry_sdk.init( # ... integrations=[ StarletteIntegration( transaction_style="endpoint" ), FastApiIntegration( transaction_style="endpoint" ), ], ) app = FastAPI() @app.get("/catalog/product/{product_id}") async def product_detail(product_id): return {...}In the above code, the transaction name will be:
"/catalog/product/{product_id}"if you settransaction_style="url""product_detail"if you settransaction_style="endpoint"
The default is
"url".failed_request_status_codes:A list of integers or containers (objects that allow membership checks via
in) of integers that will determine which status codes should be reported to Sentry.Examples of valid
failed_request_status_codes:[500]will only send events on HTTP 500.[400, range(500, 599)]will send events on HTTP 400 as well as the 500-599 range.[500, 503]will send events on HTTP 500 and 503.
The default is
[range(500, 599)].
- FastAPI: 0.79.0+
- Python: 3.7+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").

