A modern, type-safe Fastify API starter with authentication, database integration, and TypeScript support. This was suppose to be me testing out how to build with node and fastify, and ended up becoming a Auth API!
- 🚀 Fastify 4.x with TypeScript
- 🔐 JWT Authentication
- 🗄️ PostgreSQL with Knex.js
- 📝 TypeScript for type safety
- 🔄 Hot reloading in development
- 🛡️ Rate limiting
- 🏗️ Modular architecture (plugins, services, controllers)
- Node.js 18 or higher
- PostgreSQL 12 or higher
- pnpm (recommended) or npm
- Clone the repository:
git clone https://github.com/yourusername/fastify-auth-starter.git
cd fastify-auth-starter- Install dependencies:
pnpm install- Create a
.envfile in the root directory with the following variables:
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=fastify_api
# JWT Configuration
JWT_SECRET=your-secret-key
# Server Configuration
PORT=3001
# Authentication Mode (jwt, api-key, or none)
AUTH_MODE=jwt- Start the database:
docker-compose up -d- Run migrations:
pnpm migrate- Start the development server:
pnpm devThe server will be running at http://localhost:3001 with hot reloading enabled.
├── src/
│ ├── config/ # Configuration files
│ ├── controllers/ # Request handlers
│ ├── plugins/ # Fastify plugins
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ ├── types/ # TypeScript type definitions
│ └── index.ts # Application entry point
├── migrations/ # Database migrations
├── seeds/ # Database seed files
├── dist/ # Compiled JavaScript (ignored in git)
└── docker-compose.yml # Docker configuration
pnpm dev- Start development server with hot reloadingpnpm build- Build TypeScript filespnpm start- Start production serverpnpm start:prod- Start production server with production environmentpnpm migrate- Run database migrationspnpm migrate:reset- Reset and rerun all migrationspnpm migrate:prod- Run migrations in production environment
POST /api/login- Login and get JWT token{ "username": "your_username", "password": "your_password" }
GET /api/users- Get all users (requires authentication)POST /api/users- Create a new user{ "username": "new_user", "email": "user@example.com", "password": "password123", "role": "user" }PUT /api/users/:id- Update a user (requires authentication)DELETE /api/users/:id- Delete a user (requires authentication)
The API supports three authentication modes, configured via the AUTH_MODE environment variable:
jwt(default) - JWT-based authenticationapi-key- API key authentication (requiresAPI_KEYin .env)none- No authentication (not recommended for production)
This project uses TypeScript for type safety. The source code is in the src directory, and the compiled JavaScript is output to the dist directory.
Key TypeScript features:
- Strict type checking
- Interface definitions for all data structures
- Type-safe database queries
- Type-safe request/response handling
Migrations are written in JavaScript with JSDoc type annotations for IDE support. To create a new migration:
pnpm knex migrate:make migration_nameSeed files are also in JavaScript with JSDoc type annotations. To create a new seed:
pnpm knex seed:make seed_nameFor production deployment:
- Set
NODE_ENV=production - Set
DATABASE_URLwith your production database connection string - Build the TypeScript files:
pnpm build- Start the production server:
pnpm start:prodMIT