---
title: "Plug and Phoenix"
description: "Learn how to capture errors and their context in Plug pipelines and Phoenix applications."
url: https://docs.sentry.io/platforms/elixir/integrations/plug_and_phoenix/
---

# Plug and Phoenix | Sentry for Elixir

You can capture errors in [Plug](https://github.com/elixir-plug/plug) and [Phoenix](https://phoenixframework.org) applications with `Sentry.PlugContext` and `Sentry.PlugCapture`.

`Sentry.PlugContext` adds contextual metadata from the current request to the reported errors that happen during Plug execution. For [Cowboy](https://hex.pm/packages/cowboy) applications, an additional plug `Sentry.PlugCapture` is required to ensure this metadata propagates properly.

## [Phoenix Configuration](https://docs.sentry.io/platforms/elixir/integrations/plug_and_phoenix.md#phoenix-configuration)

If you are using Phoenix, follow these steps to add the Plug integration:

1. Add `Sentry.PlugContext` below `Plug.Parsers`.

```elixir
defmodule MyAppWeb.Endpoint
   use Phoenix.Endpoint, otp_app: :my_app

   # ...

   plug Plug.Parsers,
     parsers: [:urlencoded, :multipart, :json],
     pass: ["*/*"],
     json_decoder: Phoenix.json_library()

+  plug Sentry.PlugContext
```

2. If you're using Cowboy, add `Sentry.PlugCapture` above the `use Phoenix.Endpoint` line in your endpoint file.

```elixir
defmodule MyAppWeb.Endpoint
+  use Sentry.PlugCapture
   use Phoenix.Endpoint, otp_app: :my_app

   # ...
```

If you're also using [Phoenix LiveView](https://github.com/phoenixframework/phoenix_live_view), consider also setting up your LiveViews to use the `Sentry.LiveViewHook` hook (available in the Sentry SDK versions `10.5.0` and higher):

```elixir
defmodule MyAppWeb do
  def live_view do
    quote do
      use Phoenix.LiveView

      on_mount Sentry.LiveViewHook
    end
  end
end
```

### [Capturing User Feedback](https://docs.sentry.io/platforms/elixir/integrations/plug_and_phoenix.md#capturing-user-feedback)

If you would like to [capture user feedback](https://docs.sentry.io/platforms/elixir/enriching-events/user-feedback.md), the `Sentry.get_last_event_id_and_source/0` function can be used to see if Sentry has sent an event within the current Plug process (and get the source of that event). `:plug` will be the source for events coming from `Sentry.PlugCapture`. The options described in the Sentry documentation linked above can be encoded into the response as well.

Below is an example Phoenix application setup that displays the user feedback form on `500` responses on requests accepting HTML:

```elixir
defmodule MyAppWeb.ErrorView do
  # ...

  def render("500.html", _assigns) do
    case Sentry.get_last_event_id_and_source() do
      {event_id, :plug} when is_binary(event_id) ->
        opts = Jason.encode!(%{eventId: event_id})

        ~E"""
        <script src="https://browser.sentry-cdn.com/5.9.1/bundle.min.js" integrity="sha384-/x1aHz0nKRd6zVUazsV6CbQvjJvr6zQL2CHbQZf3yoLkezyEtZUpqUNnOLW9Nt3v" crossorigin="anonymous"></script>
        <script>
          Sentry.init({ dsn: '<%= Sentry.get_dsn() %>' });
          Sentry.showReportDialog(<%= raw opts %>)
        </script>
        """

      _ ->
        "Error"
    end
  end
end
```

## [Plug Configuration](https://docs.sentry.io/platforms/elixir/integrations/plug_and_phoenix.md#plug-configuration)

If you have a non-Phoenix application, follow these steps to configure the Plug integration:

1. Add `Sentry.PlugContext` below `Plug.Parsers` (if it is in your stack).

```elixir
 defmodule MyApp.Router do
   use Plug.Router

   # ...

   plug Plug.Parsers,
     parsers: [:urlencoded, :multipart]

+  plug Sentry.PlugContext
```

2. If you're using Cowboy, add `Sentry.PlugCapture` at the top of your Plug application.

```elixir
 defmodule MyApp.Router do
   use Plug.Router
+  use Sentry.PlugCapture

   # ...
```
