---
source: https://iinventedlight.com/library/notes/fastapi-security-oauth2.md
title: "FastAPI OAuth2PasswordBearer: the first security step"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Security first steps; FastAPI, first-steps.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: fastapi oauth2"
tags: [fastapi, oauth2, security, bearer, depends]
client: Librarian
sources:
  - id: S1
    title: "Security first steps"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/security/first-steps/
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "first-steps.md"
    publisher: FastAPI
    url: https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/security/first-steps.md
    accessed: 2026-11-18
    license: "MIT"
---
# FastAPI OAuth2PasswordBearer: the first security step
**Answer:** We can use OAuth2 to build security with FastAPI. FastAPI provides several tools, at different levels of abstraction, to implement this.  

## Examples

```python
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
```

## Notes

The client sends a username and password to that token URL. The API responds with a token. The oauth2_scheme variable is an instance of OAuth2PasswordBearer, so it can be used with Depends. 

## Reference list

- `FastAPI` can use OAuth2 for this password-based flow.
- `oauth2` 
- `The` scheme instance can be used with Depends.

## See also

- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)
- [fastapi-request-body](https://iinventedlight.com/library/notes/fastapi-request-body/)
- [fastapi-error-handling](https://iinventedlight.com/library/notes/fastapi-error-handling/)

## Claims

- id: C1
  claim: FastAPI can use OAuth2 for this password-based flow.
  source: S1
  evidence: "We can use OAuth2 to build that with FastAPI"

- id: C2
  claim: 
  source: S2
  evidence: "declared with `tokenUrl=\"token\"`"

- id: C3
  claim: The scheme instance can be used with Depends.
  source: S2
  evidence: "it can be used with `Depends`"

## Sources

- [S1] Security first steps. FastAPI docs. https://fastapi.tiangolo.com/tutorial/security/first-steps/ . License: MIT. Accessed 2026-11-18.

- [S2] first-steps.md. FastAPI. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/security/first-steps.md . License: MIT. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Security first steps; FastAPI, first-steps.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
