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)
  • 🔢 In-memory backend for testing/local use

📦 Installation

pip install idemptx

🚀 Quick Start

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

app = FastAPI()
client = redis.Redis(host='localhost', port=6379, db=0)
redis_backend = RedisBackend(client)

@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

🔀 Async Redis Backend

import redis.asyncio as aioredis
from idemptx.backend import AsyncRedisBackend

async_client = aioredis.Redis(host='localhost', port=6379, db=0)
async_backend = AsyncRedisBackend(async_client)

🔖 In-memory Backend (for testing only)

from idemptx.backend import InMemoryBackend

backend = InMemoryBackend()

@app.post('/example')
@idempotent(storage_backend=backend)
async def create_something(request: Request):
    return {'ok': True}

Note: Not suitable for multi-process or production environments.


🔐 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.2.1.tar.gz (8.0 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.2.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: idemptx-0.2.1.tar.gz
  • Upload date:
  • Size: 8.0 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.2.1.tar.gz
Algorithm Hash digest
SHA256 f7c48f1edc6540bd7790187550f45caf2f179c28e5857c0fc56712055eabe72a
MD5 994c8828d74a83fcc9b0f4ba2f6c06ea
BLAKE2b-256 46b3fbf55bd9ba173521a2bb81db2ffad26cbd79a24f6b2320cf1dfdcc843b8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: idemptx-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b3e429895dadaa9fe6f6b8cd39e19c5ec2179796c31bef3239539787be1e3fa
MD5 43ec6ef1f9b93a8b580110c4ed831285
BLAKE2b-256 7466369a72abfc98b1ef9a4efde8e8df832ee336de47822435f31ac26befc825

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