Software Development

Best practices for API versioning in FastAPI without duplicating code?

LA Asked by Larry Simmons · 15-08-2025
0 upvotes 17,541 views 0 comments
The question

I'm managing a project that requires supporting both v1 and v2 of our API simultaneously. I'm using FastAPI APIRouter. What is the cleanest way to handle versioning so that common logic isn't duplicated across folders, but I can still have distinct schemas for each version?

3 answers

0
PA
Answered on 17-08-2025

The most maintainable approach in FastAPI is to use directory-based versioning for your routers (e.g., api/v1/endpoints/ and api/v2/endpoints/). For the logic, move your "business logic" into a separate services/ layer that both versions call. This way, the routers only handle the Pydantic schema validation and status codes. If v2 requires a new field, you just update the v2 Pydantic model and the service function, while v1 remains tied to the old model. This keeps your code DRY while allowing the public contract to evolve.

0
SE
Answered on 18-08-2025

What about header-based versioning? Is it possible to use FastAPI dependencies to route a request to a specific function based on the Accept-Version header instead of using URL prefixes?

LA 19-08-2025

It is possible but much harder to document with Swagger/OpenAPI. FastAPI's automatic docs work best with URL prefixes (/v1, /v2). If you use headers, you often have to write a custom OpenAPI generator script to show all versions clearly to your frontend team.

0
TH
Answered on 20-08-2025

I always use the URL prefix method. It makes it very clear in the logs which version of the API is being hit the most.

PA 21-08-2025

Good point, Theresa. Observability is a huge factor when deciding on an architecture for long-term projects.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session