11

I am working on a project that requires to version FastAPI endpoints. We want to version the endpoint through HTTP accept header, for example:

headers={'Accept': 'application/json;version=1.0.1'}, 
headers={'Accept': 'application/json;version=1.0.2'}

Setting up the api version like this seem not work:

app = FastAPI(
        version=version,
        title="A title",
        description="Some description.",
    )

Does anyone know what else I need to do with this ?

2
  • I think version parameter is for docs only. Maybe try with Response headers for your responses. Commented Feb 27, 2021 at 10:44
  • Future readers should find this answer helpful as well. Commented Feb 22, 2024 at 16:37

3 Answers 3

15

Well maybe the version in path url could be better

sub apps docs

from fastapi import FastAPI

app = FastAPI()
v1 = FastAPI()

@v1.get("/app/")
def read_main():
    return {"message": "Hello World from api v1"}

v2 = FastAPI()

@v2.get("/app/")
def read_sub():
    return {"message": "Hello World from  api v2"}

app.mount("/api/v1", v1)
app.mount("/api/v2", v2)

You will see the auto docs for each app

localhost:8000/api/v1/docs

localhost:8000/api/v2/docs

But you always get the headers in request

from starlette.requests import Request

from fastapi import FastAPI
app = FastAPI()

@app.post("/hyper_mega_fast_service")
def fast_service(request: Request, ):

    aceept = request.headers.get('Accept')

    value = great_fuction_to_get_version_from_header(aceept)
    if value == '1.0.1': 
        "Do something"
        
    if value == '1.0.2': 
        "Do something"
Sign up to request clarification or add additional context in comments.

1 Comment

Another approach could be to add a middleware to identify the version in header then redirect the request to service you want
5

Try api versioning for fastapi web applications

Installation

pip install fastapi-versioning

Examples

from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title="My App")


@app.get("/greet")
@version(1, 0)
def greet_with_hello():
    return "Hello"


@app.get("/greet")
@version(1, 1)
def greet_with_hi():
    return "Hi"


app = VersionedFastAPI(app)

this will generate two endpoints:

/v1_0/greet
/v1_1/greet

as well as:

/docs
/v1_0/docs
/v1_1/docs
/v1_0/openapi.json
/v1_1/openapi.json

There's also the possibility of adding a set of additional endpoints that redirect the most recent API version. To do that make the argument enable_latest true:

app = VersionedFastAPI(app, enable_latest=True)

this will generate the following additional endpoints:

/latest/greet
/latest/docs
/latest/openapi.json

In this example, /latest endpoints will reflect the same data as /v1.1.

Try it out:

pip install pipenv
pipenv install --dev
pipenv run uvicorn example.annotation.app:app
# pipenv run uvicorn example.folder_name.app:app

Usage without minor version

from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title='My App')

@app.get('/greet')
@version(1)
def greet():
  return 'Hello'

@app.get('/greet')
@version(2)
def greet():
  return 'Hi'

app = VersionedFastAPI(app,
    version_format='{major}',
    prefix_format='/v{major}')

this will generate two endpoints:

/v1/greet
/v2/greet

as well as:

/docs
/v1/docs
/v2/docs
/v1/openapi.json
/v2/openapi.json

Extra FastAPI constructor arguments

It's important to note that only the title from the original FastAPI will be provided to the VersionedAPI app. If you have any middleware, event handlers etc these arguments will also need to be provided to the VersionedAPI function call, as in the example below

from fastapi import FastAPI, Request
from fastapi_versioning import VersionedFastAPI, version
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware

app = FastAPI(
    title='My App',
    description='Greet uses with a nice message',
    middleware=[
        Middleware(SessionMiddleware, secret_key='mysecretkey')
    ]
)

@app.get('/greet')
@version(1)
def greet(request: Request):
    request.session['last_version_used'] = 1
    return 'Hello'

@app.get('/greet')
@version(2)
def greet(request: Request):
    request.session['last_version_used'] = 2
    return 'Hi'

@app.get('/version')
def last_version(request: Request):
    return f'Your last greeting was sent from version {request.session["last_version_used"]}'

app = VersionedFastAPI(app,
    version_format='{major}',
    prefix_format='/v{major}',
    description='Greet users with a nice message',
    middleware=[
        Middleware(SessionMiddleware, secret_key='mysecretkey')
    ]
)

1 Comment

1

You can try my package https://github.com/modern-python/fast-version which adds versioning by Accept-header into FastAPI

Defining app and routes:

import fastapi

from fast_version import VersionedAPIRouter, init_fastapi_versioning


VERSION_HEADER: str = "application/vnd.some.name+json"
ROUTER_OBJ = VersionedAPIRouter()


@ROUTER_OBJ.get("/test/")
async def test_get() -> dict:
    return {"version": (1, 0)}


@ROUTER_OBJ.get("/test/")
@ROUTER_OBJ.set_api_version((2, 0))
async def test_get_v2() -> dict:
    return {"version": (2, 0)}


app = fastapi.FastAPI()
app.include_router(ROUTER_OBJ)
init_fastapi_versioning(app=app, vendor_media_type=VERSION_HEADER)

Query examples:

# call 1.0 version
curl -X 'GET' 'https://test.ru/test/' -H 'accept: application/vnd.some.name+json; version=1.0'

curl -X 'GET' 'https://test.ru/test/' -H 'accept: application/vnd.some.name+json'

curl -X 'GET' 'https://test.ru/test/'

# call 2.0 version
curl -X 'GET' 'https://test.ru/test/' -H 'accept: application/vnd.some.name+json; version=2.0'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.