Skip to main content

Claude/ChatGPT Prompt to Implement Idempotency Keys for Safe API Retries

Implement idempotency keys for POST/PUT — header, response storage, TTL, race handling, and tests — so client retries never double-charge.

Füllen Sie die Platzhalter aus

Edit the values, then copy your finished prompt.

Ihr Prompt
prompt.txt

                                

What this prompt does

This prompt makes the AI act as a senior backend engineer that implements idempotency keys for a specific endpoint, returning working code instead of pseudocode. You provide the [api_name], your [stack], the key [ttl], and the [duplication_risk] - what actually breaks if a request runs twice - and it produces the migration, the middleware or handler, and the test file, each fenced and copy-ready.

The six deliverables are ordered to cover the full lifecycle of a key: accept and validate the Idempotency-Key header, persist the key with a hash of the request body and the stored response, enforce a [ttl] cleanup so old keys do not grow unbounded, handle races via a DB unique constraint so two concurrent requests cannot both execute, replay the stored response with its original status code, and test first call, exact replay, in-flight collision, and body-mismatch rejection. The structure works because it pushes the correctness into the database, not the application layer - the unique constraint is what actually stops the double-write under load.

When to use it

  • You are shipping a POST or PUT with real side effects - charges, emails, provisioning - where a retry must not run twice.
  • A flaky network is turning a single client request into duplicate operations.
  • You need safe client retries on a payments or checkout endpoint.
  • You want race handling that survives concurrent requests, not just a naive existence check.
  • You need a test suite that proves replay, collision, and body-mismatch behaviour before launch.

Example output

You get three fenced code blocks: a database migration creating the idempotency table with a unique constraint on the key, the middleware or handler that validates the Idempotency-Key header, hashes the body, stores and replays responses, and applies the [ttl] cleanup, plus a test file covering the first call, an exact replay, an in-flight collision, and a body-mismatch rejection.

Pro tips

  • Set [duplication_risk] concretely - "the customer is charged twice" tells the model the stakes and pushes it toward stronger guarantees.
  • Put your effort into deliverable four: insist on a DB unique constraint, not an application-level check, because only the constraint stops the double-write under concurrency.
  • Pick a [ttl] that matches how long clients realistically retry; 24 hours is a safe default for payments, shorter for low-stakes calls.
  • Match [stack] to your real database so the migration and constraint syntax are usable as written.
  • Make sure the stored response includes the original status code, or replays will look different from the first call.
  • If the body-mismatch test is missing, ask for it - reusing a key with a different payload should be rejected, not silently replayed.

Frequently Asked Questions

Why use a database unique constraint instead of an application check?
Under concurrent load, two requests can both pass an application-level existence check before either writes, causing a double execution. A database unique constraint on the idempotency key forces one of the concurrent inserts to fail, which is the only reliable way to prevent the double-write.
What does the TTL control and why does it matter?
The `[ttl]` sets how long stored keys and responses live before cleanup removes them. Without it, the idempotency table grows unbounded; with it, old keys expire after the window during which a client might realistically retry the same request.
Why does the prompt hash the request body?
Storing a hash of the request body lets you detect when a client reuses the same `Idempotency-Key` with a different payload. That body-mismatch case should be rejected rather than replayed, since it signals a client bug rather than a legitimate retry.
Does this prevent duplicate charges on a payments API?
It is designed for exactly that, with `[duplication_risk]` defaulting to double-charging. The header, stored response, and unique constraint together make retries safe, but you still need to validate the generated code and tests against your real payment flow before production.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Mehr in API Development Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support