Suppose I have a FastAPI project that contains 100+ API endpoints. How can I list all APIs/paths?
To get all possible URL patterns, we need to get access to the defined URL routes which is an attribute of running app instance.
We can do that in at least two ways,
FastAPI
app: This is handy when you have access to the FastAPi instanceRequest
instance: This is handy when you have access to the incoming requests, but not to the FastAPI instance.from fastapi import FastAPI, Request
app = FastAPI()
@app.get(path="/", name="API Foo")
def foo():
return {"message": "this is API Foo"}
@app.post(path="/bar", name="API Bar")
def bar():
return {"message": "this is API Bar"}
# Using FastAPI instance
@app.get("/url-list")
def get_all_urls():
url_list = [{"path": route.path, "name": route.name} for route in app.routes]
return url_list
# Using Request instance
@app.get("/url-list-from-request")
def get_all_urls_from_request(request: Request):
url_list = [
{"path": route.path, "name": route.name} for route in request.app.routes
]
return url_list
I tried to provide an edit the original answer but wouldn't let me.
Another use case: Suppose you are not in the main app file and don't have access to app
in the namespace. In that case Starlette documentation says that we also have access to the app instance from the request as request.app
. For example if in the main file you only have the app instance and don't want to have any endpoints in the main file but all of them to be in separate routers.
from fastapi import FastAPI
# then let's import all the various routers we have
# please note that api is the name of our package
from api.routers import router_1, router_2, router_3, utils
app = FastAPI()
app.include_router(router_1)
app.include_router(router_2)
app.include_router(router_3)
app.include_router(utils)
I have my list_endpoints endpoint in the utils router. To be able to list all of the app routes, I would do the following:
utils.pyfrom fastapi import APIRouter, Request
router = APIRouter(
prefix="/utils",
tags=["utilities"]
)
@router.get('/list_endpoints/')
def list_endpoints(request: Request):
url_list = [
{'path': route.path, 'name': route.name}
for route in request.app.routes
]
return url_list
Note that rather than using app.routes
I used request.app.routes
and I have access to all of them. If you now access /utils/list_endpoints
you will get all your routes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With