[go: up one dir, main page]

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)