Gatsby

To use Sentry with your Gatsby application, you will need to use @sentry/gatsby (Sentry’s Gatsby SDK):

Copied
npm install --save @sentry/gatsby

Connecting the SDK to Sentry

You can configure the SDK in one of two ways discussed below: by creating a configuration file, or defining the options along with the Gatsby configuration. If you define options in both places, the SDK will prioritize the init in the configuration file and ignore the options in the Gatsby configuration.

Sentry Configuration File

Using a Sentry configuration file is the approach we recommend since it supports defining non-serializable options in the init. Note that you still need to include the plugin, even if you don't set any options.

gatsby-config.js
Copied
module.exports = {
  plugins: [
    {
      resolve: "@sentry/gatsby",
    },
  ]
};

Configure your Sentry.init:

sentry.config.js
Copied
import * as Sentry from '@sentry/gatsby';

Sentry.init({
    dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
    sampleRate: 1.0, // Adjust this value in production
    beforeSend(event) {
      // Modify the event here
      if (event.user) {
        // Don't send user's email address
        delete event.user.email;
      }
      return event;
    },
    // ...
});

Gatsby Plugin Configuration

Another alternative is to use Gatsby's plugin configuration options. While this keeps the SDK options with the plugin definition, it doesn't support non-serializable options.

gatsby-config.js
Copied
module.exports = {
    {
      resolve: "@sentry/gatsby",
      options: {
        dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
        sampleRate: 1.0, // Adjust this value in production
        // Cannot set `beforeSend`
      },
    },

  ]
};

With this approach, the SDK sets some options automatically based on environmental variables, but these can be overridden by setting custom options.

environment (string)
Defaults to process.env.NODE_ENV or development

release (string)
Defaults to certain environment variables based on the platform

  • GitHub Actions: process.env.GITHUB_SHA
  • Netlify: process.env.COMMIT_REF
  • Vercel: process.env.VERCEL_GIT_COMMIT_SHA
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").