Skip to main content
When clients talk to your API, they have several different ways of sending you data: values embedded in the URL, key-value pairs in the query string, a JSON body in the request payload, and metadata in request headers. FastAPI gives you clean, declarative ways to handle all of them — and it validates every piece of incoming data automatically, so you never have to write manual type-checking code. This lesson walks through each channel and shows you how to use the powerful Annotated validation style recommended in Pydantic v2.

Request Data Processing Flow

FastAPI intercepts incoming HTTP requests, extracts parameters from every part of the request, validates their types using Pydantic, and feeds the clean values directly into your route function.
Validation happens before your function is ever called. If any input fails, FastAPI immediately returns a 422 Unprocessable Entity with a detailed error message explaining exactly what went wrong.

Path Parameters

Path parameters are variables embedded directly inside the URL path. You define them using curly braces in the route, then add a matching function argument with a type annotation.
  • Define the variable in the path inside curly braces: /employees/{employee_id}.
  • Annotate employee_id: int in the function signature — if a user requests /employees/abc, FastAPI immediately returns 422 without you writing any checking code.

Validated Path Parameters

Use Path() with Annotated to enforce additional constraints:

Query Parameters

Any function parameter that is not part of the path is automatically treated as a query parameter. They appear after the ? in the URL.
Request:
  • Use str | None = None for optional parameters.
  • Provide a default value directly (e.g., limit: int = 10).

Validated Query Parameters

The alias="dept" means clients send ?dept=Engineering but your parameter is named department in Python.

Request Bodies with Pydantic

When you need to send structured data — typically to create or update a resource — you use a request body with POST, PUT, or PATCH. Define the shape using a Pydantic model.
FastAPI automatically:
  1. Reads the request body as JSON
  2. Validates it against EmployeeCreate
  3. Creates an EmployeeCreate object and injects it as the employee parameter
Use employee.model_dump() to get a standard Python dictionary from the validated model. In Pydantic v2, the preferred approach separates the type from the validation metadata:
Both styles work, but Annotated is the recommended approach going forward.

Headers

You can read HTTP headers sent by clients using the Header class. FastAPI automatically converts snake_case parameter names to kebab-case header names.
FastAPI maps x_api_key (Python snake_case) to the X-API-Key header (HTTP kebab-case) automatically. You don’t need to do anything special.

Validation Reference

Common Validation Options

Useful Pydantic Types

EmailStr requires the email-validator package. Install it with:

Putting It All Together

Here’s a complete example combining path parameters, query parameters, a request body, and validation in one file: