Get started with Hapi

Getting Opbeat set up for your Hapi 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.


$ 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 hapi, before http, even before Babel if you use that.

// 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 Hapi = require('hapi')

var server = new Hapi.Server()

  host: 'localhost',
  port: 8000

  method: 'GET',
  path: '/hello',
  handler: function (request, reply) {
    return reply('hello world')


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 configuration options in the API documentation.

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

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.


See the Compatibility section in the API documentation.


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