APILensAPILens
frameworks/fastify

Fastify

v4 / v5

High-performance observability via Fastify's plugin system.

Registered as a Fastify plugin. Uses `addHook` internally — compatible with Fastify 4 and 5, encapsulation-aware.

The plugin sets `Symbol.for("skip-override") = true` so hooks apply to all routes regardless of scope.

Install

bash
npm install auto-api-observe

Quick Start

The minimal setup — import, configure, and you're observing.

fastify-quick-start.js
javascript
const fastify = require('fastify')({ logger: false });
const { fastifyObservability } = require('auto-api-observe');

await fastify.register(fastifyObservability, {
  apiKey: process.env.APILENS_KEY,
});

fastify.get('/users', async () => ({ users: [] }));
await fastify.listen({ port: 3000 });

Full Example

All available options with custom fields and real-world usage.

fastify-full.js
javascript
const fastify = require('fastify')({ logger: false });
const { fastifyObservability, addField } = require('auto-api-observe');

await fastify.register(fastifyObservability, {
  apiKey:                 process.env.APILENS_KEY,
  slowThreshold:          500,
  tags:                   { service: 'order-api', env: process.env.NODE_ENV },
  processMetrics:         60_000,
  captureUnhandledErrors: true,
});

fastify.get('/orders/:id', async (req, reply) => {
  addField('orderId', req.params.id);
  const order = await db.query('SELECT * FROM orders WHERE id = $1', [req.params.id]);
  return order.rows[0];
});

await fastify.listen({ port: 3000 });

What's Tracked Automatically

  • Route pattern via routeOptions.url
  • HTTP method, status code, latency
  • Request / response Content-Length
  • Client IP and User-Agent
  • DB queries (auto-instrumented)
  • Outbound HTTP calls

Options Reference

OptionTypeDefault
apiKeystring
slowThresholdnumber1000
skipRoutesstring[][]
tagsRecord{}
processMetricsnumber | false30000
sampleRatenumber1.0

Custom Fields

Attach any business context to the current request via AsyncLocalStorage — scoped per request, works the same on every framework.

javascript
const { addField } = require('auto-api-observe');

// In any route handler:
addField('userId',  req.user.id);
addField('plan',    req.user.plan);
addField('country', req.headers['cf-ipcountry']);

// Sensitive keys are auto-redacted before shipping:
addField('authorization', token); // → "[REDACTED]"
addField('password',      pass);  // → "[REDACTED]"

Ready to observe your Fastify app?

Sign up free — get your API key in under 30 seconds. No credit card required.

Get your free API key