Fastapi: [FEATURE] Generating openapi.json programmatically

Created on 27 Mar 2020  路  3Comments  路  Source: tiangolo/fastapi

Is your feature request related to a problem

Is your feature request related to a problem?

In a common CI environment there are cases when automatically deploying an openapi.json to services like Github Pages can very useful. However, in order to access the openapi.json thats generated after fastapi instance is running and providing the docs endpoint is sub-optimal because it requires spinning up the service in CI just to curl that file.

The solution you would like

Add extra flags to fastapi that will allow to generate the openapi.json without running the service instance.

For example when invoking via uvicorn pass extra args that fastapi will somehow intercept and produce the openapi file to specified location:

uvicorn main:app --fastapi_generate_openapi --fastapi_openapi_output "{output_path_goes_here}"

If its not possible to intercept the args when invoked via uvicorn, then perhaps another alternative is to provide a simple fastapi cli, pre packaged with the python module and do something like:

fastapi --fastapi_generate_openapi --fastapi_openapi_output "{output_path_goes_here}"

Describe alternatives you've considered

As described in problem description, the working alternative is running the instance and curling the openapi.json from the docs endpoint.

Additional comments

If this feature is somehow already available and someone could provide a link to how to use/invoke it ill greatly appreciate it and close the GitHub issue :-)

enhancement

Most helpful comment

Hello there,

You can achieve this programmatically using get_openapi function from fastapi.openapi.utils.

from fastapi.openapi.utils import get_openapi
from main import app
import json


with open('openapi.json', 'w') as f:
    json.dump(get_openapi(
        title=app.title,
        version=app.version,
        openapi_version=app.openapi_version,
        description=app.description,
        routes=app.routes,
        openapi_prefix=app.openapi_prefix,
    ), f)

I believe you are looking for a script like this one.

All 3 comments

Hello there,

You can achieve this programmatically using get_openapi function from fastapi.openapi.utils.

from fastapi.openapi.utils import get_openapi
from main import app
import json


with open('openapi.json', 'w') as f:
    json.dump(get_openapi(
        title=app.title,
        version=app.version,
        openapi_version=app.openapi_version,
        description=app.description,
        routes=app.routes,
        openapi_prefix=app.openapi_prefix,
    ), f)

I believe you are looking for a script like this one.

@egeucak yeah exactly what I was looking for. Thanks! closing issue

Thanks for the help here @egeucak ! :rocket:

And thanks for reporting back and closing the issue @aorumbayev :+1:

For reference, here are the docs: https://fastapi.tiangolo.com/advanced/extending-openapi/

Was this page helpful?
0 / 5 - 0 ratings