A production-ready FastAPI boilerplate with PostgreSQL, SQLAlchemy (async), JWT authentication, and comprehensive documentation. Perfect for building scalable APIs with modern Python practices.
A modern, scalable FastAPI boilerplate that provides everything you need to build production-ready APIs. Features async database operations, JWT authentication, comprehensive logging, Docker support, and extensive documentation.
- FastAPI - Modern, fast web framework for building APIs
- PostgreSQL - Robust relational database
- SQLAlchemy 2.0 - Async ORM with type safety
- Alembic - Database migrations
- JWT Authentication - Secure token-based authentication
- Pydantic - Data validation and serialization
- Docker - Containerized deployment
- Comprehensive Logging - Structured logging configuration
- CORS Support - Cross-origin resource sharing
- Health Checks - Application monitoring endpoints
- Python 3.11
- FastAPI 0.104+
- SQLAlchemy 2.0+
- PostgreSQL
- Alembic
- Pydantic 2.5+
- Uvicorn
- Docker
fastapi-starter/
βββ alembic/ # Database migrations
β βββ versions/ # Migration files
β βββ env.py # Alembic environment
β βββ script.py.mako # Migration template
βββ src/
β βββ api/ # API routes and dependencies
β β βββ dependencies.py # Authentication and dependencies
β β βββ v1/ # API version 1
β β βββ user.py # User endpoints
β βββ core/ # Core configuration
β β βββ config.py # Application settings
β β βββ logger.py # Logging configuration
β β βββ security.py # JWT authentication
β βββ db/ # Database configuration
β β βββ postgres.py # PostgreSQL connection
β βββ models/ # SQLAlchemy models
β β βββ user.py # User model
β βββ schemas/ # Pydantic schemas
β β βββ user.py # User schemas
β βββ services/ # Business logic
β βββ user.py # User service
βββ main.py # Application entry point
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose setup
βββ alembic.ini # Alembic configuration
βββ README.md # This file
- Python 3.11+
- PostgreSQL
- Docker (optional)
git clone <repository-url>
cd fastapi-starter
# Copy environment file
cp .env\ example .env
# Edit .env with your configuration
nano .env
pip install -r requirements.txt
# Create PostgreSQL database
createdb fastapi_starter
# Run migrations
alembic upgrade head
python main.py
The API will be available at:
- API Documentation: http://localhost:8000/docs
- ReDoc Documentation: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
# Build the image
docker build -t fastapi-starter .
# Run the container
docker run -p 8000:8000 --env-file .env fastapi-starter
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f app
The API uses JWT (JSON Web Tokens) for authentication. Include the token in the Authorization header:
Authorization: Bearer <your-jwt-token>
POST /api/v1/users/auth/login
- Login with emailPOST /api/v1/users/auth/register
- Register new user with email
GET /api/v1/users/me
- Get current user infoPUT /api/v1/users/me
- Update current userDELETE /api/v1/users/me
- Delete current user
GET /health
- Health checkGET /
- Root endpoint
curl -X POST "http://localhost:8000/api/v1/users/auth/register" \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com"}'
curl -X POST "http://localhost:8000/api/v1/users/auth/login" \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com"}'
curl -X GET "http://localhost:8000/api/v1/users/me" \
-H "Authorization: Bearer <your-token>"
curl -X PUT "http://localhost:8000/api/v1/users/me" \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{"email": "newemail@example.com"}'
Variable | Description | Default |
---|---|---|
PROJECT_NAME |
Application name | fastapi-starter |
PROJECT_HOST |
Host to bind to | 127.0.0.1 |
PROJECT_PORT |
Port to bind to | 8000 |
POSTGRES_USER |
PostgreSQL username | postgres |
POSTGRES_PASSWORD |
PostgreSQL password | postgres |
POSTGRES_SERVER |
PostgreSQL server | localhost |
POSTGRES_PORT |
PostgreSQL port | 5432 |
POSTGRES_DB |
PostgreSQL database | fastapi_starter |
SECRET_KEY |
JWT secret key | your-very-secret-and-long-key-that-is-hard-to-guess-at-least-32-characters |
ACCESS_TOKEN_EXPIRE_MINUTES |
Token expiration time | 43200 (30 days) |
alembic revision --autogenerate -m "Description of changes"
alembic upgrade head
alembic downgrade -1
pytest
pytest --cov=src
The application uses structured logging with the following levels:
- DEBUG: Detailed information for debugging
- INFO: General information about application flow
- WARNING: Warning messages for potential issues
- ERROR: Error messages for failed operations
- JWT tokens for authentication
- Password hashing (when implemented)
- CORS configuration
- Input validation with Pydantic
- SQL injection protection with SQLAlchemy
- Set
PROJECT_HOST=0.0.0.0
for external access - Configure proper CORS origins
- Use strong
SECRET_KEY
- Set up proper database credentials
- Configure logging for production
- Use async/await for database operations
- Implement connection pooling
- Add caching layer (Redis) if needed
- Monitor with health checks
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.
For support and questions:
- Create an issue in the repository
- Check the API documentation at
/docs
- Review the logs for debugging information
β Star this repository if you find it helpful!