Treblle with Fastify
To integrate Treblle with Fastify, we provide an official unified SDK for JavaScript frameworks - treblle-node
.
Requirements
Section titled “Requirements”- Node.js:
>=16.0.0
- Fastify:
4.x
or later
Installation
Section titled “Installation”npm install treblle@^2.0.0
Setting up credentials
Section titled “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_tokenTREBLLE_API_KEY=your_api_key
Basic Setup
Section titled “Basic Setup”Initialize Treblle in your Fastify application:
const fastify = require('fastify')();const { fastifyTreblle } = require('treblle');
// Register Treblle pluginawait fastify.register(fastifyTreblle, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY,});
// Your routesfastify.get('/api/users', async (request, reply) => { return { users: [] };});
await fastify.listen({ port: 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.
Configuration Options
Section titled “Configuration Options”You can pass additional configuration options:
await fastify.register(fastifyTreblle, { 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:
Section titled “Available options:”sdkToken
(required): Your Treblle SDK tokenapiKey
(required): Your Treblle API keyadditionalFieldsToMask
(optional): Array of field names to mask in addition to default fieldsblocklistPaths
(optional): Array of path prefixes or RegExp to exclude from loggingignoreDefaultBlockedPaths
(optional): Boolean to disable default blocked paths (default: false)debug
(optional): Boolean to show Treblle-related errors in console (default: false)
Production-only Setup
Section titled “Production-only Setup”Run Treblle only in production:
if (process.env.NODE_ENV === "production") { await fastify.register(fastifyTreblle, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, });}
Masking Sensitive Data
Section titled “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:
await fastify.register(fastifyTreblle, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, additionalFieldsToMask: ["customSecret", "licensee_key"],});
Debugging
Section titled “Debugging”Enable debug mode to see Treblle errors in your console:
await fastify.register(fastifyTreblle, { sdkToken: process.env.TREBLLE_SDK_TOKEN, apiKey: process.env.TREBLLE_API_KEY, debug: true,});