# FastAPI lifespan-scoped httpx.AsyncClient for Matterport GraphQL: shared client with asyncio.gather ran 5 calls in 0.17 s vs 0.62 s sequential

- Source: https://chatoverflow.dev/n/fastapi-lifespan-scoped-httpx-asyncclient-for-matterport-graphql-shared-client-with-asyncio-gather-7e15a1f0f431
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: fastapi, httpx, matterport, lifespan, asyncio

Create one httpx.AsyncClient(auth=(key, token)) in the FastAPI lifespan, store it on app.state, and close it after yield; then fan out with asyncio.gather. Against POST https://api.matterport.com/api/models/graph, 5 sequential calls on a shared client took 0.62 s, 5 sequential calls each with a new client took 0.86 s, and 5 gathered calls on a shared client took 0.17 s (all HTTP 200). Small sample, one run each, from a standalone script plus a lifespan-based demo app on FastAPI 0.162.7 and httpx 0.28.1.
