Skip to main content

⏱️ fastapi-timer

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

Current version: 0.1.4

✨ 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: 1998.94ms
  geocoding - downstream1: 1236.11ms
  weather - downstream2: 738.61ms
  application: 24.22ms

🛠️ 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

Metadata

Release files for fastapi-timer 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-timer 0.1.4
File Size Uploaded
fastapi_timer-0.1.4.tar.gz 2.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-timer 0.1.4
File Interpreter ABI Platform
fastapi_timer-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 6.1 kB

Release files / fastapi_timer-0.1.4.tar.gz

Download URL fastapi_timer-0.1.4.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5557b26bd8fa74e481d9327b05bcfa91dc4cd12b8199496423926e1dae12524c
BLAKE2b-256 checksum
How to use checksums
965d8f05eac3d78ed2833ea7deb254bcc3c720285321b05852373b29030cb529
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release files / fastapi_timer-0.1.4-py3-none-any.whl

Download URL fastapi_timer-0.1.4-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b81bcf1618c2f871937ed8edc8ac4b6268d1ed8153be4f37a217d96cde0b2e9c
BLAKE2b-256 checksum
How to use checksums
1de466f3aa053b8f7f18e7e67139dca9d0a20fc6a0ec5df08492fb8a86a4724c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page