Skip to content

hstm/fastify-sample-api

Repository files navigation

Fastify API starter

A production-ready REST API built with Fastify, TypeScript, Prisma, and PostgreSQL. Features API key authentication, health monitoring, and comprehensive test coverage.

Tech Stack

  • Framework: Fastify
  • Language: TypeScript
  • Database: PostgreSQL with Prisma ORM
  • Testing: Vitest
  • Authentication: API Key-based
  • Documentation: Swagger/OpenAPI

Features

  • Fast and efficient REST API with Fastify
  • Type-safe database queries with Prisma
  • API key authentication with public route support
  • Health check endpoint for monitoring
  • Comprehensive test suite with real database testing
  • Auto-generated API documentation
  • CORS configuration
  • Structured logging

Prerequisites

  • Node.js 18+
  • PostgreSQL
  • npm or yarn

Installation

# Clone the repository
git clone https://github.com/hstm/fastify-sample-api.git
cd fastify-backend

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your database credentials

# Generate Prisma client
npx prisma generate

# Run migrations
npx prisma migrate dev

Development

# Start development server with hot reload
npm run dev

# Run tests
npm test

# Run tests in CI mode
npm run test:run

# Build for production
npm run build

# Start production server
npm start

Project structure

src/
├── routes/          # API route handlers
├── plugins/         # Fastify plugins (auth, prisma, etc.)
├── tests/           # Test files
├── utils/           # Utility functions
├── app.ts           # Application setup
└── server.ts        # Server entry point

API Documentation

Once the server is running, access the interactive API documentation at:

Swagger UI: http://localhost:3000/docs

Public Endpoints

The following endpoints are publicly accessible without authentication:

GET / - Welcome message
GET /health - Health check
GET /docs - API documentation

About

Fastify API starter with authentication and testing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published