Skip to main content

A powerful CLI tool for generating production-ready FastAPI applications with SQLAlchemy and MongoDB support

Project description

FastAPI Template Logo

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 project 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

Template Comparison

Fullstack 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

Fullstack 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

  1. Setup Database

    cd myproject
    pip install -e .
    alembic upgrade head
    
  2. Run Development Server

    uvicorn app.main:app --reload
    
  3. Create Database Migration

    alembic revision --autogenerate -m "Add new table"
    

Beanie Projects

  1. Setup MongoDB

    cd myproject
    pip install -e .
    # MongoDB will auto-initialize on first connection
    
  2. Run Development Server

    uvicorn app.main:app --reload
    

Fullstack Projects (Docker)

  1. Start All Services

    cd myproject
    docker-compose up -d
    
  2. Access Services

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 login
  • POST /auth/jwt/logout - User logout
  • POST /auth/register - User registration
  • POST /auth/forgot-password - Request password reset
  • POST /auth/reset-password - Reset password

Users

  • GET /users/me - Get current user
  • PATCH /users/me - Update current user
  • GET /users/{id} - Get user by ID
  • GET /users - List users (admin only)

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

  1. Environment Variables

    export SECRET_KEY=your-production-secret
    export DATABASE_URL=your-production-db-url
    
  2. Gunicorn/Uvicorn

    gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
    
  3. Traefik Reverse Proxy Both templates 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

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes
  4. Add tests for new functionality
  5. Run tests: pytest
  6. Commit your changes: git commit -am 'Add feature'
  7. Push to the branch: git push origin feature-name
  8. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_template_cli-1.3.11.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_template_cli-1.3.11-py3-none-any.whl (52.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_template_cli-1.3.11.tar.gz.

File metadata

  • Download URL: fastapi_template_cli-1.3.11.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for fastapi_template_cli-1.3.11.tar.gz
Algorithm Hash digest
SHA256 62cf31ef5a1050ced691d6f7aabe4890bc17a51519c7bc3394596f2532f94a2f
MD5 5f82b084c1a7f21b7b3eee2a3d66da16
BLAKE2b-256 c80aba4980ab2ba1e065f3e9412c545b7e5d96745db9b227f116d8c078304ccc

See more details on using hashes here.

File details

Details for the file fastapi_template_cli-1.3.11-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_template_cli-1.3.11-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed1be8d32a2d2c7384d0155a570686f761abeff260ffbef4fdbb620d05d2b82
MD5 3702611d29db5866ea34064967fc0753
BLAKE2b-256 2e29757dbeea00911ebdb9b61c8e6baa75cae8200d1f8e2d80dfdbb9cda78417

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page