# Startup, Shutdown and a Health Check

FastAPI's lifespan function runs code once as the app starts, such as the migrations, and once as it stops, a TestClient used in a with block runs it in tests, and a /health route that asks the database a real question answers 200 when the API can do its job and 503 in the one error shape when it can't, which is what a host checks before it sends anyone to the app.

![Cheat sheet: Startup, Shutdown and a Health Check](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/f223d92d9c8245e79e264110204a822d.webp)

- Category: FastAPI
- Level: Freelancer
- Tags: 503 Service Unavailable, Context Manager, fastapi, Health Check (rest-api), Lifespan Events, rest-api
- Author: Gregor Dzierzon
- Updated: November 23, 2026

Source: https://www.ask-codey.com/cheat-sheets/fastapi-pro-lifespan-and-health-cheat-sheet
