Integrate Sentry to FastAPI

Phil Girard
1 min readApr 19, 2020

I recently came across the problem of integrating Sentry to FastAPI. I was not able to make the sentry ASGI integration (SentryASGIMiddleware). I will show in this post how I solved my problem with a custom integration.

The solution is very simple. It uses a barebone FastAPI middleware.

  1. All the exceptions that can happen during a request are catch in a general handler
  2. A sentry context is created with additional information about the error. You can customize this by following the documentation here.
  3. The error and the context are sent to sentry
  4. The error is re-thrown, so that FastAPI can display their internal 500 error.

Let my know if this was useful in the comments or with a clap . If you want to see how to integrate logging with FastAPI you can read about it here.

Buy me a coffee

If you find this article useful consider buying me a coffee :) This helps me publish more useful content!

--

--