fastapi/fastapi
Application core
Purpose
The FastAPI class in fastapi/applications.py is the entry point user code instantiates. It is a Starlette subclass that adds:
- A default
APIRouter(self.routerfromfastapi/routing.py) and@app.get/.post/...decorators that delegate to it. - Automatic OpenAPI generation and the
/openapi.json,/docs,/redoc,/docs/oauth2-redirectroutes. - Default exception handlers for
HTTPExceptionand validation errors. - The
AsyncExitStackMiddlewarethat owns request-scoped cleanup.
Directory layout
fastapi/
├── applications.py # FastAPI class, ~4690 lines (mostly typed parameters with Doc(...))
├── __init__.py # Re-exports FastAPI, APIRouter, Depends, HTTPException, etc.
└── exception_handlers.py # http_exception_handler, request_validation_exception_handler, …Key abstractions
| Symbol | File | Description |
|---|---|---|
FastAPI |
fastapi/applications.py |
Subclass of starlette.applications.Starlette. Owns the default router, the OpenAPI schema cache, and the docs URLs. |
FastAPI.openapi() |
fastapi/applications.py |
Lazily builds and caches the OpenAPI dict via fastapi.openapi.utils.get_openapi. |
FastAPI.add_api_route() / .get() / .post() / .api_route() |
fastapi/applications.py |
Forward to self.router (an APIRouter). |
FastAPI.include_router() |
fastapi/applications.py |
Forward to self.router.include_router, which merges another router under a prefix. |
FastAPI.exception_handler() |
inherited via Starlette | Decorator to register custom exception handlers; FastAPI registers two by default. |
FastAPI.add_middleware() |
inherited via Starlette | Adds Starlette-compatible middleware; FastAPI itself adds AsyncExitStackMiddleware last so it wraps everything. |
How it works
The constructor accepts a long list of parameters that fall into four buckets:
- OpenAPI metadata —
title,summary,description,version,terms_of_service,contact,license_info,openapi_tags,openapi_url,webhooks. These are forwarded tofastapi.openapi.utils.get_openapi. - Docs UI URLs —
docs_url,redoc_url,swagger_ui_init_oauth,swagger_ui_parameters. Setting any toNonedisables the corresponding route. - Routing defaults —
default_response_class,dependencies,responses,redirect_slashes,generate_unique_id_function. Stored on the default router. - Lifespan + middleware —
lifespan,middleware,exception_handlers,on_startup,on_shutdown(the last two are kept via_DefaultLifespanfor backward compatibility).
Each @app.get(path, ...) decorator calls add_api_route(path, endpoint, methods=["GET"], ...), which delegates to self.router.add_api_route and stores an APIRoute in the route table. See Routing for what happens next.
Integration points
- Down to Starlette. The class is a
Starlettesubclass and reuses its routing tree, middleware stack, exception handling, and lifespan support. - Down to
APIRouter. All path registration goes throughself.router. The same router type is what users build separately and pass toapp.include_router(...). - Down to
fastapi/openapi.FastAPI.openapi()callsget_openapi(...)(fastapi/openapi/utils.py), passing the application's metadata, routes, and webhook routes. - Up to user code. Decorators and
include_routerare the surface user code touches.app.openapi()andapp.openapi_schemaare the direct hooks for tooling that wants the schema without HTTP.
Entry points for modification
- Adding or changing constructor parameters: edit
FastAPI.__init__and the matchingadd_api_routedefaults. Keep theAnnotated[T, Doc("""...""")]style — those docstrings power the API reference page on the docs site. - Adding new routes that should appear under
/docs: calladd_api_route(or use@app.get(...)); setinclude_in_schema=Falsefor routes that should be excluded. - Changing exception responses: register handlers in
FastAPI.__init__or overrideexception_handlers=on construction. Defaults live infastapi/exception_handlers.py. - Changing OpenAPI behaviour: modify
fastapi/openapi/utils.py:get_openapi. The cache lives onFastAPI.openapi_schema; clear it in tests if you want to re-generate.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.
Previous
Systems
Next
Routing