Idempotency

Idempotency ensures that retrying a request multiple times has the same effect as making it once. This prevents accidental duplicate charges, transfers, or other critical operations.

What is Idempotency?

Idempotency keys allow you to safely retry API requests without worrying about performing the same operation twice. When you include an idempotency key with a request, Devdraft will return the same result for subsequent requests with the same key.
Idempotency flow diagram showing request deduplication

How It Works

1

Generate Unique Key

Create a unique idempotency key for each operation (recommended: UUID)
2

Include in Request

Add the key to your API request header as Idempotency-Key
3

Safe Retries

If the request fails due to network issues, retry with the same key
4

Consistent Results

Devdraft returns the same response for requests with the same key

Supported Endpoints

Idempotency is supported on all state-changing operations:

Payment Operations

  • Create payment intents
  • Process payments
  • Refund transactions
  • Cancel payments

Account Operations

  • Create customers
  • Update customer data
  • Create invoices
  • Generate payment links

Transfer Operations

  • Initiate transfers
  • Create withdrawals
  • Process deposits
  • Currency conversions

Product Operations

  • Create products
  • Update inventory
  • Process orders
  • Manage catalog

Implementation

// Generate a unique idempotency key
const idempotencyKey = crypto.randomUUID();

// Include in API request
const response = await fetch('https://api.devdraft.ai/v1/payments', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
    'Idempotency-Key': idempotencyKey
  },
  body: JSON.stringify({
    amount: 1000,
    currency: 'USD',
    customer_id: 'cust_123'
  })
});

Best Practices

Key Behavior

Same Key, Same Result:When you retry a successful request with the same idempotency key:
  • Returns the original response
  • No additional processing occurs
  • Same HTTP status code
  • Identical response body
Response Example
{
  "id": "pay_123456789",
  "amount": 1000,
  "currency": "USD",
  "status": "succeeded",
  "created_at": "2024-01-15T10:30:00Z",
  "idempotency_key": "uuid-original-key"
}

Common Use Cases

Testing Idempotency

1

Create Test Scenario

Set up a test that makes the same request multiple times with the same idempotency key
2

Verify Response Consistency

Ensure all responses are identical (status, body, headers)
3

Check Side Effects

Confirm that the operation only occurred once (e.g., only one charge created)
4

Test Error Scenarios

Verify proper handling of conflicting keys and parameter mismatches
Use idempotency keys for all critical operations, especially in production environments where network issues and retries are common.
Idempotency keys are cached for 24 hours. After this period, using the same key will create a new operation rather than returning the cached result.