Skip to content

Idempotency

A network drops, a timeout fires, you never get the response. Do you retry? With an Idempotency-Key, yes, safely: the same operation is not done twice nor charged twice. It is honored by the short link creation and by SMS sends. It is optional, and we recommend it: without it, every request is a new operation and a retry can duplicate the send and the charge.

5 min readReviewed on

What it is

Idempotency guarantees that repeating a request has the same effect as making it once. You generate a unique identifier per operation and send it in the Idempotency-Key header. If your organization already created a link with that key, the API returns that link instead of creating another one, and does not charge it again.

The key does not expire: it is stored with the link it created, forever, and it is unique within your organization. Retrying a minute later or a month later is equally safe. The other side: a used key cannot serve another link, not even months later.

Optional, and your responsibility if you do not use it

The key is a tool Hablame gives you so that a retry, or a programming bug that repeats the same request over and over, does not duplicate sends or charges. It is optional on every Hablame API. On the endpoints that honor it (below, in "Where it applies today"), if you send it we take it into account and do not repeat what was already done; on the others it has no effect. If you do not send it, every request is a new operation: it runs and, if it is charged, it is charged, and anything a retry duplicates is your responsibility. No API answers with an error for not sending it.

Think of the key as "one per business intent", not "one per retry". Retrying the same send reuses the same key. Two different sends carry two different keys.

How to use it

  1. Generate a unique identifier per operation. We recommend a UUID v4.
  2. Send it in the request Idempotency-Key header.
  3. If the operation fails on the network, times out or gets a 5xx, retry it with the same key and the same body.
  4. For a different operation, use a new key.

The key accepts 1 to 255 characters from the set A-Z a-z 0-9 _ -. Scope is per organization: your key never collides with another account. An empty header is the same as not sending it.

Examples

You only add one header. The body and the rest of the request stay the same.

curl -X POST https://developers.hablame.co/api/v6/urlshortener/links \
  -H "Authorization: Bearer hk_TU_API_KEY" \
  -H "Idempotency-Key: 5f3b2c10-9a7e-4b2d-8c1f-0a1b2c3d4e5f" \
  -H "Content-Type: application/json" \
  -d '{"longUrl":"https://example.com/promo"}'

Responses

The API tells you what happened with each key in the Idempotency-Status header.

New: it ran

The first time. The link is created, charged (unless your plan includes it) and the key is stored with it.

HTTP
HTTP/1.1 201 Created
Idempotency-Status: created

{ "success": true, "data": { "domain": "h0b.co", "code": "a1b2c3d", "shortUrl": "https://h0b.co/a1b2c3d", "status": "active" } }

Repeated: replay

A retry with the same key and the same body. No other link is created, nothing is charged again and it does not use your plan’s daily quota: you get the same 201 with the link that already exists. It comes as it is today: if you edited it later, with its new destination; if you deleted it, with status: deleted. It counts as the same body even if the alias casing, the spaces around the destination or the time zone you write the same expiry in change.

HTTP
HTTP/1.1 201 Created
Idempotency-Status:   replayed
Idempotency-Replayed: true

{ "success": true, "data": { "domain": "h0b.co", "code": "a1b2c3d", "shortUrl": "https://h0b.co/a1b2c3d", "status": "active" } }

Two at once: a single link

If you send the same key while the first request is still being processed (for example, because your client got tired of waiting and retried), you do not get an error and you do not have to wait: both end with the same link. Only one is created and charged, and the one that arrives second responds Idempotency-Status: replayed.

Reused with a different body: 422

You used a key your organization already used, with a different body. Nothing is created or charged. It is almost always a client bug: one key identifies one operation, not several. Use a new key.

HTTP
HTTP/1.1 422 Unprocessable Entity

{ "success": false, "error": { "code": "IDEMPOTENCY_KEY_REUSED" } }

Invalid key: 400

The key is too long or contains characters outside A-Z a-z 0-9 _ -. Nothing is done.

HTTP
HTTP/1.1 400 Bad Request

{ "success": false, "error": { "code": "IDEMPOTENCY_KEY_INVALID" } }

Tips for automatic retries

  • Generate the key before the first attempt. Store it and reuse it on every retry of the same event. Generating a new key per attempt loses the protection.
  • On a timeout, a network error or a 5xx, retry with the same key and the same body: if the link got created, you receive it; if not, it is created now. In no case is it charged twice.
  • Treat a 422 as a bug on your side: you changed the body under the same key. Fix the logic, do not retry in a loop.
  • Retries use the endpoint quota like any other request: use exponential backoff with random jitter, just like with usage limits.

The key is only stored when a link is created. If the first request failed with an error (an invalid value, insufficient balance, a taken alias), nothing was created and the key is still free: the retry runs from scratch and, if the problem persists, gets the same error. Errors are not stored to be replayed.

Where it applies today

It is honored by the short link creation (POST /api/v6/urlshortener/links and the bulk creation) and by the three SMS sends (POST /api/v6/sms/priority, POST /api/v6/sms/marketing and POST /api/v6/sms/bulk), with the rules of the next section. In bulk link creation the key belongs to the whole batch: when you repeat it, the links already created come back as replayed (without being created or charged again) and whatever was missing is tried again. On other endpoints the header has no effect: a retry runs again and, if the operation is charged, it is charged again.

On SMS sends

An SMS that goes out twice reaches a person’s phone twice and is charged twice. That is why we recommend always sending a key on SMS sends. It is optional: without it, every request is a new send, which goes out and is charged, even if the body is the same. If your program retries without a key (after a timeout, a 503 or a bug that makes it repeat), the message can arrive and be charged twice or more, and that is your responsibility. The shape is the same as the rest of the platform: 1 to 255 characters from A-Z a-z 0-9 _ -; we recommend a UUID v4.

Same key and same body: the same response

Within that period, repeating the send with the same key and the same body returns the first response (the same id or the same batchId, the same parts, the same cost) without sending or charging another one. With another body, 422 IDEMPOTENCY_KEY_REUSED: nothing is sent.

Two at once: 409 and you retry

Here SMS differs from the URL shortener: if you send the same key while the first request is still being processed, the second one does not wait: it gets 409 IDEMPOTENCY_IN_PROGRESS with Retry-After. Retry with the same key after that wait and you will get the first one’s response. Only one message is sent and charged.

HTTP
HTTP/1.1 409 Conflict
Retry-After: 2

{ "success": false, "error": { "code": "IDEMPOTENCY_IN_PROGRESS" } }

A 503 or a timeout: the same key

A 503 (or a response that never reaches you) does not mean the send did not go out. If the wait ran out or it could not be confirmed, the send may have been received and charged. Retry after Retry-After with the same key and the same body: if it had already been received, you get its 202 without a new charge; if not, it is received on that attempt, only once. Do not resend it with another key or through another route: it would be another send, sent and charged twice. 409 IDEMPOTENCY_IN_PROGRESS and 429 ask you to wait and retry with the same key. A 4xx rejection says nothing was sent or charged in THAT request. If it arrives when retrying after a 503 or a timeout, it says nothing about the first attempt, which may have been received and charged: for example, a 403 because your organization was suspended or the key lost the service. Do not resend it through another route. Once it is resolved, retry with the same key: within its validity you get the first one’s 202 if it had been received.

A rejected bulk send keeps its key

If a bulk send was rejected as a whole (no balance for all of it, a spend limit, no valid destination), the key stays with that rejection: repeating it with the same key returns the same error, it does not try again. After topping up or fixing the list, send it with a new key. Three responses are also final for that key, even though one is a 5xx: 409 SMS_BULK_ABANDONED (the bulk send was cut off halfway through being received and the platform discarded it) and 500 SMS_BULK_NOT_RECEIVED (it could not be stored) go with a new key; 409 SMS_CURRENCY_MISMATCH is not fixed by retrying: write to us. None of the three was charged. Branch on error.code, not only on the HTTP status.

One key per send, not per retry

Generate the key before the first attempt and store it with the send. If each retry carries a new key, each retry is another SMS: it reaches the phone again and is charged again.

Need help?

Was this page helpful?

See all support channels

Your cookie preferences

You decide what we measure. You can change your choice at any time. Rejecting takes no feature away from the documentation.

Privacy policy