Use Sentry with NestJS

Luqman Marzuki
2 min readNov 29, 2019

The quickest way to integrate Sentry.io with NestJS framework without using Raven

First of all, install the required Sentry Node SDK with npm or yarn:

# Using yarn
$ yarn add @sentry/node@5.9.0
# Or use npm
$ npm install @sentry/node@5.9.0

Locate your Sentry DSN by going to the project settings:

Settings > Projects > Select the project to track
Choose on Client Keys (DSN) to show the DSN

Add these lines to src/main.ts file and change it to your own DSN:

import * as Sentry from '@sentry/node';Sentry.init({
dsn: 'https://4e98de50242247eebc5512b55bc558a3@sentry.io/1837145',
});

An example for the implementation would be like this:

src/main.ts

Create a new interceptor file:

src/sentry.interceptor.ts

Inject that interceptor to the desired controller by adding these lines:

import { UseInterceptors } from '@nestjs/common';
import { SentryInterceptor } from './sentry.interceptor';
@UseInterceptors(SentryInterceptor)

An example on how those codes were to be added:

src/app.controller.ts

And that’s it!

To test if it’s working, you can add an error exception to a route and make requests to that route. For example:

src/app.controller.ts

Then check if it’s recorded in your Sentry’s project dashboard:

Sentry Project Issue Dashboard

--

--

Responses (6)