Skip to main content
The FastAPI class is the main entry point for creating FastAPI applications. It provides the core functionality for defining routes, handling requests, and generating OpenAPI documentation.

Class Signature

Constructor Parameters

bool
default:"False"
Boolean indicating if debug tracebacks should be returned on server errors.
str
default:"FastAPI"
The title of the API. It will be added to the generated OpenAPI (visible at /docs).
str | None
default:"None"
A short summary of the API. It will be added to the generated OpenAPI.
str
default:""
A description of the API. Supports Markdown (using CommonMark syntax). It will be added to the generated OpenAPI.
str
default:"0.1.0"
The version of the API. This is the version of your application, not the version of the OpenAPI specification.
str | None
default:"/openapi.json"
The URL where the OpenAPI schema will be served from. Set to None to disable OpenAPI schema and automatic /docs and /redoc endpoints.
list[dict[str, Any]] | None
default:"None"
A list of tags used by OpenAPI. The order specifies the order shown in tools like Swagger UI.
list[dict[str, str | Any]] | None
default:"None"
A list of dicts with connectivity information to target servers.
Sequence[Depends] | None
default:"None"
A list of global dependencies, applied to each path operation including in sub-routers.
type[Response]
default:"JSONResponse"
The default response class to be used for all path operations.
bool
default:"True"
Whether to detect and redirect slashes in URLs when the client doesn’t use the same format.
str | None
default:"/docs"
The path to the automatic interactive API documentation (Swagger UI). Set to None to disable.
str | None
default:"/redoc"
The path to the alternative automatic interactive API documentation (ReDoc). Set to None to disable.
str | None
default:"/docs/oauth2-redirect"
The OAuth2 redirect endpoint for the Swagger UI.
dict[str, Any] | None
default:"None"
OAuth2 configuration for the Swagger UI.
Sequence[Middleware] | None
default:"None"
List of middleware to be added when creating the application.
dict[int | type[Exception], Callable] | None
default:"None"
A dictionary with handlers for exceptions.
Lifespan[AppType] | None
default:"None"
A Lifespan context manager handler for startup and shutdown events.
str | None
default:"None"
A URL to the Terms of Service for your API.
dict[str, str | Any] | None
default:"None"
A dictionary with the contact information for the exposed API.
dict[str, str | Any] | None
default:"None"
A dictionary with the license information for the exposed API.
str
default:""
A path prefix handled by a proxy that is not seen by the application but is seen by external clients.
bool
default:"True"
Whether to automatically generate the URLs in the servers field using the root_path.
dict[int | str, dict[str, Any]] | None
default:"None"
Additional responses to be shown in OpenAPI.
list[BaseRoute] | None
default:"None"
OpenAPI callbacks that should apply to all path operations.
APIRouter | None
default:"None"
OpenAPI webhooks for the application.
bool | None
default:"None"
Mark all path operations as deprecated.
bool
default:"True"
Whether to include all path operations in the generated OpenAPI.
dict[str, Any] | None
default:"None"
Parameters to configure Swagger UI.
Callable[[APIRoute], str]
default:"generate_unique_id"
Customize the function used to generate unique IDs for path operations.
bool
default:"True"
Whether to generate separate OpenAPI schemas for request body and response body.
dict[str, Any] | None
default:"None"
Additional external documentation links.
bool
default:"True"
Enable strict checking for request Content-Type headers. When True, requests with a body that do not include a Content-Type header will not be parsed as JSON.

Methods

@app.get(path, **kwargs)

Define a GET endpoint.

@app.post(path, **kwargs)

Define a POST endpoint.

@app.put(path, **kwargs)

Define a PUT endpoint.

@app.patch(path, **kwargs)

Define a PATCH endpoint.

@app.delete(path, **kwargs)

Define a DELETE endpoint.

@app.options(path, **kwargs)

Define an OPTIONS endpoint.

@app.head(path, **kwargs)

Define a HEAD endpoint.

@app.trace(path, **kwargs)

Define a TRACE endpoint.

add_api_route(path, endpoint, **kwargs)

Add an API route programmatically.
str
required
URL path for the route.
Callable
required
The endpoint function to call.
list[str] | None
HTTP methods for this route (e.g., ["GET", "POST"]).
Any
Pydantic model for response validation and serialization.
int | None
Default status code for the response.
list[str | Enum] | None
Tags for OpenAPI documentation.
Sequence[Depends] | None
List of dependencies for this route.
str | None
Short summary for OpenAPI documentation.
str | None
Detailed description for OpenAPI documentation.

include_router(router, *, prefix="", tags=None, dependencies=None, **kwargs)

Include an APIRouter in the application.
APIRouter
required
The APIRouter to include.
str
default:""
URL path prefix for all routes in the router.
list[str | Enum] | None
Tags to be applied to all routes in the router.
Sequence[Depends] | None
Dependencies to be applied to all routes in the router.
bool | None
Mark all routes in the router as deprecated.
bool
default:"True"
Include routes in OpenAPI schema.

add_middleware(middleware_class, **options)

Add middleware to the application.

@app.exception_handler(exception_class)

Register a custom exception handler.

openapi()

Generate and return the OpenAPI schema.

@app.websocket(path)

Define a WebSocket endpoint.

Attributes

State
A state object for the application. The same object for the entire application.
dict[Callable, Callable]
A dictionary with overrides for dependencies, useful for testing.
APIRouter
The internal router instance.
list[BaseRoute]
List of all registered routes.
dict[str, Any] | None
The cached OpenAPI schema.
str
The OpenAPI version string (default: “3.1.0”).
APIRouter
The webhooks router for OpenAPI webhooks documentation.

Example