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.
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}"
As described in problem description, the working alternative is running the instance and curling the openapi.json from the docs endpoint.
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 :-)
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/
Most helpful comment
Hello there,
You can achieve this programmatically using get_openapi function from fastapi.openapi.utils.
I believe you are looking for a script like this one.