Framework cheatsheet
FastAPI 0.115 Cheatsheet
Install, run, test and deploy FastAPI apps from the command line.
Getting started
- Create a virtual environment — Create an isolated environment for the project's packages.
- Activate the virtual environment — Use the environment's Python and packages in this shell. On Windows run .venv\Scripts\activate.
- Install FastAPI — Install FastAPI with the standard extras: the fastapi command, Uvicorn and the other common dependencies.
Running the app
- Start the development server — Run the app with auto-reload. It listens on 127.0.0.1:8000 by default.
- Change the host and port — Listen on all interfaces on port 8080, for example inside a container or on a LAN.
- Start the production server — Run without auto-reload, listening on 0.0.0.0 by default.
- Run several worker processes — Start multiple processes to use more than one CPU core.
- Run with Uvicorn directly — Start the app object named app in main.py with auto-reload.
- Fetch the OpenAPI schema — Download the JSON schema FastAPI generates. The interactive docs are served at /docs.
Testing
- Install the test tools — TestClient needs httpx, and pytest runs the tests.
- Run the tests — Run every test under the current directory.
- Run one test — Run a single test function and show print output.
Deployment
- Record the installed packages — Write the exact package versions to a requirements file for deployment.
- Build a Docker image — Build an image from the Dockerfile in the current directory.
- Run the Docker image — Start a container in the background and publish port 80.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- Django 6 cheatsheet — Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- Express 5 cheatsheet — Build HTTP servers and APIs with Express 5 on Node.js.
- Flask 3 cheatsheet — Create, run, test and deploy Flask apps, with routes, templates, blueprints, sessions and database snippets.