Sentry is a tool that helps you monitor your application and identify issues from backend to frontend. In this guide, we will set up Sentry for your supastarter app. ## Create a Sentry project Before we start integrating Sentry into your application, you need to sign up for [Sentry](https://sentry.io) and create a new organization and project in the Sentry dashboard. ![Create sentry organization](/images/documentation/nextjs/monitoring/sentry/create-organization.png) Follow the setup wizard to create a new project by selecting Next.js as the platform and entering the project name: ![Select platform](/images/documentation/nextjs/monitoring/sentry/select-platform.png) ![Enter project name](/images/documentation/nextjs/monitoring/sentry/create-project.png) ## Set up Sentry in your application ### 1. Install the Sentry SDK Add the Sentry SDK to your web application by running the following command: ```bash pnpm --filter web add @sentry/nextjs ``` ### 3. Add Sentry to Next.js config In your `/apps/web/next.config.ts` file, add the Sentry plugin: ```ts title="apps/web/next.config.ts" import { withSentryConfig } from "@sentry/nextjs"; // ... export default withNextIntl( withContentCollections( withSentryConfig(nextConfig, { org: "supastarter", project: "supastarter-web", authToken: process.env.SENTRY_AUTH_TOKEN, tunnelRoute: "/monitoring-tunnel", silent: false, }), ), ); ``` \__Note Make sure to call the `with*` functions in the correct order, as there might be issues with a different order._ As we are using the tunnel route, we need to make sure that the middleware is not blocking the request to the tunnel route. For this we need to extend the matcher to the middleware config: ```typescript title="apps/web/middleware.ts" // ... export const config = { matcher: [ "/((?!api|image-proxy|images|fonts|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|monitoring-tunnel).*)", ], }; ``` ### 3. Create sentry config files Create the following files in the `/apps/web` folder: **Note: Replace the DSN url with the one from your Sentry project.** ```typescript title="apps/web/sentry.client.config.ts" import * as Sentry from "@sentry/nextjs"; Sentry.init({ dsn: "https://a19de6cb0beb3b0f5b5874eb59901f84@o4508742845857792.ingest.de.sentry.io/4508742854574160", integrations: [Sentry.replayIntegration()], tracesSampleRate: 1, replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1.0, debug: false, }); ``` ```typescript title="apps/web/sentry.server.config.ts" import * as Sentry from "@sentry/nextjs"; Sentry.init({ dsn: "https://a19de6cb0beb3b0f5b5874eb59901f84@o4508742845857792.ingest.de.sentry.io/4508742854574160", tracesSampleRate: 1, debug: false, }); ``` ```typescript title="apps/web/sentry.server.config.ts" import * as Sentry from "@sentry/nextjs"; Sentry.init({ dsn: "https://a19de6cb0beb3b0f5b5874eb59901f84@o4508742845857792.ingest.de.sentry.io/4508742854574160", tracesSampleRate: 1, debug: false, }); ``` ### 4. Set up Next.js intstrumentation Next, we will set up Next.js intstrumentation to capture errors and performance data for the different types of requests. Create a new file in the `/apps/web` folder: ```typescript title="apps/web/instrumentation.ts" import * as Sentry from "@sentry/nextjs"; export async function register() { if (process.env.NEXT_RUNTIME === "nodejs") { await import("./sentry.server.config"); } if (process.env.NEXT_RUNTIME === "edge") { await import("./sentry.edge.config"); } } export const onRequestError = Sentry.captureRequestError; ``` ### 5. Create global error handler Create a new file in the `/apps/web/app` folder: ```tsx title="apps/web/app/global-error.tsx" "use client"; import * as Sentry from "@sentry/nextjs"; import NextError from "next/error"; import { useEffect } from "react"; export default function GlobalError({ error, }: { error: Error & { digest?: string }; }) { useEffect(() => { Sentry.captureException(error); }, [error]); return ( ); } ``` ### 6. Set Sentry envs In the [Sentry dashboard](https://supastarter.sentry.io/settings/auth-tokens/), go to _Settings > Auth Tokens_ and create a new token. Add the new token as the `SENTRY_AUTH_TOKEN` environment variable to your `.env` file. We also need to set the `SENTRY_SUPPRESS_TURBOPACK_WARNING` environment variable to `1` to suppress the warning about the Turbopack integration. ```bash SENTRY_AUTH_TOKEN= SENTRY_SUPPRESS_TURBOPACK_WARNING=1 ``` And that's it! You can now start your application and see the errors and performance data in the Sentry dashboard. You can find a full example of the Sentry setup in the repository [supastarter-nextjs/feat/sentry-integration](https://github.com/supastarter/supastarter-nextjs/tree/feat/sentry-integration) repository. For more information and further use cases, check out the [offical Sentry setup guide for Next.js](https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/).