Skip to content

Treblle with Fastify

To integrate Treblle with Fastify, we provide an official SDK for Fastify - treblle-fastify.

Installation

Terminal window
npm i @treblle/fastify --save

Register treblle-fastify

treblle-fastify is a Fastify plugin, so you need to register it.

const treblleFastify = require('@treblle/fastify')
fastify.register(treblleFastify)

Notice you are not providing your apiKey and projectId, this is because treblle-fastify will look for the following environment variables:

  • TREBLLE_API_KEY
  • TREBLLE_PROJECT_ID

And use them for the apiKey and projectId respectively. That said, we recommend you set these environment variables in your .env file or your production server environment.

Config options

The treblle-fastify plugin can optionally take the following properties.

For example you can setup the treblle-fastify plugin like so:

const treblleFastify = require('@treblle/fastify')
fastiy.register(treblleFastify, {
apiKey: process.env.TREBLLE_API_KEY,
projectId: process.env.TREBLLE_PROJECT_ID,
additionalFieldsToMask: ['licensee_key'],
})

Platformatic DB

Since the Treblle SDK for Fastify is a Fastify plugin, you can easily set up Treblle in a Platformatic DB application with the following steps:

install the SDK

Run the below command to install the Treblle SDK in your Platformatic DB project

Terminal window
npm i @treblle/fastify --save

Setup environment variables

In your .env file, add the following environment variables for your Treblle API Key and Project ID respectively:

  • PLT_TREBLLE_API_KEY
  • PLT_TREBLLE_PROJECT_ID

Add plugin options

Next edit your platformatic.db.json config file to have an options property with environment variable placeholders for the Treblle credentials you set earlier in your .env file:

"plugins": {
"paths": [
{
"path": "./plugins",
"encapsulate": false,
"options": {
"treblleApiKey": "{PLT_TREBLLE_API_KEY}",
"treblleProjectId": "{PLT_TREBLLE_PROJECT_ID}"
}
},
{
"path": "./routes"
}
]
},

Setup the plugin

To setup the plugin create treblle.js file in plugins/ folder and add the below code:

/// <reference path="../global.d.ts" />
'use strict'
/** @param {import('fastify').FastifyInstance} fastify */
module.exports = async function (fastify, opts) {
const treblleFastify = require('@treblle/fastify')
fastify.register(treblleFastify, {
apiKey: opts.treblleApiKey,
projectId: opts.treblleProjectId,
})
}

Now when a request comes into your Platformatic DB app, it will get sent to your Treblle dashboard.