DEV Community

Cover image for Fastify, Prisma, Zod & Swagger
Azzam Faraj
Azzam Faraj

Posted on

Fastify, Prisma, Zod & Swagger

Fastify Prisma Boilerplate — A Productive API Starter

As someone who frequently spins up backend projects, I got tired of repeating the same setup. So I built a boilerplate that helps me get started faster with less boilerplate and more actual work.

This project combines Fastify, Prisma, Zod, and TypeScript into a lightweight, type-safe, and efficient REST API starter — with auto-generated routes, a CLI, and built-in validation.

👉 GitHub Repo


Features

  • Fastify for high-performance APIs
  • 🐘 Prisma for type-safe DB access (PostgreSQL)
  • Zod for schema validation and typing
  • 🔒 End-to-end type safety with fastify-type-provider-zod
  • 📝 Swagger auto-docs via @fastify/swagger
  • 🛠 CLI to scaffold routes
  • ♻️ Auto-registration of route files
  • 🌐 .env support via dotenv

Getting Started

git clone https://github.com/AZZIE2000/fastify-prisma-boilerplate my-api
cd my-api
npm install
cp .env.example .env     # Set DATABASE_URL
npx prisma migrate dev   # Apply DB schema
npm run dev              # Start server
Enter fullscreen mode Exit fullscreen mode
  • API: http://localhost:8080
  • Docs: http://localhost:8080/docs

CLI: Generate Routes Automatically

The project includes a built-in CLI that creates new route files for you, based on what type of endpoints you want.

Usage

npm run route <name> <crud combo>
Enter fullscreen mode Exit fullscreen mode

Example

npm run route product crud
Enter fullscreen mode Exit fullscreen mode

This will generate src/routes/product.ts with the following endpoints:

Letter Endpoint
c POST /api/product
r GET /api/product + GET /:id
u PATCH /:id
d DELETE /:id

You can customize which APIs to include. For example:

npm run route product cd
Enter fullscreen mode Exit fullscreen mode

Will generate only the create and delete routes. This helps you avoid unused code and stay focused.


Auto-Registration of Routes

Any file you place in src/routes is automatically registered by the server.
The filename becomes the API path prefix. For example:

src/routes/user.ts → /api/user
Enter fullscreen mode Exit fullscreen mode

You don’t need to import or register anything manually. The server handles it for you.


Type Safety & Validation

Each route uses Zod for schema validation. When combined with fastify-type-provider-zod, your schemas automatically become your TypeScript types.

This means:

  • Requests are validated at runtime
  • Your handlers get fully typed request.body, request.params, etc.
  • No need to manually define TypeScript interfaces for every route

Project Structure

.
├── prisma/             # Prisma schema
├── src/
│   ├── cli/            # Route generator logic
│   ├── plugins/        # Fastify plugins (e.g. db)
│   ├── routes/         # API endpoints
│   ├── utils/          # Shared utilities
│   └── index.ts        # Server entry
Enter fullscreen mode Exit fullscreen mode

Scripts

  • npm run dev – Start dev server
  • npm run build – Build TypeScript
  • npm run start – Start compiled server
  • npm run route <name> <crud> – Generate route file

This boilerplate helps reduce setup time and lets you focus on building features — with structure, type safety, and simplicity built in.

👉 https://github.com/AZZIE2000/fastify-prisma-boilerplate

Top comments (0)