Idempotency Keys
Add anIdempotency-Key header to your request. If you execute the same request twice with the same key, the system will skip processing and return the original successful response.
How it works
- When you send a request with an idempotency key, the ledger stores a hash of the request along with the response
- If you retry with the same key and identical request, the ledger returns the cached response with an
Idempotency-Hit: trueheader - If you retry with the same key but different request parameters, the ledger returns a validation error
Supported endpoints
Idempotency keys work on all write endpoints:- Create transaction (including batch and Numscript)
- Update metadata (account or transaction)
- Revert transaction
Scope
Idempotency keys are scoped to a specific ledger. The same key can be used on different ledgers without conflict.
Example
References
Areference is a field in the transaction body that acts as a unique identifier. If a transaction with the same reference already exists, the ledger returns an error.
Example
Choosing Between Idempotency Keys and References
| Use Case | Recommended | Reason |
|---|---|---|
| Retry logic for network failures | Idempotency Key | System returns success on retry |
| Mapping to external entities (e.g., orders, refunds) | Reference | Get an error if duplicate, useful for debugging |
| Unknown or dynamic transaction identity | Idempotency Key | Works with any unique value |
Validation Errors
Starting with Ledger v2.2, you will receive aVALIDATION error when reusing an idempotency key with different transaction parameters than the original request.
Error format
Common causes
- Changing any field in the request body (including metadata)
- Using a different endpoint path
- Modifying the request after a previous successful call
Best practices
- Use unique keys for each distinct transaction - Generate a new key for each new transaction
- Keep request parameters consistent - If retrying, ensure all parameters match the original request exactly
- Use a new key if parameters change - If you need to modify the transaction, generate a new idempotency key
If a transaction was created with Ledger v2.1 or earlier, the idempotency hash stored in the database is empty. In this case, the hash validation check is skipped for backward compatibility.