{"success":true,"name":"make-write-endpoints-safe-to-retry-design-for-double-clicks-","description":"Building an API endpoint that creates, pays, sends, or changes something on POST","category":"ai-brimind","path":"ai-brimind/make-write-endpoints-safe-to-retry-design-for-double-clicks-/SKILL.md","file":"SKILL.md","content":"---\nname: make-write-endpoints-safe-to-retry-design-for-double-clicks-\ndescription: \"Building an API endpoint that creates, pays, sends, or changes something on POST\"\n---\n\n# Make Write Endpoints Safe to Retry: Design for Double Clicks and Lost Networks\n\n## When to use\nBuilding an API endpoint that creates, pays, sends, or changes something on POST\n\n## Example request\nUser double-clicks 'Submit' → two identical orders appear in the DB; the endpoint was not idempotent. Mobile app loses network after sending, so it retries → the payment is charged twice; the retry was treated as a brand-new request. A registration request times out and is resent → 'email already exists' error confuses the user; a duplicate check plus a friendly 'already done' response was the missing pattern. Frontend disables the button after click → helps, but the network can still resend; the real protection must live on the backend, this is the 'idempotency' concept.\n\n## Instructions\nAct as a backend engineer who assumes every write request WILL arrive twice — because on the real internet, it will. Before finishing any endpoint that creates or changes data, walk through this: (1) Say out loud what happens if this exact request runs two times in a row. If the answer is 'two orders', 'two charges', or 'two emails', the endpoint is not safe yet. (2) Pick a protection that fits: a unique constraint in the database (e.g. unique order reference, unique email) so the second insert is rejected by the DB itself; or an idempotency key — a client-generated ID sent with the request, stored on first success, so a repeat with the same key returns the saved result instead of doing the work again; or a state check ('is this order already paid?') before acting. (3) Decide what the SECOND request should return — usually the same success response as the first, not an error, so retries feel invisible to the user. (4) Do not rely on the frontend disabling a button; the backend is the last line of defense. If you are unsure whether duplicates can really happen here, trace the flow: network timeouts, retries, and double clicks all cause them. Only mark the endpoint done once you can state exactly which mechanism stops the duplicate and why the second attempt is now harmless.\n\n## Metadata\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #187\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}