The most expensive kind of error between a mobile app and an API is the duplicate transaction. The user taps the pay button, the network drops, the app resends the request and two orders get created. This isn't a bug so much as a design gap: in the mobile world, request retries aren't the exception, they're the norm.
The root of the problem: the lost response
The thing to watch for isn't the request failing to arrive. Most of the time the request reaches the server and the operation completes successfully, but the response gets lost on its way back to the client. From the client's point of view, the outcome is unknown; the only reasonable thing it can do is retry. In other words, you can't fix this on the client side with "better error handling." The server has to guarantee that it won't perform the same operation twice.
How an idempotency key works
The common, robust solution: when starting an operation, the client generates a unique key (a UUID is enough) and sends it in the Idempotency-Key header. When retrying the same operation, it uses the same key. On the server side, the flow is:
- If the key hasn't been seen before, perform the operation and store the key together with the response.
- If the key has been seen and the operation has completed, return the stored response without performing a new operation.
- If the key has been seen but the operation is still in progress, return an "in progress" status with something like a 409.
- If a different request body arrives with the same key, reject it with an error; this is a client bug and shouldn't be silently let through.
The critical detail: the key record and the business record must be written within the same database transaction. If they're written separately, a crash right between the two costs you the guarantee, and you turn the problem you're trying to solve into one that's rarer but harder to track down.

Who generates the key matters
The key should be generated on the client, once, when the screen for that operation opens, and then kept. A common mistake is regenerating the key before every attempt; in that case the mechanism doesn't work at all. On the Flutter side, the practical approach is to make the key part of the operation's state — not to tie it to the button press.
A second line of defense at the database level
Even though the check in the application layer looks sufficient, we don't rely on a single line of defense. Two concurrent requests can both see "no key" and proceed. The definitive fix is a unique constraint at the database level: put a unique index on the key column so the second write fails, and interpret that error as "operation already done." Catching a constraint violation is both simpler and more reliable than wrestling with locks.
Where to apply it
You don't need it on every endpoint. GET and DELETE are idempotent by nature; the real concern is POST. Our list usually looks like this: initiating payments, creating orders, money transfers, point/balance movements, sending notifications and costly calls to external services. Also set a retention period — 24 hours is reasonable for most scenarios; there's no benefit in letting the key table grow forever.
The chain breaks when you talk to an external service
The hard part is when the operation happens outside your own database. You sent a call to the payment provider and got a timeout; you don't know whether the payment went through or not. The only correct behavior here is not to guess: leave the operation in an "unknown" state and verify its status through the provider's query endpoint.
That's why we build the state machine for payment flows with four values instead of three: pending, unknown, succeeded, failed. Skipping the "unknown" state is the most common source of the reconciliation problems we see in the field. Also, if the provider supports its own idempotency key, map it to your own key and send that as well; two-sided protection is markedly safer than one-sided.
Reconciliation: the last safety net
No matter how well you design it, inconsistencies can still crop up at the edges. That's why we run a daily reconciliation job on every system that handles payments or balances: a job that compares our own records with the provider's and reports the differences. If the number of differences is greater than zero, a human takes a look. This job is never exciting, but the first time it catches a discrepancy, it pays for itself many times over — because otherwise you'd find out about that discrepancy from a customer complaint.

If you run an API that handles payments, orders or balance movements and you're getting complaints about duplicate records, this is usually something a one- to two-day intervention can close out. Write to us via our contact page; we'll look at your current endpoints and work out together which ones need protection.