Skip to main content

⏱️ fastapi-timer

Lightweight FastAPI middleware for measuring API response time and downstream operation latency.

Current version: 0.1.0

✨ Features

  • ⏱️ Measure total API response time
  • 🌐 Measure latency of any downstream operation
  • 📊 See application vs downstream latency
  • ⚡ Designed for async FastAPI applications
  • 🪶 Lightweight and simple
  • 🚫 No database required
  • 🚫 No response headers

🚀 Why fastapi-timer?

When a FastAPI API is slow, it can be difficult to tell whether the time is being spent inside your application or waiting for a downstream service.

Without instrumentation, you might repeatedly write timing code:

import time

start = time.perf_counter()

response = await client.get("https://example.com")

elapsed = (time.perf_counter() - start) * 1000
print(f"Downstream latency: {elapsed:.2f}ms")

fastapi-timer provides a simple way to do this:

async with request.state.timer.track("downstream"):
    response = await client.get("https://example.com")

The middleware then logs:

GET /example
  total: 460.98ms
  downstream: 459.97ms
  application: 1.01ms

📊 What does it measure?

  • 🕐 total — complete API response time
  • 🌐 downstream — time spent in a tracked downstream operation
  • ⚙️ application — remaining application processing time

📦 Installation

Using uv:

uv add fastapi-timer

Using pip :

pip install fastapi-timer

🚀 Usage

Add the middleware to your FastAPI application:

from fastapi import FastAPI, Request

from fastapi_timer import TimingMiddleware

app = FastAPI()

app.add_middleware(TimingMiddleware)

Track any downstream async operation:

import httpx

@app.get("/example")
async def example(request: Request):
    timer = request.state.timer

    async with timer.track("downstream"):
        async with httpx.AsyncClient() as client:
            response = await client.get("https://example.com")

    return response.json()

🌤️ Example

A complete working example using a weather API is available in:

examples/weather.py

The weather API is only an example. fastapi-timer can be used with any downstream async operation, including external APIs, microservices, and other network calls.

Run the example locally:

uv run uvicorn examples.weather:app --reload

Then request weather for a city:

curl "http://127.0.0.1:8000/weather?city=Chennai"

Example output:

GET /weather
  total: 2445.45ms
  downstream: 777.26ms
  application: 1668.18ms

🛠️ Development

Install development dependencies:

uv sync

Run tests:

uv run pytest

Run tests with console output:

uv run pytest -s

Build the package:

uv build

📋 Requirements

  • 🐍 Python 3.12+
  • ⚡ FastAPI 0.141.1+

📄 License

MIT License

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_timer-0.1.2.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_timer-0.1.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_timer-0.1.2.tar.gz.

File metadata

  • Download URL: fastapi_timer-0.1.2.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.9

File hashes

Hashes for fastapi_timer-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3176852718ab6795e741c298191799427ef8d55b5ef70b963c87707599cdc561
MD5 7fc95a15e494b210ed5c1125cd7cf7ff
BLAKE2b-256 e09502a227465e6b2017eeebbfd4380c73b6efc83bc6be5c1d1ecb2b36c4b895

See more details on using hashes here.

File details

Details for the file fastapi_timer-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_timer-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.9

File hashes

Hashes for fastapi_timer-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5b93a6fe293f285e86a576529e352323e938c3e8e7fe6a4c9c070f98895b861e
MD5 7d6f9e0210df1844a7e6b1300a2bef0c
BLAKE2b-256 04cf61fd98a2d58f8258d825d654130a6b330c278b23af83d2659379f04ab9dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page