Fastapi: How to get the server version number

Created on 11 Nov 2019  路  7Comments  路  Source: tiangolo/fastapi

Description

How can I get the server version number, apart from downloading the whole openapi.json file from api/v1/openapi.json? I would not like to download the whole API schema just for the version number only.

I could create my own endpoint for that, but am I missing a more obvious way of doing it?

question

Most helpful comment

request.app.version is MUCH better than my from myproject import __version__, I might steal that...!

All 7 comments

Would this do it for you?

from fastapi import __version__ as fastapi_version

@app.get('/version')
def version(request: Request):
    """Retrieve version information"""

    return {'version': fastapi_version}

Not realy... :)

This would expose the fastapi project version. I am interested in the API version that fastapi is serving!

My implementation is similar though:

from myproject import __version__

router = APIRouter()

@router.get("/version", response_class=JSONResponse)
def version():
    v = {"version": __version__}
    return JSONResponse(status_code=200, content=v)

And I just update the __version__ var in the root __init__.py file.

I also have a:

app = FastAPI(
    title=config.PROJECT_NAME,
    description="Some description",
    openapi_url="/api/v1/openapi.json",
    version=__version__,
)

in my main.py file.

I am doing something similar for my app version and I am not aware of an easier solution.

Yeah, your best bet would probably be to return request.app.version in an endpoint. There is no built-in way to surface this information externally short of accessing the openapi spec.

request.app.version is MUCH better than my from myproject import __version__, I might steal that...!

Thanks everyone for the help! :cake:

I think that solves your question, right @stratosgear ? Do you want to close the issue?

Yes, I'm fine with this...

Was this page helpful?
0 / 5 - 0 ratings