Google News
logo
FastAPI - Interview Questions
Explain the concept of routers in FastAPI.
In FastAPI, routers are a way to organize and modularize your API by grouping related endpoints together. Routers allow you to create separate sets of routes that are logically grouped based on their functionality. This helps in maintaining a clean and organized codebase, making it easier to manage and scale your FastAPI application.

Here are key concepts related to routers in FastAPI:

Creating a Router : You create a router using the APIRouter class from FastAPI. This class allows you to define routes, dependencies, and other aspects of your API within a specific router instance.
from fastapi import APIRouter

router = APIRouter()?

Defining Routes in a Router : You can define routes within a router using the same decorators (@app.get, @app.post, etc.) that you use at the application level. The difference is that you use the decorators on the router instance instead of the main FastAPI application instance.
@router.get("/items/")
def read_items():
    return {"message": "Read items"}?

Mounting a Router : To include a router in your main FastAPI application, you use the app.include_router method. This mounts the routes defined in the router to a specific path in the application.
from fastapi import FastAPI

app = FastAPI()

app.include_router(router, prefix="/v1")?

In this example, all routes defined in the router will be accessible under the /v1 path.

Dependency Injection in Routers : Routers support dependency injection in the same way as route functions. You can define dependencies within a router, and FastAPI will handle the injection of dependencies when processing requests to routes in that router.
from fastapi import APIRouter, Depends

router = APIRouter()

def get_db():
    # Function to get a database connection
    # ...

@router.get("/items/")
def read_items(db: Session = Depends(get_db)):
    return {"message": "Read items"}?

Organizing Code : Routers help in organizing your code by allowing you to group related endpoints together. This is particularly useful as your application grows, and you have multiple sets of functionalities.

Sub-routers : You can create sub-routers by instantiating additional APIRouter instances within a router. This allows for hierarchical organization of routes within your application.
from fastapi import APIRouter

parent_router = APIRouter()
child_router = APIRouter()

@parent_router.get("/parent/")
def parent_route():
    return {"message": "Parent route"}

@child_router.get("/child/")
def child_route():
    return {"message": "Child route"}

parent_router.include_router(child_router, prefix="/child")?

In this example, the /child/ route is a sub-route of the /parent/ route.

Using routers helps in creating modular and maintainable APIs by allowing you to separate concerns and organize your code in a structured manner. It's especially useful when building larger applications with multiple sets of functionality.
Advertisement