#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read
•Question 11 of 28medium

What is idempotency in APIs?

Understanding idempotent operations.

What You'll Learn

  • What idempotency means
  • Why it matters
  • Idempotent HTTP methods

What is Idempotency?

An operation is idempotent if making the same request multiple times has the same effect as making it once.

Why It Matters

  • Safe retries on network failures
  • Prevents duplicate operations
  • Critical for payment/transaction APIs

HTTP Methods

MethodIdempotentSafe
GETYesYes
HEADYesYes
PUTYesNo
DELETEYesNo
POSTNoNo
PATCHNo*No

*PATCH can be idempotent depending on implementation

Examples

code.jsJavaScript
// Idempotent - same result every time
PUT /users/1 { "name": "John" }
// Always sets name to "John"

DELETE /users/1
// First call deletes, subsequent calls return 404

GET /users/1
// Always returns same user

// NOT Idempotent
POST /orders { "item": "book", "qty": 1 }
// Creates new order each time!

Implementing Idempotency

code.jsJavaScript
// Use idempotency keys for POST
POST /payments
Idempotency-Key: unique-request-id-123
{ "amount": 100 }

// Server checks if key exists
// If yes, return cached response
// If no, process and store response