Opbeat is joining forces with Elastic – Learn more about Elastic APM

Get started with Express

Getting Opbeat set up for your Express app is easy, and there are various ways you can tweak it to fit to your needs.

Follow the guide below and refer to the API documentation for all the advanced stuff.

Watch a short screencast about how to get started with Opbeat in your Node.js app.


$ npm install opbeat --save


It’s important that the Opbeat agent is started before you require any other modules in your Node.js application - i.e. before express, http, etc.

This means that you should probably require and start it in your applications main file (usually index.js, server.js or app.js).

Here’s a simple Express example with Opbeat installed:

// add this to the VERY top of the first file loaded in your app
var opbeat = require('opbeat').start({
  appId: '<app id>',
  organizationId: '<org id>',
  secretToken: '<token>'

var app = require('express')()

app.get('/', function (req, res) {
  res.send('Hello World!')

// any errors caught by Express can be logged by Opbeat as well


The Opbeat agent will now monitor the performance of your Express application and record any uncaught exceptions.

Note: opbeat.middleware.express must be added to the middleware stack before any other error handling middlewares or there’s a chance that the error will never get to Opbeat. See middleware documentation for details.

Advanced configuration

In the above example we initialize the agent by calling the start() function. This function takes an optional options object used to configure the agent. Any option not supplied via the options object can instead be configured using environment variables. So if you prefer, you can set the following three environment variables:

OPBEAT_APP_ID=<app id>

And then just start the agent like so:

// Start the Opbeat agent before any thing else in your app
var opbeat = require('opbeat').start()

See all possible ways to configure the agent in the API documentation.

Full documentation

Performance monitoring

Opbeat automatically measures the performance of your Express application. It records traces for database queries, external HTTP requests and other slow operations that happens during requests to your Express app.

By default Opbeat will trace the most common modules. To trace other events, you can use custom traces. For information about custom traces, see the Custom traces in Node.js article.

Traces are grouped in transactions - by default one for each incoming HTTP request. But it’s possible to create custom transactions not associated with an HTTP request. See the Custom transactions in Node.js article for details.

Unknown routes

When viewing the performance metrics of your application on Opbeat, you might see some transactions named “unknown route”. This indicates that the Opbeat agent detected an incomming HTTP request to your application, but didn’t know which route in your Express app the HTTP request matched.

This might simply be 404 requests, which by design doesn’t match any route, or it might be a symptom that Opbeat wasn’t set up correctly. If you see this or can’t get any meaningful metrics to show up, please follow the Troubleshooting Guide.

Error logging

The Opbeat agent will by default watch for uncaught exceptions and log these with Opbeat. But in most cases you also want to log non-thrown errors. This section explains how.

Express error handlers

Express works by funnelling all incoming http requests through a series of middlewares (one of which being your router). If an error is either thrown synchronously inside one of the middleware functions or is passed as the first argument to the middleware next() function, it will be passed to the Express error handler.

It’s recommended that you register Opbeat as an Express error handler to automatically catch those errors and log them with Opbeat.

In the case where you have multiple Express error handlers, the Opbeat error handler should be the first in the chain to ensure that it will receive the error correctly.

var opbeat = require('opbeat').start()
var express = require('express')

var app = express()

// Your regular middleware and router...

// Add the Opbeat middleware after your regular middleware

// ...but before any other error handler
app.use(function (err, req, res, next) {
  // Custom error handling goes here

Manual error logging

By default the Opbeat agent will watch for uncaught exceptions and send them to Opbeat automatically. But in most cases errors are not thrown but returned via a callback, caught by a promise or simply manually created. Those errors will not automatically be sent to Opbeat. To manually send an error to Opbeat, simply call opbeat.captureError():

var err = new Error('Ups, something broke!')


For advanced logging of errors, including adding extra meta-data to the error, see the API documentation.

Ignore ‘404 Not Found’ errors

By default Express will treat http requests that do not match any route as an error. This is a good idea in development, but usually not something you want in production. To avoid sending “404 Not Found” errors to Opbeat, make sure you handle those before they reach the Opbeat middleware:

// Your regular middleware and router...

// Put a catch-all route handler as the very last route handler
app.use(function (req, res) {
  // If we reach this point it means that no prior route matched.
  // This means that we should render a "404 Not Found" page. Notice
  // that we do not call next() here as we don't want to forward the
  // request to the error handler below.

  // Send a 404 to the user
  res.status(404).send('This isn\'t the page you\'re looking for..')

// After the 404 Not Found handler, add the Opbeat error handler

Filter sensitive information

By default the Opbeat agent will filter common sensitive information before sending errors and metrics to the Opbeat servers.

It’s possible for you to tweak these defaults or remove any information you don’t want to send to Opbeat:

  • By default the Opbeat agent will not log the body of sampled HTTP requests. To enable this, use the logBody config option

  • By default the Opbeat agent will filter certain HTTP headers known to contain sensitive information. To disable this, use the filterHttpHeaders config option

  • To apply custom filters, use the opbeat.addFilter() function

Add your own data

Opbeat will keep track of the active HTTP request and will link it to errors and recorded transaction metrics when they are sent to Opbeat. This allows you to see details about which request resulted in a particular error or which requests cause a certain HTTP endpoint to be slow.

But in many cases, information about the HTTP request it self isn’t enough. To add even more metadata to errors and transactions, use one of the two functions below:

  • opbeat.setUserContext() - Call this to enrich collected performance data and errors with information about the user/client

  • opbeat.setExtraContext() - Call this to enrich collected performance data and errors with any information that you think will help you debug performance issues and errors


See the Compatibility article for details.


If you can’t get the Opbeat agent to work as expected, please follow the Troubleshooting Guide.