Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all defined URL paths in FastAPI?

Suppose I have a FastAPI project that contains 100+ API endpoints. How can I list all APIs/paths?

like image 590
JPG Avatar asked Aug 01 '20 14:08

JPG


2 Answers

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,

  1. Using FastAPI app: This is handy when you have access to the FastAPi instance
  2. Using Request instance: This is handy when you have access to the incoming requests, but not to the FastAPI instance.

Complete Example

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
like image 151
JPG Avatar answered Oct 03 '22 18:10

JPG


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.

main.py
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.py
from 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.

like image 38
Vlad Avatar answered Oct 03 '22 19:10

Vlad