Skip to content
masterclass Buddy

FastAPI · Concept

Lifespan events

FastAPI lifespan handlers run setup once before requests and cleanup once when the application shuts down. Define them with an async context manager and pass it to FastAPI.

Last updated

Syntax

FastAPI(lifespan=lifespan)

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

Subscribe
  • startup
  • shutdown
  • TestClient
  • Sub Applications - Mounts
  • on_event

Source: Lifespan Events - FastAPI. Checked 2026-11-03.