Skip to content
masterclass Buddy

FastAPI · Function

Security

Use OAuth2PasswordBearer as a dependency to read a bearer token from the Authorization header. FastAPI also adds the scheme to OpenAPI and returns 401 when the header is missing or lacks a bearer token.

Last updated

Syntax

OAuth2PasswordBearer(tokenUrl="token")
Annotated[str, Depends(oauth2_scheme)]

In-depth API docs are only for subscribers: parameters, examples, common mistakes and interview practice.

Subscribe
  • OAuth2
  • OAuth2PasswordRequestForm
  • SecurityBase
  • Depends
  • OpenAPI
  • HTTPBearer

Source: Security - First Steps - FastAPI. Checked 2026-11-03.