Cloudflare Docs
Pages
Edit this page on GitHub
Set theme to dark (⇧+D)

Sentry Pages Plugin

The Sentry Pages Plugin captures and logs all exceptions which occur below it in the execution chain of your Pages Functions. It is therefore recommended that you install this Plugin at the root of your application in functions/_middleware.ts as the very first Plugin.

​​ Installation

$ npm install @cloudflare/pages-plugin-sentry

​​ Usage

functions/_middleware.ts
import sentryPlugin from "@cloudflare/pages-plugin-sentry";
export const onRequest: PagesFunction = sentryPlugin({
dsn: "https://sentry.io/welcome/xyz",
});

The Plugin uses Toucan. Refer to the Toucan README to review the options it can take. context, request, and event are automatically populated and should not be manually configured.

If your DSN is held as an environment variable or in KV, you can access it like so:

functions/_middleware.ts
import sentryPlugin from "@cloudflare/pages-plugin-sentry";
export const onRequest: PagesFunction<{
SENTRY_DSN: string;
}> = (context) => {
return sentryPlugin({ dsn: context.env.SENTRY_DSN })(context);
};
functions/_middleware.ts
import sentryPlugin from "@cloudflare/pages-plugin-sentry";
export const onRequest: PagesFunction<{
KV: KVNamespace;
}> = async (context) => {
return sentryPlugin({ dsn: await context.env.KV.get("SENTRY_DSN") })(context);
};

​​ Additional context

If you need to set additional context for Sentry (for example, user information or additional logs), use the data.sentry instance in any Function below the Plugin in the execution chain.

For example, you can access data.sentry and set user information like so:

functions/admin/_middleware.ts
import type { PluginData } from "@cloudflare/pages-plugin-sentry";
export const onRequest: PagesFunction<unknown, any, PluginData> = async ({
data,
next,
}) => {
// Authenticate the user from the request and extract user's email address
const email = await getEmailFromRequest(request);
data.sentry.setUser({ email });
return next();
};

Again, the full list of features can be found in Toucan’s documentation.