Express integration guide

Add BugSnag to your Express projects to automatically capture and report errors in production.

New to BugSnag? Create an account

Looking for performance monitoring? See our web app integration



Install BugSnag and the BugSnag / Express integration from the npm registry using npm or yarn:

npm install --save @bugsnag/js @bugsnag/plugin-express
# or
yarn add @bugsnag/js @bugsnag/plugin-express

The latest available version of @bugsnag/js is v7.23.0.

Basic configuration

This documentation is for version 7 of the BugSnag JavaScript notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Depending on which module system you are using, you’ll need to include BugSnag in one of the following ways:

// commonjs/node-style require
var Bugsnag = require('@bugsnag/js')
var BugsnagPluginExpress = require('@bugsnag/plugin-express')

// ES module-style import
import Bugsnag from '@bugsnag/js'
import BugsnagPluginExpress from '@bugsnag/plugin-express'

To start BugSnag with the Express integration, import it and pass it along with your API key to Bugsnag.start as configuration:

  apiKey: 'YOUR_API_KEY',
  plugins: [BugsnagPluginExpress],
  otherOptions: value

You can find your API key in Project Settings from your BugSnag dashboard.

Finally, in the part of your application where the Express server is configured, obtain and use the Express middleware:

const app = express()
const middleware = Bugsnag.getPlugin('express')

// This must be the first piece of middleware in the stack.
// It can only capture errors in downstream middleware

/* all other middleware and application routes go here */

// This handles any errors that Express catches. This needs to go before other
// error handlers. BugSnag will call the `next` error handler if it exists.

For information on values that can be set in the configuration object, see configuration options.

TypeScript support

Type definitions are provided and will be picked up automatically by the TypeScript compiler when you import any of the top-level @bugsnag/* packages.

AWS Lambda support

@bugsnag/js can be used in AWS Lambda functions using the @bugsnag/plugin-aws-lambda package.

See AWS Lambda for integration instructions.

Showing full stacktraces

Source maps enable BugSnag to show you the original file, line, method and surrounding code in your stacktraces.

See the build integrations guide to find out how to upload source maps to BugSnag.

Reporting unhandled errors

After completing installation and basic configuration, unhandled exceptions and unhandled promise rejections will be reported and automatically appear on your BugSnag dashboard.

Unhandled errors in Express routes will be reported with information about the request.

Reporting handled errors

Sometimes it is useful to manually notify BugSnag of a problem. To do this, call Bugsnag.notify(). For example:

try {
} catch (e) {

When reporting handled errors, it’s often helpful to send custom diagnostic data or to adjust the severity of particular errors. For more information, see reporting handled errors.

As well as capturing and reporting unhandled errors, the requestHandler middleware attaches a request-scoped BugSnag client to req.bugsnag. This means that if you encounter an error in route or piece of middleware, you can call req.bugsnag.notify(err) which will include information about the request in the error report. For example:

app.get('/purchase/:productId', function (req, res, next) {
  db.products.find(, function (err, product) {
    // calling next(err) routes the error to the BugSnag errorHandler middleware
    if (err) return next(err)

    if (product.discontinued) {

      // This report will include detail about the request, plus anything we attach here
        new Error('Attempted to purchase discontinued product'),
        function (event) {
          event.addMetadata('product', product)

    } else {

Sending diagnostic data

Automatically captured diagnostics

BugSnag will automatically capture the following data for every exception:

  • Request info (if relevant and available)
  • Device time
  • Hostname
  • Release stage (production, beta, staging, etc)

Attaching custom diagnostics

It can often be helpful to attach application-specific diagnostic data to error reports. This can be accomplished by setting a callback which will be invoked before any reports are sent to BugSnag.

The following adds a map of data to the “company” tab on the BugSnag dashboard for all captured events:

  onError: function (event) {
    event.addMetadata('company', {
      name: "Acme Co.",
      country: "uk"

For more information, see Customizing error reports.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your BugSnag dashboard.

You can set the user information of an error report using the user configuration property when BugSnag starts or via an onError callback.

  onError: function (event) {
    event.setUser('3', '', 'Bugs Nag')

For information on doing so, see Adding user data.

Logging breadcrumbs

Breadcrumbs are not yet supported on Node.

Session tracking

BugSnag tracks the number of “sessions” that happen within your application. This allows you to compare stability scores between releases and helps you to understand the quality of your releases.

Sessions are captured and reported by default. This behavior can be disabled using the autoTrackSessions configuration option.

Sessions are reported in Node.js every time a request is served if you are using one of the server integrations:

  • @bugsnag/plugin-express
  • @bugsnag/plugin-restify
  • @bugsnag/plugin-koa

A summary of sessions recorded will be periodically sent to BugSnag.

For more information about manually controlling session tracking, see Capturing sessions.

Declaring feature flags and experiments

Monitor errors as you roll out features or run experiments and A/B tests by declaring your feature flag and experiment usage in the BugSnag client. You can use the Features dashboard to identify whether these features have introduced errors into your app.

req.bugsnag.addFeatureFlag('Checkout button color', 'Blue')
req.bugsnag.addFeatureFlag('New checkout flow')

Where req is the request context.

For more information, see Feature flags & experiments.

Tracking releases

Configure your app version to see the release that each error was introduced in.

Bugsnag.start({ appVersion: '4.10.0' })

Then set up a build tool integration to enable linking to code in your source control provider from the releases dashboard, timeline annotations, and stack traces.

Next steps

  • View @bugsnag/js, the library powering BugSnag for Node.js, on GitHub
  • Monitor the performance of your web app in your users’ browsers with the Web performance guide
  • Get support for your questions and feature requests