question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] recommended way to do API versioning

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Reactions:26
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

25reactions
DeanWaycommented, Dec 3, 2019

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!

14reactions
gpakoszcommented, Feb 25, 2020

Hello @tiangolo,

If you want to have different versions in the same API, like http://localhost:8000/api/v1/users and http://localhost:8000/api/v2/users, in recent versions of FastAPI you can include a router more than once with a different path prefix.

With this choice, is it possible to have a generated doc that separates v1 endpoints from v2 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.

Read more comments on GitHub >

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

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found