Connect Fastify to Bugsink

When a Fastify request fails, Bugsink shows you the exception, the application frame that raised it, and the HTTP request that reached the route.

This guide covers the initial setup and Fastify’s automatic capture of route errors. You’ll connect an existing application, then make one request fail to confirm that it reaches Bugsink.

To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.

Install the SDK

Install the open-source Sentry SDK alongside Fastify. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your project:

npm install --save-exact @sentry/node@11.14.0

Initialize the SDK before Fastify

Create an instrumentation module which initializes the SDK from the DSN in the environment:

import * as Sentry from "@sentry/node";


Sentry.init({
  dsn: process.env.SENTRY_DSN,
});

Load this module with Node’s --import option before server.mjs. That gives the SDK a chance to instrument Fastify while modules load:

"scripts": {
    "start": "node --import ./instrument.mjs server.mjs"
  },

Set SENTRY_DSN in the environment where the server runs:

export SENTRY_DSN=https://key@bugsink.example/1

Add the Fastify error handler

Import the SDK in the server module, define your routes, then register its Fastify error handler:

import * as Sentry from "@sentry/node";
import Fastify from "fastify";
:::javascript
Sentry.setupFastifyErrorHandler(app);

Fastify still owns the HTTP response. The SDK observes exceptions which reach the framework’s error path and lets Fastify continue handling them.

Trigger an unhandled route exception

There is no reporting call inside a route. To verify the setup, this checkout route throws when the requested product is missing:

app.get("/checkout/:sku", async (request) => {
  const product = inventory.get(request.params.sku);
  if (!product) {
    throw new ProductNotFound(`Product ${request.params.sku} is not in inventory`);
  }
  return { reserved: product.name };
});

Start the application as usual, then request a missing product:

npm start
:::bash
curl http://127.0.0.1:3000/checkout/fastify-missing

Fastify returns an HTTP 500 response and the SDK reports the exception.

See the error in Bugsink

Open your Bugsink project and select the new ProductNotFound issue. Its event points to the failing line in the route and includes the GET request.

Bugsink preview

That’s it: errors reaching Fastify’s error handler will now be reported to Bugsink. Your routes remain ordinary application code, while Bugsink receives their JavaScript stack and request context.

For handled exceptions, messages, tags, structured context, and breadcrumbs, continue with the Node.js guide.