Skip to content

Fastify Cheatsheet

High-performance Node.js web framework with low overhead.

01

Getting Started

Hello Server

Fastify auto-serializes JSON responses.

fastify
import Fastify from "fastify";
const fastify = Fastify();

fastify.get("/", async () => {
  return { hello: "world" };
});

fastify.listen({ port: 3000 });

Schema Validation

JSON Schema for fast validation.

fastify
fastify.post("/user", {
  schema: {
    body: {
      type: "object",
      required: ["name"],
      properties: {
        name: { type: "string" }
      }
    }
  }
}, async (req) => req.body);

Plugins

Encapsulated plugins with prefixes.

fastify
fastify.register(async (instance) => {
  instance.get("/plugin", async () => "ok");
}, { prefix: "/api" });

Was this helpful?