{"success":true,"name":"make-backend-write-endpoints-safe-against-double-clicks-and-","description":"Building a backend endpoint that creates or charges something","category":"ai-brimind","path":"ai-brimind/make-backend-write-endpoints-safe-against-double-clicks-and-/SKILL.md","file":"SKILL.md","content":"---\nname: make-backend-write-endpoints-safe-against-double-clicks-and-\ndescription: \"Building a backend endpoint that creates or charges something\"\n---\n\n# Make Backend Write Endpoints Safe Against Double Clicks and Retries\n\n## When to use\nProject has a backend API, and you are writing an endpoint that creates records, sends emails, or charges money, where the same request could arrive twice (double click, retry, refresh).\n\n## Example request\nuser double-clicks 'Order' → two identical orders appear; the endpoint needed to detect the repeat, not trust the button. a network retry resends the payment call → the client is charged twice; that is the classic idempotency problem, known and solvable. signup submitted twice → two accounts with the same email; a unique constraint in the database is the last line of defense the code forgot. 'we disabled the button on the frontend' → curl and flaky networks do not see your disabled button; the protection belongs in the backend.\n\n## Instructions\nAct like a backend engineer who assumes every important request will, someday, arrive twice — because networks retry and users double-click. For any endpoint that creates, sends, or charges, ask: what happens if this exact request runs two times in a row? If the answer is 'two orders / two charges / two emails', the endpoint is not safe yet. Make the repeat harmless: use a unique constraint in the database on the natural key (email, order reference) so the second insert fails cleanly, or check-then-create inside a transaction, or accept a client-generated request id and skip work already done for that id — this pattern has a name, idempotency, and your framework or payment provider may already support it, so check before hand-rolling. Frontend button-disabling is comfort, never protection. If you are unsure what should happen on a legitimate repeat (error? return the existing record?), ask. Only finish the endpoint once running it twice provably causes the effect once, and explain which mechanism (constraint, transaction, idempotency key) guarantees it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #524\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}