Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

727
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda