When your FastAPI app needs to call another API — fetching exchange rates, sending webhooks, looking up data — you need an HTTP client. The cleanest way is to create a single httpx.AsyncClient when the app starts, store it in app.state, and have every endpoint share that client through request.app.state. The shared client reuses its connection pool, never opens a new connection per request, and shuts down cleanly when the app stops.

The Problem

The pattern many people write first is to create the client right inside the endpoint:

from fastapi import FastAPI
import httpx
app = FastAPI()
@app.get("/weather")
async def weather():
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get("https://api.example.com/weather")
return resp.json()

Every request creates a new client, opens a new TCP connection, does a new TLS handshake, then closes it all when done. That is fine for a handful of requests, but as traffic grows the app gets both slower and more wasteful: every call to an external API pays the connection cost again, and bursts of calls can exhaust the available ports. A module-level global is no better: the client is created at import time, before any event loop is running, so it can latch onto the wrong loop, and tests cannot easily swap in a fake client.

The Solution

lifespan is the one place where the app can initialize resources in line with its own lifecycle. Create the client in lifespan, assign it to app.state.client, and close it in the finally block:

from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, Request
import httpx
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.client = httpx.AsyncClient(timeout=10.0)
yield
await app.state.client.aclose()
app = FastAPI(lifespan=lifespan)
def get_client(request: Request) -> httpx.AsyncClient:
return request.app.state.client
@app.get("/weather")
async def weather(client: httpx.AsyncClient = Depends(get_client)):
resp = await client.get("https://api.example.com/weather")
return resp.json()

Endpoints get the client through the get_client dependency — tidy and easy to test: in tests you only need to override this dependency to inject a fake client, without touching the real code. You can also read request.app.state.client directly inside an endpoint if you do not want the extra dependency.

Note one important boundary: app.state only holds things shared for the whole life of the app — an HTTP client, a database pool, immutable configuration. Do not store per-request data such as the logged-in user or a request id there; those belong in request.state, which lives for exactly one request and then disappears. One more detail for tests: TestClient only runs lifespan when used as a with statement, so tests must be written as with TestClient(app) as client: for app.state.client to exist.

Apply It to Your Project

Check your own app: anywhere you are creating an httpx.AsyncClient or aiohttp.ClientSession inside an endpoint, move it to a single client in lifespan. If the app calls several different external APIs, create one client per base URL — one for the payment API, one for the weather API, for example — and give each a clear name in app.state. Measure the external API call time before and after the change, and you will see the connection overhead disappear.

Related Posts

Frequently Asked Questions

How is app.state different from request.state?

app.state lives for the whole life of the app and holds shared resources such as an HTTP client or a database pool. request.state lives for exactly one request and holds per-request data such as a request id or the authenticated user.

Do I need a separate client for each endpoint?

No. One client per base URL is enough, because httpx.AsyncClient is safe to use concurrently from many requests. Creating one per endpoint only defeats the connection pool.

Can I use @app.on_event(“startup”) to create the client?

It works technically, but you should not do it anymore: on_event is deprecated in both FastAPI and Starlette, and if an app has both lifespan and on_event, only lifespan runs — the on_event handler is silently skipped.

How do I get the client in tests when lifespan has not run?

Use TestClient as a with statement so lifespan runs for real, or override the get_client dependency to return a fake client. Overriding is cleaner when the test only needs to check the endpoint logic, not call a real API.

💬 Have you tried this in your own project? If you hit an error or have a cleaner approach, leave a comment below — I read them all and will add the best ideas to the post.

References