A production-ready FastAPI template with MongoDB integration, JWT authentication, and LLM support. Perfect for building scalable APIs with modern Python practices.
- FastAPI Backend - Modern, fast web framework for building APIs
- MongoDB Integration - Async database operations with Motor
- JWT Authentication - Secure user authentication and authorization
- LLM Integration - Support for OpenAI and Gemini APIs
- Docker Support - Containerized development and deployment
- Comprehensive Testing - Pytest setup with async support
- Production Ready - Security, CORS, environment configuration
- API Documentation - Automatic Swagger/OpenAPI docs at
/docs
βββ app/
β βββ api/
β β βββ deps.py # Dependencies and authentication
β β βββ v1/
β β βββ api.py # Main API router
β β βββ routes/
β β βββ auth.py # Authentication endpoints
β β βββ user.py # User management endpoints
β β βββ llm.py # LLM integration endpoints
β βββ core/
β β βββ config.py # Settings and configuration
β β βββ security.py # JWT and password utilities
β βββ db/
β β βββ mongodb.py # Database connection
β β βββ crud/
β β βββ user.py # User CRUD operations
β βββ models/
β β βββ user.py # Pydantic models
β βββ schemas/
β β βββ auth.py # Request/response schemas
β βββ services/
β βββ auth_service.py # Authentication business logic
β βββ llm_client.py # LLM integration service
βββ tests/
β βββ conftest.py # Pytest configuration
β βββ test_auth.py # Authentication tests
β βββ test_users.py # User endpoint tests
βββ main.py # FastAPI application entry point
βββ requirements.txt # Python dependencies
βββ Dockerfile # Production Docker image
βββ docker-compose.yml # Local development setup
βββ env.example # Environment variables template
βββ README.md # This file
- Python 3.11+
- MongoDB (local or cloud)
- Docker & Docker Compose (optional)
-
Clone the repository
git clone https://github.com/krishnakamalbaishnab/TemplatesFastAPI.git cd TemplatesFastAPI
-
Set up environment variables
cp env.example .env # Edit .env with your configuration
-
Install dependencies
pip install -r requirements.txt
-
Start MongoDB (if using local MongoDB)
# Using Docker docker run -d -p 27017:27017 --name mongodb mongo:7.0 # Or install MongoDB locally
-
Run the application
uvicorn main:app --reload
-
Access the API
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
-
Start all services
docker-compose up -d
-
Access services
- FastAPI: http://localhost:8000
- MongoDB Express: http://localhost:8081 (admin/admin123)
-
View logs
docker-compose logs -f app
Copy env.example
to .env
and configure:
# Security
SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30
# MongoDB
MONGODB_URL=mongodb://localhost:27017
MONGODB_DB_NAME=fastapi_template
# LLM Settings
OPENAI_API_KEY=your-openai-api-key
GEMINI_API_KEY=your-gemini-api-key
LLM_PROVIDER=openai # or "gemini"
# CORS
BACKEND_CORS_ORIGINS=["http://localhost:3000"]
POST /api/v1/auth/register
- Register new userPOST /api/v1/auth/login
- Login with form dataPOST /api/v1/auth/login-json
- Login with JSON
GET /api/v1/users/me
- Get current user (authenticated)PUT /api/v1/users/me
- Update current user (authenticated)GET /api/v1/users/
- Get all users (superuser only)GET /api/v1/users/{user_id}
- Get user by ID (superuser only)PUT /api/v1/users/{user_id}
- Update user (superuser only)DELETE /api/v1/users/{user_id}
- Delete user (superuser only)
POST /api/v1/llm/generate
- Generate text (authenticated)POST /api/v1/llm/chat
- Chat completion (authenticated)GET /api/v1/llm/models
- Get available models (authenticated)
Run the test suite:
# Run all tests
pytest
# Run with coverage
pytest --cov=app
# Run specific test file
pytest tests/test_auth.py
# Run with verbose output
pytest -v
# Build the image
docker build -t fastapi-template .
# Run the container
docker run -p 8000:8000 --env-file .env fastapi-template
-
Build production image
docker build -t fastapi-template:prod .
-
Run with production settings
docker run -d \ --name fastapi-app \ -p 8000:8000 \ --env-file .env.prod \ fastapi-template:prod
- JWT Authentication - Secure token-based authentication
- Password Hashing - BCrypt password hashing
- CORS Protection - Configurable CORS settings
- Input Validation - Pydantic model validation
- Environment Configuration - Secure configuration management
- 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
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI - The web framework used
- Motor - Async MongoDB driver
- Pydantic - Data validation
- Python-Jose - JWT implementation
If you have any questions or need help, please open an issue on GitHub.
Made with β€οΈ for the FastAPI community