NestJS
v9 / v10A 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-observeQuick Start
The minimal setup — import, configure, and you're observing.
nestjs-quick-start.js
// 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
// 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
| Option | Type | Default |
|---|---|---|
| apiKey | string | — |
| slowThreshold | number | 1000 |
| skipRoutes | string[] | [] |
| tags | Record | {} |
| processMetrics | number | false | 30000 |
| captureUnhandledErrors | boolean | false |
Custom Fields
Attach any business context to the current request via AsyncLocalStorage — scoped per request, works the same on every framework.
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