Products / SaaS / Backend Infrastructure

Live

IdemKey

HTTP Idempotency-Key middleware — safe POST/PATCH retries

Express middleware that fingerprints method+path+key+body hash, claims a Postgres row, runs the handler once, and replays the stored response on retries.

Value

Problem. Clients retry POST/PATCH. Without Idempotency-Key handling, flaky networks double-charge, double-book, or double-email.

How it helps. Express middleware that fingerprints method+path+key+body hash, claims a Postgres row, runs the handler once, and replays the stored response on retries.

Why buy. Stripe-style safe retries for your mutating endpoints — complements HookQueue (inbound webhooks) for your own API.

How it works

flowchart LR
  Client -->|"Idempotency-Key + body"| MW[IdemKey Middleware]
  MW -->|fingerprint sha256| FP["method|path|key|bodyHash"]
  MW -->|begin claim| Store[(Postgres)]
  Store -->|first / expired| Exec[Run handler]
  Store -->|completed| Replay["Replay status + body"]
  Store -->|started / in-flight| C409["409 Conflict"]
  Store -->|same key ≠ body| C422["422 Key reuse"]

Mermaid flowchart (render in GitHub / VS Code / mermaid.live).

Use case

Mobile taps Pay twice with the same Idempotency-Key; first call creates one fake charge, second returns the same charge id with Idempotency-Replayed: true.

What you get

Project structure

idemkey/
  src/
    middleware/   idempotency.ts
    stores/       postgres, memory, types
    routes/       charges, health
    lib/          fingerprint, config, prisma
  prisma/ tests/ scripts/demo-curl.sh docker-compose.yml

Quick start

docker compose up -d
cp .env.example .env
npm install && npm run prisma:generate && npm run prisma:migrate
npm run dev
./scripts/demo-curl.sh

Example

From src/middleware/idempotency.ts

// Idempotency-Key → fingerprint(method|path|key|bodyHash)
// first → status=started → run handler → store response → completed
// retry → replay body + Idempotency-Replayed: true
// concurrent → 409; same key ≠ body → 422

Tested

14 Vitest tests — fingerprinting, memory store claim/replay, middleware first-call vs replay without second handler invoke.

npm test

2026-09-20 — 14 passed; npm run build OK

Design decisions

Limitations

Who it’s for

API authors shipping POST/PATCH that must survive mobile and gateway retries.

Not for: Teams that only need webhook-provider dedupe (use HookQueue) or Redis-only SETNX shops.

What you own

Full MIT-licensed TypeScript source via Gumroad ZIP. Fork it, ship it commercially, keep the license notice. You are responsible for secrets, hardening, and production ops.

Related

RateGuard · HookQueue · TenantScope · CronLock

Demo

IdemKey demo
Live Postgres demo — first POST /v1/charges stores response; replay with same Idempotency-Key returns same charge id.
Buy IdemKey — $19 All products