Skip to main content
Fastify

Learn how to integrate ApiTraffic with a Fastify application

Jason Fill avatar
Written by Jason Fill
Updated over a month ago

Install the SDK

Install ApiTraffic for Fastify via NPM. Run the following command:

npm i @apitraffic/fastify --save

IMPORTANT: Node 18+ required.

Once the package is installed, include in the main server file like this:

const fastify = require('fastify')();
const apiTraffic = require('@apitraffic/fastify');


Getting Started

Ensure you have already created a trial or paid account at apitraffic.io to get your API Key and Bucket ID. After you have those, there are two different ways you can add them to your application, via environment variables or directly in the middleware function call. The full list of configuration options can be found below. In this example we will use the function parameter option.

const apiTraffic = require('@apitraffic/fastify'); 

fastify.register(apiTraffic.middleware({
token: "[your-ingest-token]",
bucket: "[your-bucket-id]"
}));

That is it! Your inbound (and outbound) API requests will now be sent to the ApiTraffic servers for logging. Additionally, powerful workflows can now be setup to take action on matching requests.

Environment Variables

Example of setting the required variables as environment variables:

export API_TRAFFIC_TOKEN=[your-ingest-token]
export API_TRAFFIC_BUCKET=[your-bucket-id]

Function Parameters

Example of setting the required variables as function parameters:

{
token: "[your-ingest-token]",
bucket: "[your-bucket-id]"
}

Configuration Options

These methods are not mutually exclusive, if for whatever reason you need to set some as parameters and some as environment variables, it is ok they can be mixed.

If the same variable is set in both places, the parameters that are passed in will always supersede the environment variables.

Ingestion Token

This is a required parameter that authenticates your API with the requests to ApiTraffic. It is important that the token type is set as ingest when setting up your token in ApiTraffic, regular integration tokens will not work for sending traffic to the ingestion endpoint.

Function Parameter: token

Environment Variable: API_TRAFFIC_TOKEN

Bucket ID

This is a required parameter that indicates what bucket the data should be stored in. Buckets allow for more granular data segmentation.

Function Parameter: bucket

Environment Variable: API_TRAFFIC_BUCKET

Intercept Outbound Requests

An optional parameter that indicates if the SDK should be intercepting outbound HTTP requests. By default this is set to true but can be set to false if required.

Function Parameter: interceptOutbound

Environment Variable: API_TRAFFIC_INTERCEPT_OUTBOUND

Debug

An optional parameter that controls if the SDK will add console.log code while running. This can be useful if something is not operating as expected and more detail needs to be output.

Function Parameter: debug

Environment Variable: API_TRAFFIC_DEBUG


Sample Application

A fully functional sample application can be found in the SDK repository under examples/basic.

Did this answer your question?