FastifyJs SDK Guide
To integrate your FastifyJs application with APItoolkit, you need to use this SDK to monitor incoming traffic, aggregate the requests, and then send them to APItoolkit's servers. Kindly follow this guide to get started and learn about all the supported features of APItoolkit's FastifyJs SDK.
Prerequisites
Ensure you have already completed the first three steps of the onboarding guide.
Installation
Kindly run the command below to install the SDK:
npm install apitoolkit-fastify
# Or
yarn add apitoolkit-fastify
Configuration
Next, initialize APItoolkit in your application's entry point (e.g., index.js
), like so:
import Fastify from "fastify";
import APIToolkit from "apitoolkit-fastify";
const fastify = Fastify();
// Initialize the APItoolkit client
const apitoolkitClient = APIToolkit.NewClient({
fastify,
apiKey: "{ENTER_YOUR_API_KEY_HERE}",
debug: false,
tags: ["environment: production", "region: us-east-1"],
serviceVersion: "v2.0",
});
apitoolkitClient.init();
// END Initialize the APItoolkit client
fastify.get("/hello", function (request, reply) {
reply.send({ hello: "world" });
});
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
In the configuration above, only the apiKey
option is required, but you can add the following optional fields:
Option | Description |
---|---|
debug | Set to true to enable debug mode. |
tags | A list of defined tags for your services (used for grouping and filtering data on the dashboard). |
serviceVersion | A defined string version of your application (used for further debugging on the dashboard). |
redactHeaders | A list of HTTP header keys to redact. |
redactResponseBody | A list of JSONPaths from the request body to redact. |
redactRequestBody | A list of JSONPaths from the response body to redact. |
Tip
- The
fastify
instance is required to configure the SDK. - The
{ENTER_YOUR_API_KEY_HERE}
demo string should be replaced with the API key generated from the APItoolkit dashboard.
Redacting Sensitive Data
If you have fields that are sensitive and should not be sent to APItoolkit servers, you can mark those fields to be redacted (the fields will never leave your servers).
To mark a field for redacting via this SDK, you need to add some additional arguments to the apitoolkitClient
configuration options with paths to the fields that should be redacted. There are three arguments you can provide to configure what gets redacted, namely:
redactHeaders
: A list of HTTP header keys.redactRequestBody
: A list of JSONPaths from the request body.redactResponseBody
: A list of JSONPaths from the response body.
JSONPath is a query language used to select and extract data from JSON files. For example, given the following sample user data JSON object:
{
"user": {
"name": "John Martha",
"email": "[email protected]",
"addresses": [
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
],
"credit_card": {
"number": "4111111111111111",
"expiration": "12/28",
"cvv": "123"
}
}
}
Examples of valid JSONPath expressions would be:
JSONPath | Description |
---|---|
$.user.addresses[*].zip | In this case, APItoolkit will replace the zip field in all the objects of the addresses list inside the user object with the string [CLIENT_REDACTED] . |
$.user.credit_card | In this case, APItoolkit will replace the entire credit_card object inside the user object with the string [CLIENT_REDACTED] . |
Tip
To learn more about JSONPaths, please take a look at the official docs or use this JSONPath Evaluator to validate your JSONPath expressions.
You can also use our JSON Redaction Tool to preview what the final data sent from your API to APItoolkit will look like, after redacting any given JSON object.
Here's an example of what the configuration would look like with redacted fields:
import Fastify from "fastify";
import APIToolkit from "apitoolkit-fastify";
const fastify = Fastify();
const apiKey = "{ENTER_YOUR_API_KEY_HERE}";
const redactHeaders = ["content-type", "Authorization", "HOST"];
const redactRequestBody = ["$.user.email", "$.user.addresses"];
const redactResponseBody = ["$.users[*].email", "$.users[*].credit_card"];
const apitoolkitClient = APIToolkit.NewClient({
fastify,
apiKey,
redactHeaders,
redactRequestBody,
redactResponseBody,
});
apitoolkitClient.init();
fastify.listen({ port: 3000 }, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
Error Reporting
APItoolkit automatically detects different unhandled errors, API issues, and anomalies but you can report and track specific errors at different parts of your application. This will help you associate more detail and context from your backend with any failing customer request.
To manually report specific errors at different parts of your application (within the context of a web request handler), use the ReportError()
function, passing in the error
argument, like so:
import Fastify from "fastify";
import axios from "axios";
import APIToolkit, { ReportError } from "apitoolkit-fastify";
const fastify = Fastify();
const apitoolkitClient = APIToolkit.NewClient({
fastify,
apiKey: "{ENTER_YOUR_API_KEY_HERE}",
});
apitoolkitClient.init();
fastify.get("/", async (request, reply) => {
try {
const response = await observeAxios(axios).get(
baseURL + "/non-existing-endpoint"
);
} catch (error) {
// Report the error to APItoolkit
ReportError(error);
}
});
Monitoring Outgoing Requests
Outgoing requests are external API calls you make from your API. By default, APItoolkit monitors all requests users make from your application and they will all appear in the API Log Explorer page. However, you can separate outgoing requests from others and explore them in the Outgoing Integrations page, alongside the incoming request that triggered them.
The monitorAxios
to the defineConfig
configuration options in the config/apitoolkit.js|ts
file, like so:import Fastify from "fastify";
import axios from "axios";
import APIToolkit from "apitoolkit-fastify";
const fastify = Fastify();
const apitoolkitClient = APIToolkit.NewClient({
fastify,
apiKey: "{ENTER_YOUR_API_KEY_HERE}",
monitorAxios: axios,
});
apitoolkitClient.init();
fastify.get("/", async (request, reply) => {
// This axios request gets monitored
const res = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
return res.data;
});
observeAxios()
function, like so:import Fastify from "fastify";
import axios from "axios";
import APIToolkit, { observeAxios } from "apitoolkit-fastify";
const fastify = Fastify();
const apitoolkitClient = APIToolkit.NewClient({
fastify,
apiKey: "{ENTER_YOUR_API_KEY_HERE}",
});
apitoolkitClient.init();
fastify.get("/", async (request, reply) => {
const res = await observeAxios(axios).get(baseURL + "/users/user1234");
return res.data;
});
observeAxios
function above accepts a required axios
instance and the following optional arguments:Option Description pathWildCard
The url_path
string for URLs with path parameters.redactHeaders
A list of HTTP header keys to redact. redactResponseBody
A list of JSONPaths from the request body to redact. redactRequestBody
A list of JSONPaths from the response body to redact.
Explore the FastifyJS SDK