Integrations / Frameworks / Fastify

Fastify

Track business events from Fastify route handlers. Fully typed, works with Fastify's schema validation and plugin system.


Install

bash
npm install logit fastify

Initialize

ts
import Fastify from "fastify";
import { init } from "logit";

const app = Fastify({ logger: true });
const logit = init({ token: process.env.LOGIT_API_KEY! });

Route with schema

ts
interface SignupBody {
  email: string;
  plan: "free" | "pro";
}

app.post<{ Body: SignupBody }>("/signup", {
  schema: {
    body: {
      type: "object",
      required: ["email", "plan"],
      properties: {
        email: { type: "string" },
        plan: { type: "string", enum: ["free", "pro"] },
      },
    },
  },
}, async (request, reply) => {
  const { email, plan } = request.body;

  // ... create user

  await logit.now("signups", {
    event: "New user signed up",
    description: `${email} joined on ${plan}`,
    icon: "👤",
    notify: true,
    tags: { plan },
    metadata: { email, ip: request.ip },
  });

  return reply.code(201).send({ ok: true });
});

As a plugin

ts
import type { FastifyPluginAsync } from "fastify";
import { init, type LogitClient } from "logit";

declare module "fastify" {
  interface FastifyInstance {
    logit: LogitClient;
  }
}

const logitPlugin: FastifyPluginAsync = async (app) => {
  app.decorate("logit", init({ token: process.env.LOGIT_API_KEY! }));
};

export default logitPlugin;

Ready to connect Fastify?

7-day free trial. No credit card required.

Start free