The official Treblle SDK for JavaScript frameworks including NestJS. Seamlessly integrate Treblle to manage communication with your dashboard, send errors, and secure sensitive data.
Treblle with NestJS
To integrate Treblle with NestJS, we provide an official unified SDK for JavaScript frameworks - treblle-node.
NestJS uses Express under the hood, so Treblle integrates by accessing the underlying Express instance.
Requirements
- Node.js:
>=16.0.0 - NestJS:
9.x,10.x, or11.x
Installation
npm install treblle@^2.0.0Don’t forget to load the required modules:
const { useNestTreblle } = require("treblle");Setting up credentials
Create a free account on treblle.com to get your credentials:
sdkToken(SDK Token)apiKey(API Key)
Add these to your .env file:
TREBLLE_SDK_TOKEN=your_sdk_token
TREBLLE_API_KEY=your_api_keyBasic Setup
Initialize Treblle in your main.ts file:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { useNestTreblle } from "treblle";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Get the underlying Express instance
const expressInstance = app.getHttpAdapter().getInstance();
// Add Treblle middleware
useNestTreblle(expressInstance, {
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
});
await app.listen(3000);
}
bootstrap();Tip
That’s it! Your API requests and responses are now being sent to your Treblle Dashboard. You’ll get features like auto-documentation, real-time request/response monitoring, and error tracking.
Configuration Options
You can pass additional configuration options:
useNestTreblle(expressInstance, {
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
additionalFieldsToMask: ["customSecret", "internalId"], // Optional
blocklistPaths: ["admin", "health"], // Optional: Skip logging certain paths
ignoreDefaultBlockedPaths: false, // Optional: Disable default blocked paths
debug: true, // Optional: Show Treblle errors in console
});Available options:
Option
Description
sdkToken (required)
Your Treblle SDK token
apiKey (required)
Your Treblle API key
additionalFieldsToMask (optional)
Array of field names to mask in addition to default fields
blocklistPaths (optional)
Array of path prefixes or RegExp to exclude from logging
ignoreDefaultBlockedPaths (optional)
Boolean to disable default blocked paths (default: false)
debug (optional)
Boolean to show Treblle-related errors in console (default: false)
Using Environment Variables
import { ConfigService } from "@nestjs/config";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const expressInstance = app.getHttpAdapter().getInstance();
useNestTreblle(expressInstance, {
sdkToken: configService.get("TREBLLE_SDK_TOKEN"),
apiKey: configService.get("TREBLLE_API_KEY"),
debug: configService.get("NODE_ENV") !== "production",
});
await app.listen(3000);
}
bootstrap();Important Notes
Note
- Must be called after
NestFactory.create()but beforeapp.listen() - Only works with the Express adapter (default NestJS adapter)
- For Fastify adapter, consider using the Fastify integration directly
Production-only Setup
Run Treblle only in production:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
if (process.env.NODE_ENV === "production") {
const expressInstance = app.getHttpAdapter().getInstance();
useNestTreblle(expressInstance, {
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
});
}
await app.listen(3000);
}Masking Sensitive Data
The following fields are automatically masked:
password,pwd,secret,password_confirmation,passwordConfirmationcc,card_number,cardNumber,ccvssncredit_score,creditScore
Add custom fields to mask:
useNestTreblle(expressInstance, {
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
additionalFieldsToMask: ["customSecret", "licensee_key"],
});Debugging
Enable debug mode to see Treblle errors in your console:
useNestTreblle(expressInstance, {
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
debug: true,
});