Skip to main content

Idempotency decorator for FastAPI

Project description

🧹 idemptx-redis6

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-redis6

🚀 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"

❗️Limitations

Currently, only JSONResponse is supported for caching.

If your endpoint uses response_model, the return value is typically a Pydantic model, which FastAPI wraps after the decorator has executed. This means the idempotency decorator cannot cache the final serialized response or set headers reliably in this case.

To enable caching, please return a JSONResponse explicitly from your endpoint.

Support for response_model and automatic response wrapping may be added in a future version.


📄 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_redis6-0.2.3.tar.gz (7.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_redis6-0.2.3-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file idemptx_redis6-0.2.3.tar.gz.

File metadata

  • Download URL: idemptx_redis6-0.2.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0

File hashes

Hashes for idemptx_redis6-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8152fb0951085520fe7eac2b8c5877613e45fb85c8ac5784ae6a0e93ab8ea030
MD5 45cb83b49990ef9c382b9900b9cf7e1c
BLAKE2b-256 98ae565bda478f47c2a56e2268690238045015fd5f652879faeb55a7739a4df0

See more details on using hashes here.

File details

Details for the file idemptx_redis6-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: idemptx_redis6-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0

File hashes

Hashes for idemptx_redis6-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bb5a0f45fcdd89b134dacfeb1090aa1f1ce9513b3b2b764d03ea77ac84a16460
MD5 e61e1d8e062c9c45fc17b6730596ca98
BLAKE2b-256 84f8a2ed8b0ab8f58dfb052623c3005e11a667409ed17cd89b8ec6a1d90c7082

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