One click, two leads: why your API creates duplicates
Disabling the button stops double clicks, but not retries or repeated webhooks. An idempotency key stops the double effect in the backend.
The first fix almost everyone applies against duplicate requests is disabling the button when the form is submitted. It works… until a webhook retries.
It's a very common pattern: the connection is slow, the same request reaches the API twice, and you end up with two identical leads in the CRM, two WhatsApp messages, or two charges.
Why disabling the button isn't enough
Disabling the button is a good idea, and you should keep doing it. But it only protects your UI. There are plenty of ways for the same request to arrive twice without anyone double-clicking:
- Webhooks: if your endpoint is slow to respond, the provider marks the delivery as failed and retries it.
- Network retries: the request arrives, but the response gets lost, and the HTTP client sends it again.
- The user: reloads the page, opens a second tab, or hits "back" and resubmits the form.
- Other clients: an app, a script, or another integration never goes through your button.
A timeout isn't a failure
Here's the key idea: when a request times out, you don't know what happened. The server may have already created the record, and only the response got lost. Retry blindly and you create a duplicate.
The fix: an idempotency key
An operation is idempotent when doing it twice has the same result as doing it once. To get there with a POST, every request carries a unique ID:
POST /leads
Idempotency-Key: 7f3c9a2e-4b1d-4c8a-9e2f-1a6b0d5c3e7f- The client generates the key once per operation and reuses it on every retry.
- The API stores the key along with its response.
- If the same key comes back, it returns the saved response instead of creating a new record.
For incoming webhooks, the key usually comes for free: almost every provider sends an event ID. Store it and skip the events you've already processed.
The hard part
Generating the key is easy. The tricky part is in the details:
- How long to keep it: long enough to cover retries (hours or days), not forever.
- Concurrent requests: if the second one arrives while the first is still running, you need a lock or a unique constraint in the database.
- Same key, different data: if the request body changed, the right move is to reject it with a clear error.
Checking "does this lead already exist?" before inserting isn't enough: two concurrent requests can both pass the check. Use a unique constraint in the database.
Checklist
- Which endpoints create something or have side effects (records, messages, charges)?
- Do they accept an idempotency key or an event ID?
- Where do you store it, and for how long?
- What happens if two identical requests arrive at the same time?
- Does your frontend reuse the same key when it retries?
The frontend prevents the double click. The backend prevents the double effect.
This article is part of Integrations without drama: a new pattern every week.