API Reference

Starlette

Lightweight ASGI framework and toolkit providing routing, middleware, requests and test client.

Official Documentation ↗
CompatibilityThis documentation covers version starlette 0.37+ / Python 3.9+(ASGI). Check the official docs for the latest API surface.

Installation

pippip install starlette

Methods

Overview

Starlette is a compact ASGI framework and toolkit. It offers routing, requests and responses, middleware, background tasks and a test client, forming the base for higher-level frameworks.

Core Methods

Build a Starlette app from routes, attach middleware, and handle requests with async endpoints returning responses. The test client runs the app in-process for tests.

Usage Notes

Use it for small services or as the foundation of a custom stack. Define middleware explicitly and leverage the test client for fast integration tests.

Common Pitfalls

Blocking code in async endpoints stalls the event loop, so use thread offloading for sync work. Middleware order matters and is easy to get wrong. Background tasks share the process and are not durable queueing.

FAQ

How does Starlette relate to FastAPI?
FastAPI builds on Starlette for routing, requests and responses, adding data validation and dependency injection. Starlette remains a good choice for minimal async services.
How do I add background work?
Use BackgroundTask in a response to run work after the response is sent, or spawn a task. Keep it short since it runs in the same process.
What is the TestClient based on?
It drives the app through the ASGI interface, so tests exercise the real application stack including middleware and routing without opening a socket.