What is an idempotency key?

An idempotency key is a unique value sent with a request so the server performs it once, even if retried. Why WhatsApp sends need one, with an example.

Short answer

An idempotency key is a unique value a client sends with a request so the server performs the action at most once, even when the same request is retried. For WhatsApp sends, it stops a timeout or retry from delivering the same message twice.

How does an idempotency key work?

The client creates one key per logical action, such as "send the order 4521 confirmation", and sends it in an Idempotency-Key header. The server stores the key with the result. If the same key arrives again with the same body, the server returns the saved result instead of acting twice. If the body differs, it rejects the request as a conflict.

Example

Your server sends a WhatsApp message, the network times out and your retry library tries again. Without a key the customer could get the message twice. With the same key, the second attempt returns the first result.

bash
curl -X POST "$SUPERSKOPE_URL/api/v1/chats/$CHAT_ID/messages" \
  -H "Authorization: Bearer $SUPERSKOPE_API_KEY" \
  -H "Idempotency-Key: order-4521-confirmation" \
  -H "Content-Type: application/json" \
  -d '{"text":"Your order 4521 is confirmed."}'

Copy-only example. This page never runs a request or asks for a credential.

Rules that avoid duplicates

  • Create the key from the business event, not from the attempt, and store it before sending.
  • Never reuse a key for a different message or operation.
  • If the result is unknown, look it up instead of sending again. SuperSkope marks interrupted sends as unknown and never retries them automatically.

Frequently asked questions

What happens if I reuse a key with a different body?

The server should refuse it. The IETF draft for the Idempotency-Key header recommends a 422 response, and SuperSkope returns a conflict for a changed body.

How long are idempotency keys kept?

It depends on the API. SuperSkope lets a key look up the result of a request for 90 days.

Sources

  1. IETF draft: The Idempotency-Key HTTP Header Field

Bring your WhatsApp work together.

Start a 14-day free trial with Starter features, link your number and invite your team. No card required.