Skip to content

Treblle with Koa

To integrate Treblle with Koa, we provide an official unified SDK for JavaScript frameworks - treblle-node.

  • Node.js: >=18.0.0
  • Koa: 2.x or 3.x
Terminal window
npm install treblle@^2.0.0

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

const Koa = require("koa");
const { koaTreblle } = require("treblle");

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

Initialize Treblle in your application:

const Koa = require("koa");
const KoaRouter = require("koa-router");
const KoaBody = require("koa-body");
const { koaTreblle } = require("treblle");
const app = new Koa();
const router = new KoaRouter();
// Treblle middleware (add before body parsing)
app.use(
koaTreblle({
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
})
);
// Body parsing
app.use(KoaBody());
// Routes
router.get("/api/users", (ctx) => {
ctx.body = { users: [] };
});
app.use(router.routes());
app.listen(3000);

That’s it! Your API requests and responses are now being sent to your Treblle project. You’ll get features like auto-documentation, real-time request/response monitoring, and error tracking.

You can pass additional configuration options:

app.use(
koaTreblle({
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
})
);
  • 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)

Run Treblle only in production:

const app = new Koa();
if (process.env.NODE_ENV === "production") {
app.use(
koaTreblle({
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
})
);
}

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:

app.use(
koaTreblle({
sdkToken: process.env.TREBLLE_SDK_TOKEN,
apiKey: process.env.TREBLLE_API_KEY,
additionalFieldsToMask: ["customSecret", "licensee_key"],
})
);

Enable debug mode to see Treblle errors in your console:

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