FastAPI Starter Template
A complete FastAPI starter template with authentication, database integration, and best practices
Overview
This FastAPI starter template provides a solid foundation for building production-ready APIs. It includes authentication, database integration, comprehensive testing, and deployment configurations.
The template follows FastAPI best practices and includes everything you need to get started quickly while maintaining code quality and scalability.
Features
Core FastAPI Setup
- High Performance: Built on Starlette and Pydantic for maximum speed
- Automatic Documentation: Interactive API docs with Swagger UI and ReDoc
- Type Safety: Full Python type hints throughout the codebase
- Async Support: Native async/await support for high concurrency
Database Integration
- SQLAlchemy ORM: Powerful and flexible database toolkit
- Alembic Migrations: Database schema version control
- Connection Pooling: Optimized database connections
- Multiple Database Support: PostgreSQL, MySQL, SQLite
Authentication & Security
- JWT Authentication: Secure token-based authentication
- Password Hashing: Bcrypt for secure password storage
- CORS Configuration: Cross-origin resource sharing setup
- Security Headers: Built-in security middleware
Development Experience
- Hot Reload: Automatic server restart during development
- Comprehensive Testing: pytest with async test support
- Code Quality: Pre-configured linting and formatting
- Environment Management: Environment-based configuration
Quick Start
Prerequisites
Make sure you have the following installed:
- Python 3.8 or higher
- pip (Python package manager)
- Git
- PostgreSQL (optional, SQLite works for development)
Installation
-
Clone or download the template
1 2git clone https://github.com/pragmatic-ai-stack/fastapi-starter.git cd fastapi-starter -
Create virtual environment
1 2python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
Install dependencies
1pip install -r requirements.txt -
Set up environment variables
1 2cp .env.example .env # Edit .env with your configuration -
Initialize database
1alembic upgrade head -
Run the application
1uvicorn app.main:app --reload
Expected Output
|
|
Visit http://localhost:8000/docs to see the interactive API documentation.
Project Structure
|
|
Configuration
The template uses environment variables for configuration. Copy .env.example to .env and customize:
|
|
API Endpoints
Authentication
POST /api/v1/auth/login- User loginPOST /api/v1/auth/register- User registrationPOST /api/v1/auth/refresh- Refresh access token
Users
GET /api/v1/users/me- Get current userPUT /api/v1/users/me- Update current userGET /api/v1/users/- List users (admin only)
Health Check
GET /health- Application health status
Testing
Run the test suite:
|
|
Deployment
Docker Deployment
-
Build the image
1docker build -t fastapi-starter . -
Run with docker-compose
1docker-compose up -d
Production Deployment
For production deployment, consider:
- Using a production WSGI server (Gunicorn with Uvicorn workers)
- Setting up a reverse proxy (nginx)
- Configuring SSL certificates
- Setting up monitoring and logging
- Using a managed database service
Customization Guide
Adding New Endpoints
- Create a new router in
app/api/v1/ - Define Pydantic schemas in
app/schemas/ - Add database models in
app/models/ - Implement CRUD operations in
app/crud/ - Include router in
app/api/v1/api.py
Database Models
Add new SQLAlchemy models in app/models/:
|
|
Background Tasks
For background tasks, integrate Celery:
|
|
Support
- Documentation: FastAPI Documentation
- GitHub Issues: Report bugs or request features
- Community: Join our Discord
License
This template is released under the MIT License. See LICENSE for details.
Project Structure
alembic/:Database migration files
app/:Main application directory
app/api/:API route definitions
app/core/:Core configuration and security
app/crud/:Database CRUD operations
app/main.py:FastAPI application entry point
app/models/:SQLAlchemy database models
app/schemas/:Pydantic schemas
docker-compose.yml:Docker services configuration
requirements.txt:Python dependencies
tests/:Test suite
Customization Options
Authentication System
Include JWT-based authentication with user management
Database Integration
Set up SQLAlchemy with PostgreSQL or SQLite
Docker Configuration
Include Docker and docker-compose setup
Background Tasks
Add Celery for background job processing
API Rate Limiting
Include rate limiting middleware
Deployment Options
Heroku
Deploy to Heroku with PostgreSQL addon
- Install Heroku CLI
- Create new Heroku app
- Add PostgreSQL addon
- Configure environment variables
- Deploy with git push
Docker
Deploy using Docker containers
- Build Docker image
- Configure environment variables
- Run with docker-compose
- Set up reverse proxy (nginx)
AWS Lambda
Serverless deployment with Mangum
- Install Mangum adapter
- Configure serverless.yml
- Deploy with Serverless Framework