Factory.ai

Open-Source Wikis

/

FastAPI

/

Systems

/

Application core

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.router from fastapi/routing.py) and @app.get/.post/... decorators that delegate to it.
  • Automatic OpenAPI generation and the /openapi.json, /docs, /redoc, /docs/oauth2-redirect routes.
  • Default exception handlers for HTTPException and validation errors.
  • The AsyncExitStackMiddleware that 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:

  1. OpenAPI metadatatitle, summary, description, version, terms_of_service, contact, license_info, openapi_tags, openapi_url, webhooks. These are forwarded to fastapi.openapi.utils.get_openapi.
  2. Docs UI URLsdocs_url, redoc_url, swagger_ui_init_oauth, swagger_ui_parameters. Setting any to None disables the corresponding route.
  3. Routing defaultsdefault_response_class, dependencies, responses, redirect_slashes, generate_unique_id_function. Stored on the default router.
  4. Lifespan + middlewarelifespan, middleware, exception_handlers, on_startup, on_shutdown (the last two are kept via _DefaultLifespan for 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 Starlette subclass and reuses its routing tree, middleware stack, exception handling, and lifespan support.
  • Down to APIRouter. All path registration goes through self.router. The same router type is what users build separately and pass to app.include_router(...).
  • Down to fastapi/openapi. FastAPI.openapi() calls get_openapi(...) (fastapi/openapi/utils.py), passing the application's metadata, routes, and webhook routes.
  • Up to user code. Decorators and include_router are the surface user code touches. app.openapi() and app.openapi_schema are 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 matching add_api_route defaults. Keep the Annotated[T, Doc("""...""")] style — those docstrings power the API reference page on the docs site.
  • Adding new routes that should appear under /docs: call add_api_route (or use @app.get(...)); set include_in_schema=False for routes that should be excluded.
  • Changing exception responses: register handlers in FastAPI.__init__ or override exception_handlers= on construction. Defaults live in fastapi/exception_handlers.py.
  • Changing OpenAPI behaviour: modify fastapi/openapi/utils.py:get_openapi. The cache lives on FastAPI.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.

Application core – FastAPI wiki | Factory