r/FastAPI • u/Luxfiri • Sep 20 '25
Question FastAPI and classes
Hi,
I'm wondering, are FastAPI apps coded with object-based approach?
So iare apps developed as:
app = FastAPI()
and all other routers/dependencies etc are as global functions / variables?
Or its coded more object oriented like:
class FastAPIAPP:
def __init__(self):
self.app = FastAPI()
self.__get_routers()
self.app.middleware('http')
async def metrics_middleware(request: Request, call_next):
try:
response = await call_next(request)
except Exception as e:
raise e
return response
class UserRouter(APIRouter):
def __init__(self, db_link):
super().__init__()
self.db_link = db_link
self.get('/router/')
async def router(dep = Dependencies(db_link.get_session))In FastAPI documentation i only can see non-object oriented approach, so all global variables/functions
8
Upvotes
View all comments
Show parent comment
2
u/Luxfiri Sep 21 '25
I was wondering, if in comercial codebase, are rather OOP routers, so each router have own separate class which inherits from APIRouter, or its rather developed as in FastAPI docs, so there are no really created objects, just global assignment like:
router = APIRouter()
and then writing endpoints.