Star us on GitHub
Star
Menu

Nest.js

Learn how to set up highlight.io in Nest.js.
1
Set up your frontend Highlight snippet.

This backend SDK requires one of the Highlight frontend SDKs to be installed, so please make sure you've followed the fullstack mapping guide first.

H.init("<YOUR_PROJECT_ID>", { tracingOrigins: ['localhost', 'example.myapp.com/backend'], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Copy
2
Install the Highlight JS SDK.

Install the @highlight-run/nest package with your package manager.

# with yarn yarn add @highlight-run/nest # with pnpm pnpm add @highlight-run/nest # with npm npm install @highlight-run/nest
Copy
3
Add the @highlight-run/nest app middleware.

Use the HighlightErrorFilter middleware to capture backend errors.

import { HttpAdapterHost, NestFactory } from '@nestjs/core' import { AppModule } from './app.module' import { HighlightErrorFilter } from '@highlight-run/nest' async function bootstrap() { const app = await NestFactory.create(AppModule) const highlightOpts = { projectID: 'YOUR_PROJECT_ID' } app.useGlobalFilters( new HighlightErrorFilter(app.get(HttpAdapterHost), highlightOpts), ) await app.listen(3000) } bootstrap()
Copy
4
Optionally, report manual errors in your app.

If you need to report exceptions outside of a handler, use the Highlight SDK.

const parsed = H.parseHeaders(request.headers) H.consumeError(error, parsed?.secureSessionId, parsed?.requestId)
Copy
5
Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your Nest.js handlers. Access the API handler and make sure the error shows up in Highlight.

import { Injectable } from '@nestjs/common' @Injectable() export class AppService { getHello(): string { console.log('hello, world!') console.warn('whoa there! ', Math.random()) if (Math.random() < 0.2) { // error will be caught by the HighlightErrorFilter throw new Error(`a random error occurred! 0.8663367722471802`) } return 'Hello World!' } }
Copy
6
Set up logging.

Start sending logs to Highlight! Follow the logging setup guide to get started.