This is an old revision of the document!
Idempotency for APIs
Idempotency for APIs is especially important because the likelyhood of an API consumer implementing a “retry” functionality is alot higher than someone working with a UI.
What does a “retry” functionality have to do with Idempotency though you may ask?
As briefly explained in the article about Idempotency Idempotency is ensuring that actions always result in the same one thing. This is hard to do for API endpoints like POSTs because if not handled properly multiple exact same requests will cause multiple entries to be created, which can violate the Data Integrity.
To counteract this, a decently new HTTP header has been developed, called an “Idempotency-key” it is basically an identifier to the request, according to which, an application should decide if it should run the business logic again or simply return the cached response from a previous request.
But in what situation can a request be sent twice, usually after a request a user won't resend the request.
But an automated API might, incase it recieves no response due to some kind of network or server issue, it might send a retry on a timeout which would cause the request to be recieved and saved twice.
This is only an issue for endpoints with POST or PATCH since the others are naturally Idempotent.
A GET request is safe anyways as it doesn't edit data.
Delete is Idempotent as calling it multiple times will not delete more pages, only the same one over and over. This will lead to a different response to the user. Some kind of 404 Not Found will be thrown however, the effect on the application remains the same which is why its Idempotent.
PUT Is Idempotent as it overwrites one singular resource in the exact same way, which leads to no change in data.
PATCH even tho listed as not Idempotent, can also be Idempotent, it can also only edit things in the exact same way, however it can also edit resources percentually, which would lead to for example a price to be raised by 20% twice on a duplicate request.
This is only if you follow the conventions, since if you wanted to, you could just map a PATCH request to logic that creates a new entry.
