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
- π
.envsupport viadotenv
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
- 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>
Example
npm run route product crud
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
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
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
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)