Idempotency

Prevent duplicate operations with idempotency keys

Idempotency ensures that repeated requests with the same parameters produce the same result, preventing duplicate operations. This is critical for operations like order creation where network issues might cause retries.


Idempotent Endpoints

API
Endpoint
Requires Idempotency Key

InternalApi

POST /orders

Required

CustomerApi

POST /orders

Required

circle-exclamation

Using the Idempotency Key

Include the Idempotency-Key header with a unique UUID:

curl -X POST https://api.partssource.com/internal/api/orders \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
  -d '{
    "requesterId": 12345,
    "userId": 67890,
    "facilityId": 1001,
    "shippingAddressId": 2001,
    "billingAddressId": 3001,
    "shippingMethod": "GROUND",
    "quoteItems": [
      {"priceOptionId": "OPT-001", "productId": "CAT-123", "quantity": 1, "price": 100.00}
    ]
  }'

Key Requirements

Requirement
Details

Format

UUID with dashes: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Uniqueness

Must be unique per logical operation

Generation

Generate client-side using UUID v4

Reuse

Same key can be reused for retries of the same operation


How It Works


Request Body Validation

The InternalApi validates that retry requests have the same body as the original:


Caching Behavior

Response Code
Cached?
Duration
Retry Behavior

2xx

Yes

24 hours

Returns cached success

4xx

No

-

Allows retry after fixing validation errors

5xx

Yes

24 hours

Returns cached error

circle-info

Why are 4xx responses not cached?

This allows you to fix validation errors and retry with the same idempotency key. If you send invalid data, correct it and retry—no need to generate a new key.


Response Header

When a cached response is returned, the API includes a special header:

Check for this header to know if your response came from cache:


Concurrent Request Handling

If a request is still processing when a retry arrives:

Status Code: 409 Conflict

Action: Wait a few seconds and retry. The original request should complete shortly.


Error Responses

Status
Error
Cause
Solution

400

Header missing

No Idempotency-Key provided

Add the header

400

Invalid format

Key is not a valid UUID

Use format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

409

Already processing

Request is in progress

Wait and retry

409

Body mismatch

Key reused with different body

Use a new key for different operations


Code Examples

C#

TypeScript

Python


Best Practices

  1. Generate keys client-side using UUID v4 (or equivalent)

  2. Store keys with the operation for retry scenarios

  3. Don't reuse keys for different operations

  4. Implement retry logic with exponential backoff

  5. Check X-Idempotency-Cached header to know if response was cached

  6. Log idempotency keys with your order records for debugging


Retry Pattern Example

Last updated