Idempotency

Prev Next

Narmi APIs support idempotent requests. When you send a request with an idempotency key, the server ensures the request is processed only once, no matter how many times it is sent or received. This is important in scenarios where API requests may be retried due to network issues, timeouts, or other errors, which can result in duplicate requests with unintended consequences, such as duplicate transactions. For example, if a request to send a transaction does not respond due to a network connection error, it may be retried. In this scenario, the deduplication mechanism would recognize the duplicated request using an idempotency key and prevent the transaction from being submitted twice.

About the Idempotency Key

To perform an idempotent request, include the optional Idempotency-Key header with a properly formatted UUID. The idempotency key is a unique key that the server uses to recognize subsequent retries of the same request. Narmi stores each idempotency key for one hour. If a subsequent request is made with the same idempotency key within that hour, it won’t be processed. Instead, the API returns a 409 HTTP response with an error body. The response includes a Retry-After header when the original request is still processing, and no Retry-After header when the original request has completed and returned a 2xx successful response.

Using the Idempotency-Key Header

Follow these steps to make idempotent requests:

1. Generate a valid UUID – Clients connecting to the APIs must manually generate a 36-character UUID which will be set as the optional idempotency-key header. We require UUID v4 for idempotency key generation, which is specifically generated using random or pseudo-random numbers. For example:

Idempotency-Key: 7ef20b34-d7a1-410b-8aca-a1691c36464d

2. Add the idempotency key header – Include the UUID as an idempotency-key header for all HTTP methods that mutate data—POST, PUT, and PATCH requests. There is no need to send idempotency keys in GET and DELETE requests, as these requests are idempotent by definition. The header is not case-sensitive.

Response Codes

The resulting request can return the following responses:

  • Normal response codes – The request is unique
  • 409 code with a Retry-After field with a value equal to REQUEST_IDEMPOTENCY_LOCK_SECONDS – The idempotent request is currently being processed
  • 409 code with no Retry-After field – The request has been previously made and completed
  • 422 code – The UUID is improperly formatted

The 409 response returns the following error body:

{
  "id": "idempotency_key_used",
  "message": "Idempotency-Key is already used."
}

The 422 response returns the following error body:

{
  "id": "idempotency_key_invalid",
  "message": "Idempotency-Key is improperly formatted."
}

Limitations

The idempotency key TTL is set to one hour. If a second (duplicate) request is submitted over one hour after the original request, the idempotency key will have no effect and may result in a duplicate.

A 409 response does not replay the response from the original request. It returns only the error body described in Response Codes. If you need the result of the original request, fetch it directly. For example, if a request to PATCH /v1/accounts/{some_account_uuid}/ succeeds but the response is lost, call GET /v1/accounts/{some_account_uuid}/ to retrieve the updated data.