TypeScript / Fastify

Instrument Fastify APIs.

Use native Fastify lifecycle hooks for customer-aware analytics and automatic shutdown flushing.

Fastify 4–5Package v0.2.0Production ready
01

Quickstart

Install the public npm packages.

The framework adapter uses @apirelio/core for the common event contract and bounded asynchronous transport.

Terminalbash
npm install @apirelio/fastify @apirelio/core
02

Framework integration

Enable automatic request tracking.

The plugin times requests in onRequest, remembers framework errors in onError and captures the final route and status in onResponse.

server.tstypescript
import Fastify from 'fastify'
import apirelioFastify from '@apirelio/fastify'

const app = Fastify()
await app.register(apirelioFastify, {
  apiKey: process.env.APIRELIO_API_KEY ?? '',
  service: 'billing-api',
  environment: 'production',
  includeRoutes: ['/api/**'],
  excludeRoutes: ['/api/health', '/api/internal/**'],
  resolveCustomer: request => request.user ? {
    id: String(request.user.companyId),
    plan: request.user.companyPlan,
  } : null,
})
03

Customer context

Attach the account and application.

Use stable external IDs from your authentication model. Customer names and plans can evolve without breaking analytics history.

Plugin optionstypescript
resolveApplication: request => ({
  id: String(request.headers['x-application-id']),
}),
resolveErrorCode: (_request, _reply, error) =>
  error && 'code' in error ? String(error.code) : null
04

Reliability

Flush safely during shutdown.

Fastify onClose automatically flushes the bounded queue when the server closes.

includeRoutes accepts exact paths or prefixes ending in /**. Explicit excludeRoutes always take precedence, keeping health checks, internal endpoints and presentation routes out of API analytics.

Non-blocking capture

The request only appends to a bounded in-memory queue. Network delivery, retry and batching happen outside the observed response path.

05

Data boundary

Operational signals only.

Request and response bodies, authorization values, cookies, query values, email addresses and client IP addresses are not captured. Custom scalar metadata must be explicitly allow-listed.

Server credentials only

Keep the ingestion key in server environment configuration. Do not expose it through client bundles or public runtime config.