The Sailscasts Blog

← Back to blog

Setting up API monitoring with Treblle in Sails

Kelvin Omereshone
Setting up API monitoring with Treblle in Sails

Treblle came on my radar on Twitter a few weeks ago and I thought “wow this is much better than looking at my cloud provider console logs” as it provides very rich insights into production APIs.

I tried integrating Treblle in the Sailscasts codebase and seeing the SDK for Node was originally intended for Express(and Sails is built on Express), I decided to create a Sails hook to make the integration super easy.

Let me walk you through how to set up Treblle in your Sails powered APIs using the treblle-sails hook I made.

Installation

To get started with the setup, install the treblle-sails hook by running the below command in your terminal:

npm i --save treblle-sails

Getting Treblle credentials

So the treblle-sails hook needs you to provide your Treblle API Key and Project ID, both info you can get on your Treblle Dashboard after you create a Treblle free account.

To get your Treblle API key, click on your avatar on the Treblle Dashboard and click on Settings.

To get the Project ID, after creating a project, open up the project and click on the Settings.

Setting up credentials

Assuming you’ve gotten both the API key and Project ID, in your config/local.js you can set it up like so:

// config/local.js
treblle: {
  apiKey: '<YOUR_TREBLLE_API_KEY>',
  projectId: '<YOUR_TREBLLE_PROJECT_ID>'
}

Now of course you won’t be able to use this in production yet because config/local.js is not being pushed to deployment.

treblle-sails will smartly look for the following environment variables:

  • TREBLLE_API_KEY
  • TREBLLE_PROJECT_ID

So if you don’t want to set them in config/local.js you can skip that step and just use the environment variables mentioned above in your service provider like Render or Heroku.

And that’s it you are done. When you deploy your API, you will get rich and complete API analytics on your Treblle Dashboard amongst other useful info like auto-generated API docs.

Pretty cool right?

Masking fields

Treblle mask user passwords by default when logging responses your API made to requests. However you can tell Treblle to also mask additional fields. To do so, create a config/treblle.js file and then pass in the following:

// config/treblle.js
module.exports.treblle = {
  additionalFieldsToMask: ['key1', 'key2'], // optional
}

Take note to replace key1 and key with the actual field names you want masked.

Turning off errors

Maybe you don’t want to show errors your API faced during a requests(I don’t see why you won’t want to see them but…).

You can turn of erros by using the showErrors property and passing false to it. So in the same config/treblle.js file, you can do:

// config/treblle.js
module.exports.treblle = {
  showErrors: false // Optional, defaults to true
}

Conclusion

And that’s it, enjoy rich API monitoring and insights on your production Sails API with Treblle using the treblle-sails hook.

I have been using Treblle with the Sailscasts API and the insights are just great. Let me know if you gave it a try or have any questions.