FastAPI Template
A powerful CLI tool for generating production-ready FastAPI projects with best practices, integrated authentication, and flexible ORM options.
Features
- 🚀 Production Ready: Pre-configured with security, logging, and deployment best practices
- 🔐 Integrated Authentication: FastAPI-Users integration with JWT authentication
- 🗄️ Flexible ORM: Choose between SQLAlchemy (PostgreSQL) or Beanie (MongoDB)
- 🐳 Docker Support: Complete Docker setup with docker-compose
- 📦 Celery Integration: Background task processing
- 🧪 Testing Ready: Pre-configured testing setup
- 📊 API Documentation: Auto-generated OpenAPI/Swagger documentation
- 🎯 CLI Driven: Simple command-line interface for project generation
Quick Start
Installation
pip install fastapi-template-cli
Create a New Project
# Create an API-only project with SQLAlchemy
fastapi-template-cli new my-api --orm sqlalchemy --type api
# Create a modular project with MongoDB
fastapi-template-cli new my-app --orm beanie --type modular
# Create with project description
fastapi-template-cli new my-project --orm sqlalchemy --type modular \
--description "My awesome FastAPI project" --author "Your Name"
Project Types
API-Only Projects
- Lightweight FastAPI backend
- Database integration (SQLAlchemy or Beanie)
- FastAPI-Users authentication
- No frontend or background tasks
Modular Projects
- Complete backend with FastAPI
- Database integration
- FastAPI-Users authentication
- Celery for background tasks
- Redis for caching and task queue
- Docker setup with docker-compose
ORM Options
SQLAlchemy (PostgreSQL)
- Database: PostgreSQL
- ORM: SQLAlchemy 2.0 with async support
- Migrations: Alembic
- Connection: asyncpg driver
Beanie (MongoDB)
- Database: MongoDB
- ODM: Beanie (async MongoDB ODM)
- Driver: Motor
- Schema: Pydantic-based documents
Usage
Basic Commands
# List available templates
fastapi-template-cli list-templates
# Create a new project
fastapi-template-cli new myproject
# Show version
fastapi-template-cli version
Project Structure
Generated projects follow a clean architecture:
myproject/
├── app/
│ ├── api/
│ │ └── v1/
│ │ ├── api.py
│ │ └── endpoints/
│ │ └── users.py
│ ├── core/
│ │ ├── config.py
│ │ └── security.py
│ ├── db/
│ │ ├── session.py (SQLAlchemy) or mongo.py (Beanie)
│ │ └── base_class.py (SQLAlchemy)
│ ├── models/
│ │ └── user.py
│ ├── schemas/
│ │ └── user.py
│ ├── users.py (FastAPI-Users config)
│ └── main.py
├── docker/
│ ├── Dockerfile
│ └── docker-compose.yml (modular only)
├── alembic/ (SQLAlchemy only)
├── workers/ (modular only)
├── pyproject.toml
├── .env
└── .gitignore
Template Comparison
Modular Template
- Includes Redis as Celery broker and result backend
- Separate Celery worker for long-running tasks
- Celery Beat for scheduled tasks
- Complete frontend integration
- Production-ready Docker setup
API Template
- Lightweight FastAPI core
- Database-only backend
- Minimal dependencies
- Optimized for microservices
- Simplified Docker setup
Project Structure Details
API Template
e-commerce/
├── app/
│ ├── api/v1/
│ │ ├── api.py # Main API router
│ │ └── endpoints/
│ │ └── users.py # User endpoints
│ ├── core/
│ │ ├── config.py # Application configuration
│ │ ├── security.py # Security utilities
│ │ └── users.py # User management
│ ├── database/
│ │ ├── base.py # Database base setup
│ │ ├── base_class.py # Base model class
│ │ └── session.py # Database session
│ ├── models/
│ │ └── users.py # User models
│ ├── schemas/
│ │ └── user.py # Pydantic schemas
│ ├── users/
│ │ ├── dependencies.py # User dependencies
│ │ └── manager.py # User manager
│ └── main.py # FastAPI application
├── alembic/ # Database migrations
├── docker/ # Docker configuration
├── tests/ # Test files
├── .env.dev # Development environment
├── .env.prod # Production environment
└── pyproject.toml # Project dependencies
Modular Template
full-erp/
├── app/
│ ├── core/
│ │ ├── config.py # Application configuration
│ │ ├── security.py # Security utilities
│ │ └── users.py # User management
│ ├── database/
│ │ ├── base.py # Database base setup
│ │ ├── base_class.py # Base model class
│ │ └── session.py # Database session
│ ├── models/
│ │ └── users.py # User models
│ ├── routers/
│ │ └── test.py # Test endpoints
│ ├── schemas/
│ │ └── user.py # Pydantic schemas
│ ├── services/ # Business logic services
│ ├── users/
│ │ ├── dependencies.py # User dependencies
│ │ └── manager.py # User manager
│ ├── utils/ # Utility functions
│ ├── workers/
│ │ ├── celery_app.py # Celery configuration
│ │ └── tasks.py # Background tasks
│ └── main.py # FastAPI application
├── alembic/ # Database migrations
├── docker/ # Docker configuration
├── tests/ # Test files
├── .env.dev # Development environment
├── .env.prod # Production environment
├── docker-compose.dev.yml # Development Docker setup
├── docker-compose.prod.yml # Production Docker setup
└── pyproject.toml # Project dependencies
Development
SQLAlchemy Projects
-
Setup Database
cd myproject pip install -e . alembic upgrade head
-
Run Development Server
uvicorn app.main:app --reload
-
Create Database Migration
alembic revision --autogenerate -m "Add new table"
Beanie Projects
-
Setup MongoDB
cd myproject pip install -e . # MongoDB will auto-initialize on first connection
-
Run Development Server
uvicorn app.main:app --reload
Modular Projects (Docker)
-
Start All Services
cd myproject docker-compose up -d
-
Access Services
- API: http://localhost:8000
- API Docs: http://localhost:8000/docs
- MongoDB: localhost:27017 (Beanie)
- PostgreSQL: localhost:5432 (SQLAlchemy)
- Redis: localhost:6379
Configuration
Environment Variables
Create a .env file in your project root:
# Database
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/dbname # SQLAlchemy
MONGODB_URL=mongodb://localhost:27017/myproject # Beanie
# Security
SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Redis (modular)
REDIS_URL=redis://localhost:6379/0
# Email (optional)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-password
Database Configuration
SQLAlchemy (PostgreSQL)
# Install PostgreSQL
# Create database
createdb myproject
# Set DATABASE_URL
export DATABASE_URL=postgresql+asyncpg://user:password@localhost/myproject
Beanie (MongoDB)
# Install MongoDB
# MongoDB will create database on first connection
export MONGODB_URL=mongodb://localhost:27017/myproject
Deployment
Docker Deployment
For modular projects:
# Build and run
docker-compose -f docker-compose.dev.yaml up -d --build
# Stop services
docker-compose -f docker-compose.dev.yaml down
Production Deployment
-
Environment Variables
export SECRET_KEY=your-production-secret export DATABASE_URL=your-production-db-url
-
Gunicorn/Uvicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
-
Traefik Reverse Proxy Includes Traefik configuration in
docker-compose.prod.yml:services: traefik: image: traefik:v3.0 command: - --api.dashboard=true - --providers.docker=true - --entrypoints.web.address=:80 - --entrypoints.websecure.address=:443 - --certificatesresolvers.letsencrypt.acme.tlschallenge=true - --certificatesresolvers.letsencrypt.acme.email=your-email@domain.com - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes
- Add tests for new functionality
- Run tests:
pytest - Commit your changes:
git commit -am 'Add feature' - Push to the branch:
git push origin feature-name - Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_template_cli-2.0.3.tar.gz.
File metadata
- Download URL: fastapi_template_cli-2.0.3.tar.gz
- Upload date:
- Size: 37.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60dcc77fd0de2ec1f92262713edf3ff74051df2161e6bb9f23f44c09dec945e4
|
|
| MD5 |
edfa50fd91f83587b52af2a42908dcfa
|
|
| BLAKE2b-256 |
020bbe163faf5cac3c9fc37e19cbc05b0d14d9e04262bdae9ef093397b214229
|
File details
Details for the file fastapi_template_cli-2.0.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_template_cli-2.0.3-py3-none-any.whl
- Upload date:
- Size: 55.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24ad318945424f55b0c9c50b359009c82eba3396bee4d9aa694367b7c964a9f0
|
|
| MD5 |
e846b981c2e38a57fcd51c657fcece39
|
|
| BLAKE2b-256 |
bdef7ef230a2885ae8fc5de1e8dc00a3e36dd85e03128ce098a8804c6f096fe7
|