Gavin Wiggins

Notes   /   Links   /   Contact


FastAPI Router for Path Operations

Written on August 9, 2025

The APIRouter class in FastAPI is used to group path operations for an app. This allows paths to be defined in multiple files which is useful for large applications. The example below defines fruits and veggies paths for the router.

# routes.py

from fastapi import APIRouter

router = APIRouter()


@router.get("/fruits")
async def get_fruits():
    return {"fruits": ["apples", "oranges", "lemons"]}


@router.get("/veggies")
async def get_veggies():
    return {"veggies": ["beans", "peas", "celery"]}

In the main app module shown below, the router is imported and included with the app instance. Notice a path is also defined in this file too thus paths are not all defined in the same file.

# app.py

from fastapi import FastAPI
from routes import router

app = FastAPI()

app.include_router(router)


@app.get("/")
async def home():
    return {"message": "hello there"}

In summary, the paths for this application are /, /fruits, and /veggies which are defined in the app.py and routes.py files.

Further reading

See the APIRouter documentation for more details about the router class. Also see the FastAPI tutorial about using a routers subpackage for bigger applications.


Gavin Wiggins © 2025
Made on a Mac with Genja. Hosted on GitHub Pages.