---
source: https://iinventedlight.com/library/notes/fastapi-lifespan.md
title: "A FastAPI lifespan context manager runs code at startup and shutdown"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); BSD-3-Clause (Starlette docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Lifespan Events, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Lifespan (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-17
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-17
as_of: 2026-11-17
confidence: medium
untested: "Not reproduced (2026-11-17). 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-python: FastAPI lifespan"
tags: [fastapi, lifespan, startup, shutdown, starlette]
client: Librarian
sources:
  - id: S1
    title: "Lifespan Events"
    publisher: fastapi/fastapi (GitHub)
    url: https://github.com/fastapi/fastapi/blob/master/docs/en/docs/advanced/events.md
    accessed: 2026-11-17
    license: "MIT"
  - id: S2
    title: "Lifespan (Starlette)"
    publisher: Kludex/starlette (GitHub)
    url: https://github.com/Kludex/starlette/blob/main/docs/lifespan.md
    accessed: 2026-11-17
    license: "BSD-3-Clause"
---
# A FastAPI lifespan context manager runs code at startup and shutdown
**Answer:** Pass a `lifespan` async context manager to the `FastAPI` app for startup and shutdown work. Code before `yield` runs once, before the app starts receiving requests. Code after `yield` runs once, after the app has finished handling requests. Use this for shared resources such as a connection pool.

## How to write it

Define an async function with `yield`. The function is decorated with `@asynccontextmanager`. Pass that async context manager to the `lifespan` parameter of the `FastAPI` app. The recommended way to handle startup and shutdown is this `lifespan` parameter. If you provide a `lifespan` parameter, the older `startup` and `shutdown` event handlers are not called.

Lifespan events run only for the main application, not for mounted sub-applications.

## Starlette's lifespan

Starlette applications can register a lifespan handler for startup and shutdown work. Pass it as `lifespan`. Incoming requests are not served until that handler has run. Teardown happens after connections are closed and in-process background tasks have completed. Lifespan `state` is a dictionary used to share objects between the lifespan and the requests.

## See also

- [flask-and-fastapi](https://iinventedlight.com/library/notes/flask-and-fastapi/)
- [fastapi-background-tasks](https://iinventedlight.com/library/notes/fastapi-background-tasks/)
- [fastapi-apirouter](https://iinventedlight.com/library/notes/fastapi-apirouter/)
- [fastapi-upload-files](https://iinventedlight.com/library/notes/fastapi-upload-files/)

## Claims

- id: C1
  claim: Startup code in the lifespan function runs before the app takes requests.
  source: S1
  evidence: "executed **before** the application **starts taking requests**"

- id: C2
  claim: The FastAPI lifespan parameter takes an async context manager.
  source: S1
  evidence: "takes an **async context manager**"

- id: C3
  claim: Starlette does not serve requests until the lifespan has run.
  source: S2
  evidence: "will not start serving any incoming requests until the lifespan has been run"

- id: C4
  claim: Starlette lifespan teardown runs after connections and background tasks finish.
  source: S2
  evidence: "run once all connections have been closed"

## Sources

- [S1] Lifespan Events. fastapi/fastapi (GitHub). https://github.com/fastapi/fastapi/blob/master/docs/en/docs/advanced/events.md . License: MIT. Accessed 2026-11-17.

- [S2] Lifespan (Starlette). Kludex/starlette (GitHub). https://github.com/Kludex/starlette/blob/main/docs/lifespan.md . License: BSD-3-Clause. Accessed 2026-11-17.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Lifespan Events, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Lifespan (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd. Text was condensed and changed from the originals.

---

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