[QUESTION] recommended way to do API versioning
See original GitHub issueDescription
Is there a recommended way to do API versioning. I know I could use APIRouter
and keep all the files for a version in separate subdirectories but that doesn’t seem very DRY.
Is there a way to say “version 1.1 looks just like version 1.0 except in these certain cases”? And save APIRouter
for major version changes or rewrites?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:26
- Comments:10 (6 by maintainers)
Top Results From Across the Web
API Versioning Do's and Don'ts. It's never too ... - Bits and Pieces
Specify the version as part of the query parameters. If you want to keep the version in the URL but avoid the above...
Read more >rest - Best practices for API versioning? - Stack Overflow
The easiest for you is to start from scratch every time, which is when putting the version in the URL makes most sense....
Read more >Everything You Need to Know About API Versioning
API versioning is a way to segue between releases smoothly. It's the best way to sundown an asset or endpoint, as you can...
Read more >How to Version a REST API - freeCodeCamp
API versioning is the practice of transparently managing changes to your API. Versioning is effective communication around changes to your API, ...
Read more >REST APIs And Their Versioning – Interview Question
Why Do You Need Versioning? You need to version the APIs when you make breaking changes. With change in requirements, we might have...
Read more >Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve implemented something along the lines of what @dmontagu suggested in this comment: https://github.com/tiangolo/fastapi/issues/200#issuecomment-525126712 it’s up on pypi here: https://pypi.org/project/fastapi-versioning/
a more realistic example of it can be found here: https://github.com/DeanWay/fastapi-versioning/tree/master/example
The idea is that each version registered at some point in the app becomes a sub-application mounted at
/v{major}_{minor}
each version builds upon the previous, and overrides matching paths.
The parent app’s docs endpoint just lists the versions
The reason for using sub-applications is to separate the openapi.json and docs of each version (kind of a surprise for me that there’s nothing in the openapi spec to handle for mulitple api versions in one document)
I’d like to allow for version headers as well, but I think you still need the url version for openapi to make sense, so maybe some sort of redirection middleware? Might be a terrible idea, not sure yet.
Anyway, might be helpful, take a look, let me know what you think!
Hello @tiangolo,
With this choice, is it possible to have a generated doc that separates
v1
endpoints fromv2
endpoints. So far, it seems they’re going to be grouped by tags, e.g."users"
and in that group I will have both/v1/users
and/v2/users
documented.