DEV Community

Cover image for What is idempotent API?
Aswin Arya
Aswin Arya

Posted on

What is idempotent API?

An Idempotent API is an API where making the same request multiple times produces the same result as making it once.

πŸ‘‰ In simple terms:
Repeat the request any number of times β€” the outcome won’t change.


Simple Definition

Idempotency = Same request β†’ Same result (no side effects after first call)


Example

Idempotent Operation

PUT /users/101
{
  "name": "Harish"
}
Enter fullscreen mode Exit fullscreen mode
  • First request β†’ updates user name
  • Second request β†’ still "Harish" (no extra change)

βœ” Result remains the same


Non-Idempotent Operation

POST /users
{
  "name": "Harish"
}
Enter fullscreen mode Exit fullscreen mode
  • First request β†’ creates user (ID: 101)
  • Second request β†’ creates another user (ID: 102)

Different result each time


Idempotent vs Non-Idempotent

Feature Idempotent API βœ… Non-Idempotent API ❌
Multiple Requests Same result Different results
Side Effects No additional effect Creates/changes data
Example Methods GET, PUT, DELETE POST

HTTP Methods and Idempotency

βœ” Idempotent Methods:

  • GET β†’ Fetch data (no change)
  • PUT β†’ Update/replace resource
  • DELETE β†’ Remove resource (once removed, same result)

Non-Idempotent:

  • POST β†’ Creates new resource each time

Real-Time Example

Payment API πŸ’³

Imagine a payment request:

POST /pay
Enter fullscreen mode Exit fullscreen mode

If not idempotent:

  • Multiple clicks β†’ Multiple charges 😱

If idempotent:

  • Only one payment processed βœ…

πŸ‘‰ That’s why idempotency is critical in real-world systems


Idempotency Key

To make APIs idempotent (especially POST), systems use an Idempotency Key.

POST /pay
Idempotency-Key: 12345
Enter fullscreen mode Exit fullscreen mode
  • Same key β†’ Same response
  • Prevents duplicate processing

Benefits of Idempotent APIs

  • Prevents duplicate operations
  • Safe retries (network failures)
  • Improves system reliability
  • Essential for distributed systems

Important Note

Even if response status differs (like 200 vs 404),
πŸ‘‰ The system state must remain the same


Conclusion

An Idempotent API ensures that repeating the same request does not cause unintended side effects. It’s a key concept in building robust, fault-tolerant systems, especially in microservices and payment systems.


Learn More

Want to master REST APIs, Java backend development, and real-time projects?

πŸ‘‰ No 1 Core JAVA Online Training in 2026


Top comments (0)