A powerful CLI tool for generating production-ready FastAPI applications with SQLAlchemy and MongoDB support
Project description
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 (fullstack projects)
- ๐งช 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 new my-api --orm sqlalchemy --type api
# Create a fullstack project with MongoDB
fastapi-template new my-app --orm beanie --type fullstack
# Create with custom description
fastapi-template new my-project --orm sqlalchemy --type fullstack \
--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
Fullstack 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 list-templates
# Create a new project
fastapi-template new myproject
# Show version
fastapi-template 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 (fullstack only)
โโโ alembic/ (SQLAlchemy only)
โโโ workers/ (fullstack only)
โโโ pyproject.toml
โโโ .env
โโโ .gitignore
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
Fullstack 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 (fullstack)
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
API Endpoints
Generated projects include these endpoints:
Authentication
POST /auth/jwt/login- User loginPOST /auth/jwt/logout- User logoutPOST /auth/register- User registrationPOST /auth/forgot-password- Request password resetPOST /auth/reset-password- Reset password
Users
GET /users/me- Get current userPATCH /users/me- Update current userGET /users/{id}- Get user by IDGET /users- List users (admin only)
Health Check
GET /health- API health statusGET /- Welcome message
Testing
Generated projects include testing setup:
# Run tests
pytest
# Run with coverage
pytest --cov=app tests/
# Run specific test file
pytest tests/test_users.py
Deployment
Docker Deployment
For fullstack projects:
# Build and run
docker-compose up -d --build
# View logs
docker-compose logs -f
# Stop services
docker-compose 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
-
Nginx Reverse Proxy
server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
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
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
Changelog
v1.0.0
- Initial release
- SQLAlchemy and Beanie support
- API-only and fullstack project types
- FastAPI-Users integration
- Docker support
- CLI tool
Project details
Release history Release notifications | RSS feed
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-1.2.5.tar.gz.
File metadata
- Download URL: fastapi_template_cli-1.2.5.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11986f62cadfeccc415f801bca040320d6bfcfa4400b7b03f5f77dd6e468b1b4
|
|
| MD5 |
1bda4b24b750653c937f390c1993b49d
|
|
| BLAKE2b-256 |
dc9a9e416a8a8e33e849198e04e59e5bff876ea1da98e6c6be504cc3289c9186
|
File details
Details for the file fastapi_template_cli-1.2.5-py3-none-any.whl.
File metadata
- Download URL: fastapi_template_cli-1.2.5-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b12718ac638e50a76937f5e6ca5684273f4781490bbfcc2ebf1557351c8ab7d
|
|
| MD5 |
c46cb32416c3319b06f88d705d2693c4
|
|
| BLAKE2b-256 |
819a26bcd1718c7a48e7e345531728168d434b72b5fe6c76a345dcdda5cd5a1d
|