# Idempotency
source: https://developer.mastercard.com/mastercard-processing-fraud/documentation/api-basics-section/idempotency/index.md

Idempotency is a key concept in computer science, referring to certain operations that can be performed multiple times without changing the result beyond the initial execution. In the context of REST APIs, an API is considered idempotent when making multiple identical requests produce the same effect as making a single request.

To achieve idempotency, you should send an `Idempotency-Key` header that is unique to each request. The server uses this header, along with the request body, to identify subsequent retries of the same request.

The mechanism is intended to work for all `POST` and `PATCH` methods. The `Idempotency-Key` is optional. If the header is not specified in the request, the request will be processed in the standard way.

If you send an `Idempotency-Key` in the request, then the application checks if this key is in the database.

* If the key is not found in the database, the API call is processed in a standard way according to the method's handling algorithm. When the processing is complete, the `Idempotency-Key` and the request and response bodies are stored in the database.
* If the `Idempotency-Key` of the request is already in the database, it additionally verifies the request body. If they are the same, it retrieves the response from the database and returns it.  

The idempotency keys are automatically removed from the application after 24 hours.

* If a request with a previous `Idempotency-Key` is retried after 24 hours, the request will be processed as a new request.
* If a request with a previous `Idempotency-Key` is retried within the 24-hour window, the API will reject the request with the following error:

```JSON
{
    "Errors": {
        "Error": [
            {
                "Source": "MASTERCARD PROCESSING",
                "ReasonCode": "IDEMPOTENCY_KEY_ALREADY_USED",
                "Description": "Idempotency Key MUST not be reused across different payloads.",
                "Recoverable": false
            }
        ]
    }
}
```

