---
title: "Flask"
description: "Learn about using Sentry with Flask."
url: https://docs.sentry.io/platforms/python/integrations/flask/
---

# Flask | Sentry for Python

The Flask integration adds support for the [Flask Web Framework](https://flask.palletsprojects.com).

## [Install](https://docs.sentry.io/platforms/python/integrations/flask.md#install)

Install `sentry-sdk` from PyPI:

```bash
pip install sentry-sdk
```

## [Configure](https://docs.sentry.io/platforms/python/integrations/flask.md#configure)

If you have the `flask` package in your dependencies, the Flask integration will be enabled automatically when you initialize the Sentry SDK.

Error Monitoring\[ ]Tracing\[ ]Profiling\[ ]Logs

```python
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
    # ___PRODUCT_OPTION_START___ logs

    # Enable logs to be sent to Sentry
    enable_logs=True,
    # ___PRODUCT_OPTION_END___ logs
)
```

Our Python SDK will install the Flask integration for all of your apps. It hooks into Flask’s signals, not anything on the app object.

## [Verify](https://docs.sentry.io/platforms/python/integrations/flask.md#verify)

```python
from flask import Flask

sentry_sdk.init(...)  # same as above

app = Flask(__name__)

@app.route("/")
def hello_world():
    1 / 0  # raises an error
    return "<p>Hello, World!</p>"
```

When you point your browser to <http://localhost:5000/> a transaction in the Performance section of [sentry.io](https://sentry.io) will be created. Additionally, an error event will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction.

It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io).

## [Behavior](https://docs.sentry.io/platforms/python/integrations/flask.md#behavior)

##### uWSGI and Sentry SDK

If you're using uWSGI, note that it doesn't support threads by default. This might lead to unexpected behavior when using the Sentry SDK, from features not working properly to uWSGI workers crashing.

To enable threading support in uWSGI, make sure you have both `--enable-threads` and `--py-call-uwsgi-fork-hooks` on.

Note that automatic tracing on file-like responses when `offloading` is configured is disabled (see [here](https://github.com/getsentry/sentry-python/pull/5556) for why). If you wish to enable tracing on these types of responses, you will need to manually instrument them.

After initialization:

* If you use `flask-login` and set `send_default_pii=True` in your call to `init`, user data (current user id, email address, username) will be attached to the event.
* Request data will be attached to all events: **HTTP method, URL, headers, form data, JSON payloads**. Sentry excludes raw bodies and multipart file uploads.
* Logs emitted by `app.logger` or *any* logger will be recorded as breadcrumbs by the [Logging](https://docs.sentry.io/platforms/python/integrations/logging.md) integration (this integration is enabled by default).

## [Options](https://docs.sentry.io/platforms/python/integrations/flask.md#options)

If you add `FlaskIntegration` explicitly to your `sentry_sdk.init()` call you can set options for `FlaskIntegration` to change its behavior:

```python
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

sentry_sdk.init(
    # ...
    integrations = [
        FlaskIntegration(
            transaction_style="url",
            http_methods_to_capture=("GET",),
        ),
    ],
)
```

You can pass the following keyword arguments to `FlaskIntegration()`:

* `transaction_style`:

  Sets the format or style that transactions are named.



  ```python
  @app.route("/myurl/<foo>")
  def myendpoint():
      return "<p>Hello, World!</p>"
  ```

  In the above code, you would set the transaction to:

  * `/myurl/<foo>` if you set `transaction_style="url"`.
  * `myendpoint` if you set `transaction_style="endpoint"`.

  The default is `"endpoint"`.

* `http_methods_to_capture`:

  A tuple containing all the HTTP methods that should create a transaction in Sentry.

  The default is `("CONNECT", "DELETE", "GET", "PATCH", "POST", "PUT", "TRACE",)`.

  (Note that `OPTIONS` and `HEAD` are missing by default.)

  ##### Added in 2.15.0

  The `http_methods_to_capture` option.

## [Supported Versions](https://docs.sentry.io/platforms/python/integrations/flask.md#supported-versions)

* Flask: 1.1.4+
* Python: 3.6+

The versions above apply for the current major version of the Python SDK. If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK.
