APILensAPILens
frameworks/nestjs

NestJS

v9 / v10

A global NestJS interceptor — wire it once, covers every controller.

Returns a class that implements `NestInterceptor`. Register globally in `main.ts` and every route is automatically observed.

Requires `rxjs` in your project (already included by NestJS).

Install

bash
npm install auto-api-observe

Quick Start

The minimal setup — import, configure, and you're observing.

nestjs-quick-start.js
javascript
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule }   from './app.module';
import { createNestObservabilityInterceptor } from 'auto-api-observe';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const Interceptor = createNestObservabilityInterceptor({
    apiKey: process.env.APILENS_KEY,
  });
  app.useGlobalInterceptors(new Interceptor());

  await app.listen(3000);
}
bootstrap();

Full Example

All available options with custom fields and real-world usage.

nestjs-full.js
javascript
// main.ts
import { NestFactory }    from '@nestjs/core';
import { AppModule }      from './app.module';
import { createNestObservabilityInterceptor } from 'auto-api-observe';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const Interceptor = createNestObservabilityInterceptor({
    apiKey:        process.env.APILENS_KEY,
    slowThreshold: 500,
    skipRoutes:    ['/health'],
    tags:          { service: 'nest-api', env: process.env.NODE_ENV },
    processMetrics: 30_000,
    captureUnhandledErrors: true,
  });

  app.useGlobalInterceptors(new Interceptor());
  await app.listen(3000);
}
bootstrap();

// In any controller, enrich the log:
// import { addField } from 'auto-api-observe';
// addField('userId', user.id);

What's Tracked Automatically

  • HTTP method, URL, status, latency
  • Request via switchToHttp().getRequest()
  • Uses RxJS tap() to measure after the handler completes
  • DB and outbound calls

Options Reference

OptionTypeDefault
apiKeystring
slowThresholdnumber1000
skipRoutesstring[][]
tagsRecord{}
processMetricsnumber | false30000
captureUnhandledErrorsbooleanfalse

Custom Fields

Attach any business context to the current request via AsyncLocalStorage — scoped per request, works the same on every framework.

javascript
const { addField } = require('auto-api-observe');

// In any route handler:
addField('userId',  req.user.id);
addField('plan',    req.user.plan);
addField('country', req.headers['cf-ipcountry']);

// Sensitive keys are auto-redacted before shipping:
addField('authorization', token); // → "[REDACTED]"
addField('password',      pass);  // → "[REDACTED]"

Ready to observe your NestJS app?

Sign up free — get your API key in under 30 seconds. No credit card required.

Get your free API key