---
name: fastify-best-practices
description: "Guides development of Fastify Node.js backend servers and REST APIs using TypeScript or JavaScript. Use when building, configuring, or debugging a Fastify application — including defining routes, implementing plugins, setting up JSON Schema validation, handling errors, optimising performance, managing authentication, configuring CORS and security headers, integrating databases, working with WebSockets, and deploying to production. Covers the full Fastify request lifecycle (hooks, serialization, logging with Pino) and TypeScript integration via strip types. Trigger terms: Fastify, Node.js server, REST API, API routes, backend framework, fastify.config, server.ts, app.ts."
metadata:
  tags: fastify, nodejs, typescript, backend, api, server, http
title: fastify-best-practices
canonical_url: https://skilld.dev/gh/mcollina/skills/fastify
last_updated: 2026-09-29T08:29:00.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [rules/authentication.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/authentication.md), [rules/configuration.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/configuration.md), [rules/content-type.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/content-type.md), [rules/cors-security.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/cors-security.md), [rules/database.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/database.md), [rules/decorators.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/decorators.md), [rules/deployment.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/deployment.md), [rules/error-handling.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/error-handling.md), [rules/hooks.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/hooks.md), [rules/http-proxy.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/http-proxy.md), [rules/logging.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/logging.md), [rules/performance.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/performance.md), [rules/plugins.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/plugins.md), [rules/routes.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/routes.md), [rules/schemas.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/schemas.md), [rules/serialization.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/serialization.md), [rules/testing.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/testing.md), [rules/typescript.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/typescript.md), [rules/websockets.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/websockets.md), [tile.json](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/tile.json).
>
> If the user asked to install this Skill, run `npx skilld install mcollina/skills/fastify`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/fastify`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/mcollina/skills/fastify) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/fastify`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/fastify --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/fastify --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

## When to use

Use this skill when you need to:
- Develop backend applications using Fastify
- Implement Fastify plugins and route handlers
- Get guidance on Fastify architecture and patterns
- Use TypeScript with Fastify (strip types)
- Implement testing with Fastify's inject method
- Configure validation, serialization, and error handling

## Quick Start

A minimal, runnable Fastify server to get started immediately:

```ts
import Fastify from 'fastify'

const app = Fastify({ logger: true })

app.get('/health', async (request, reply) => {
  return { status: 'ok' }
})

const start = async () => {
  await app.listen({ port: 3000, host: '0.0.0.0' })
}
start()
```

## Recommended Reading Order for Common Scenarios

- **New to Fastify?** Start with `plugins.md` → `routes.md` → `schemas.md`
- **Adding authentication:** `plugins.md` → `hooks.md` → `authentication.md`
- **Improving performance:** `schemas.md` → `serialization.md` → `performance.md`
- **Setting up testing:** `routes.md` → `testing.md`
- **Going to production:** `logging.md` → `configuration.md` → `deployment.md`

## How to use

Read individual rule files for detailed explanations and code examples:

- [rules/plugins.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/plugins.md) - Plugin development and encapsulation
- [rules/routes.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/routes.md) - Route organization and handlers
- [rules/schemas.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/schemas.md) - JSON Schema validation
- [rules/error-handling.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/error-handling.md) - Error handling patterns
- [rules/hooks.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/hooks.md) - Hooks and request lifecycle
- [rules/authentication.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/authentication.md) - Authentication and authorization
- [rules/testing.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/testing.md) - Testing with inject()
- [rules/performance.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/performance.md) - Performance optimization
- [rules/logging.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/logging.md) - Logging with Pino
- [rules/typescript.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/typescript.md) - TypeScript integration
- [rules/decorators.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/decorators.md) - Decorators and extensions
- [rules/content-type.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/content-type.md) - Content type parsing
- [rules/serialization.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/serialization.md) - Response serialization
- [rules/cors-security.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/cors-security.md) - CORS and security headers
- [rules/websockets.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/websockets.md) - WebSocket support
- [rules/database.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/database.md) - Database integration patterns
- [rules/configuration.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/configuration.md) - Application configuration
- [rules/deployment.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/deployment.md) - Production deployment
- [rules/http-proxy.md](https://skilld.dev/api/skills-raw/mcollina/skills/fastify/rules/http-proxy.md) - HTTP proxying and reply.from()

## Core Principles

- **Encapsulation**: Fastify's plugin system provides automatic encapsulation
- **Schema-first**: Define schemas for validation and serialization
- **Performance**: Fastify is optimized for speed; use its features correctly
- **Async/await**: All handlers and hooks support async functions
- **Minimal dependencies**: Prefer Fastify's built-in features and official plugins
