A comprehensive demonstration of API versioning strategies in FastAPI, showcasing three different approaches to help developers choose the right architecture for their needs.
This template demonstrates the evolution of API versioning from basic to advanced implementations, helping developers:
- Understand different versioning strategies
- Maintain backward compatibility
- Scale APIs effectively
- Choose the right approach based on project size
fastapi-template/
├── proj_1/ # Basic API versioning
│ └── main.py # All-in-one implementation
├── proj_2/ # Structured versioning
│ └── app/
│ ├── controllers/ # Route handlers
│ ├── services/ # Business logic
│ └── models/ # Data schemas
├── proj_3/ # Full modular versioning
│ └── app/
│ ├── controllers/
│ │ ├── v1/ # Version 1 routes
│ │ └── v2/ # Version 2 routes
│ ├── services/
│ │ ├── v1/ # Version 1 business logic
│ │ └── v2/ # Version 2 business logic
│ └── models/ # Shared data models
└── main.py # Example of problematic versioning
- Clone and Setup
git clone https://github.com/rahulsamant37/FASTAPI_versioning-Template.git
cd FASTAPI_versioning-Template
uv venv
source .venv/bin/activate # On Windows: .\.venv\Scripts\activate- Install Dependencies
uv add fastapi- Run Any Example
# Basic versioning (Project 1)
uvicorn proj_1.main:app --reload
# Structured versioning (Project 2)
uvicorn proj_2.app.main:app --reload
# Full modular versioning (Project 3)
uvicorn proj_3.app.main:app --reload| Method | Endpoint | Description | Response |
|---|---|---|---|
| GET | /users |
List all users | List[UserV1] |
| GET | /users/{id} |
Get user by ID | UserV1 |
| Method | Endpoint | Description | Response |
|---|---|---|---|
| GET | /users |
List all users with email | List[UserV2] |
| GET | /users/{id} |
Get user by ID with email | UserV2] |
- Single file implementation
- Uses FastAPI's
APIRouter - Suitable for: Small APIs, Prototypes
- Pros: Simple, Quick to implement
- Cons: Limited scalability
- MVC-like architecture
- Shared service layer
- Suitable for: Medium-sized APIs
- Pros: Good organization, Maintainable
- Cons: Some version coupling
- Complete version isolation
- Independent services per version
- Suitable for: Large APIs, Enterprise
- Pros: Highly maintainable, Scalable
- Cons: More complex setup
python = ">=3.12"
fastapi = ">=0.115.12"
pydantic = ">=2.0.0"
uvicorn = ">=0.24.0"
email-validator = ">=2.0.0" # For email validation- ✅ Type-safe with Pydantic models
- ✅ Async/await support
- ✅ OpenAPI documentation
- ✅ Dependency injection
- ✅ Clear version boundaries
- API Documentation: Available at
/docs(Swagger) and/redoc(ReDoc) - Version Management:
- V1: Basic user info (id, name)
- V2: Extended user info (id, name, email)
-
Version Prefixing
- Clear URL versioning (
/api/v1,/api/v2) - Consistent version naming
- Clear URL versioning (
-
Code Organization
- Separation of concerns
- Clear module boundaries
- Version-specific services
-
Type Safety
- Pydantic models for validation
- Comprehensive type hints
- Version-specific schemas
-
Backward Compatibility
- Maintained across versions
- Clear upgrade paths
- Version-specific responses
- Fork the repository
- Create your 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.