Skip to main content

Idempotency decorator for FastAPI

Project description

🧹 idemptx

PyPI - Version License: MIT

A minimal, pluggable idempotency decorator for FastAPI, designed for payment and retry-safe APIs.

Supports Redis as backend for deduplication, distributed locking, and response caching.


✨ Features

  • ✅ Supports Idempotency-Key header out of the box
  • 🔒 Redis-backed lock to prevent double execution
  • ⚡️ Sync & Async backends with auto-detection
  • 🧠 Request signature validation (method + URL + headers + body)
  • ⏳ Configurable wait_timeout behavior
  • 🔁 Response replay from cache (with headers)

📦 Installation

pip install idemptx

🚀 Quick Start

from fastapi import FastAPI, Request
from idemptx import idempotent
from idemptx.backend.redis import RedisBackend

app = FastAPI()
redis_backend = RedisBackend()

@app.post('/orders')
@idempotent(storage_backend=redis_backend)
async def create_order(request: Request):
    return {'status': 'created'}

⚠️ You must include request: Request in your endpoint parameters!


🔧 Advanced Usage

@idempotent(
    storage_backend=redis_backend,
    key_ttl=60,
    wait_timeout=3.0,
    validate_signature=True,
)
  • key_ttl: How long to hold cache and lock (in seconds)
  • wait_timeout: Wait for lock to be released (0 = immediate failure)
  • validate_signature: Whether to compare request content on replays

🔐 Response Headers

Header Description
Idempotency-Key Echoed back to client
X-Idempotency-Signature Hash of request for conflict detection
X-Idempotency-Status "hit" or "new"

📄 License

MIT License © 2025 pypy-riley

Project details


Download files

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

Source Distribution

idemptx-0.1.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

idemptx-0.1.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: idemptx-0.1.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.3.0

File hashes

Hashes for idemptx-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bd07399677e408ce22480182b2f3021b230cbea6e9677825ff60a4d89174fa70
MD5 e7539caee2b45e94b8831143b4835de0
BLAKE2b-256 3625500b97d00f6f41baafffb21b43178ad345daad32e4c56c4b9a201089d625

See more details on using hashes here.

File details

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

File metadata

  • Download URL: idemptx-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.3.0

File hashes

Hashes for idemptx-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1eef1f34ec448f561dae259a43a2bfa6c59c205e5f7e0b296a1e6c6d2f82e4e7
MD5 7902e565e31209490470e317e5f40248
BLAKE2b-256 13dc2d4b79e321144cc19dfb3de174ee5927f23835b17bcf60fa50c5993db8d7

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 Pingdom Monitoring Sentry Error logging StatusPage Status page