Skip to Content

Treblle Docs

The official Treblle SDK for JavaScript frameworks including Express. Seamlessly integrate Treblle to manage communication with your dashboard, send errors, and secure sensitive data.

Treblle with Express

Requirements

  • Node.js: >=14.0.0
  • Express: 4.x or 5.x

Installation

npm install treblle@^2.0.0

Don’t forget to load the required modules in your app.js:

const express = require("express"); const { useTreblle } = 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_key

Basic Setup

Initialize Treblle in your app.js file:

const express = require("express"); const { useTreblle } = require("treblle"); const app = express(); app.use(express.json()); // Treblle must be registered after body parsing middleware useTreblle(app, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, }); app.get("/api/users", (req, res) => { res.json({ users: [] }); }); app.listen(3000);

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.

Middleware Ordering

Note

Ensure Treblle is registered after body parsing middleware:

app.use(express.json()); // First: body parsing app.use(express.urlencoded({ extended: true })); useTreblle(app, { /* config */ }); // Then: Treblle // Finally: your routes app.get('/api/users', handler);

Configuration Options

You can pass additional configuration options:

useTreblle(app, { 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:

  • 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)

Production-only Setup

Run Treblle only in production:

const app = express(); app.use(express.json()); if (process.env.NODE_ENV === "production") { useTreblle(app, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, }); }

Masking Sensitive Data

The following fields are automatically masked:

  • password, pwd, secret, password_confirmation, passwordConfirmation
  • cc, card_number, cardNumber, ccv
  • ssn
  • credit_score, creditScore

Add custom fields to mask:

useTreblle(app, { 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:

useTreblle(app, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, debug: true, });

Tip

For production environments, consider using environment-based configuration to automatically enable or disable debug mode without code changes.

Last updated on