FastAPI Starter Kit
A comprehensive, production-ready FastAPI starter template that includes everything you need to build robust APIs quickly and efficiently.
๐ Features
- FastAPI Framework: Modern, fast web framework for building APIs
- Authentication: JWT-based authentication with refresh tokens
- Database Integration: SQLAlchemy ORM with PostgreSQL support
- Redis Caching: Built-in Redis integration for performance
- API Documentation: Auto-generated Swagger/OpenAPI docs
- Docker Support: Containerized deployment ready
- Testing: Comprehensive test suite with pytest
- Security: CORS, rate limiting, and security headers
- Monitoring: Health checks and metrics endpoints
๐ฆ What's Included
Core Features
- User authentication and authorization
- Database models and migrations
- API versioning
- Request/response validation
- Error handling
- Logging configuration
Development Tools
- Hot reload development server
- Code formatting (Black)
- Linting (Flake8)
- Type checking (mypy)
- Pre-commit hooks
Deployment Ready
- Docker and docker-compose files
- Nginx configuration
- Environment-based configuration
- Production settings
๐ ๏ธ Quick Start
Prerequisites
- Python 3.8+
- PostgreSQL
- Redis (optional)
Installation
``bash
Clone the repository
git clone https://github.com/geniusdynamics/fastapi-starter.git
cd fastapi-starter
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activateInstall dependencies
pip install -r requirements.txtSet up environment variables
cp .env.example .envEdit .env with your configuration
Run database migrations
alembic upgrade headStart the development server
uvicorn app.main:app --reload `
The API will be available at
http://localhost:8000 and documentation at http://localhost:8000/docs.
๐ API Endpoints
Authentication
`http
POST /api/v1/auth/login
POST /api/v1/auth/register
POST /api/v1/auth/refresh
POST /api/v1/auth/logout
`
Users
`http
GET /api/v1/users/me
PUT /api/v1/users/me
DELETE /api/v1/users/me
`
Items (Example Resource)
`http
GET /api/v1/items
POST /api/v1/items
GET /api/v1/items/{id}
PUT /api/v1/items/{id}
DELETE /api/v1/items/{id}
`
๐๏ธ Project Structure
`
fastapi-starter/
โโโ app/
โ โโโ api/
โ โ โโโ v1/
โ โ โ โโโ endpoints/
โ โ โ โ โโโ auth.py
โ โ โ โ โโโ users.py
โ โ โ โ โโโ items.py
โ โ โ โโโ api.py
โ โโโ core/
โ โ โโโ config.py
โ โ โโโ security.py
โ โ โโโ database.py
โ โโโ models/
โ โ โโโ user.py
โ โ โโโ item.py
โ โโโ schemas/
โ โ โโโ user.py
โ โ โโโ item.py
โ โโโ services/
โ โ โโโ auth.py
โ โ โโโ user.py
โ โโโ main.py
โ โโโ __init__.py
โโโ tests/
โ โโโ conftest.py
โ โโโ test_auth.py
โ โโโ test_users.py
โโโ alembic/
โโโ docker/
โโโ docs/
โโโ .env.example
โโโ docker-compose.yml
โโโ Dockerfile
โโโ requirements.txt
โโโ README.md
`
๐ง Configuration
The application uses environment variables for configuration:
`bash
Database
DATABASE_URL=postgresql://user:password@localhost/dbname
Security
SECRET_KEY=your-secret-key-here
JWT_SECRET_KEY=your-jwt-secret
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
Redis (optional)
REDIS_URL=redis://localhost:6379
Email
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your-email@gmail.com
SMTP_PASSWORD=your-app-password
`
๐งช Testing
`bash
Run all tests
pytest
Run with coverage
pytest --cov=app --cov-report=html
Run specific test file
pytest tests/test_auth.py
Run tests in verbose mode
pytest -v
`
๐ Deployment
Using Docker
`bash
Build the image
docker build -t fastapi-starter .
Run with docker-compose
docker-compose up -d
Or run manually
docker run -p 8000:8000 fastapi-starter
`
Manual Deployment
`bash
Install production dependencies
pip install -r requirements.prod.txt
Set environment to production
export ENVIRONMENT=production
Run with gunicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
`
๐ Security Features
- JWT Authentication: Secure token-based authentication
- Password Hashing: Bcrypt password hashing
- CORS Protection: Configurable CORS settings
- Rate Limiting: API rate limiting to prevent abuse
- Input Validation: Pydantic models for request validation
- SQL Injection Protection: SQLAlchemy ORM protection
- HTTPS Enforcement: SSL/TLS encryption in production
๐ Monitoring
Health Checks
`http
GET /health - Overall health status
GET /health/db - Database health
GET /health/redis - Redis health
`
Metrics
`http
GET /metrics - Application metrics (Prometheus format)
`
๐ค Contributing
Fork the repository
Create a feature branch ( git checkout -b feature/amazing-feature)
Commit your changes ( git commit -m 'Add amazing feature')
Push to the branch ( git push origin feature/amazing-feature`)
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- FastAPI team for the incredible framework
- SQLAlchemy for the powerful ORM
- All contributors and the open source community
๐ Support
- Documentation: docs.geniusdynamics.com/fastapi-starter
- Issues: GitHub Issues
- Discussions: GitHub Discussions
_Built with ๐ by Genius Dynamics_