Retries and backoff for the WhatsApp API

Retry WhatsApp sends safely: which errors to retry, the 4^X backoff, honouring retry-after, a stable Idempotency-Key and the retries on your webhook.

Every integration that sends messages eventually meets a 429, a timeout or a 503, and what happens next decides whether the customer gets one message, none or three. Retrying correctly takes three ingredients: knowing which errors are worth retrying, spacing the attempts with backoff and making each attempt safe to repeat. This tutorial builds a retry wrapper around the CRPRO Hub's send endpoint and lists what Meta and the Hub say about limits and retries.

Which errors should be retried?

Short answer: 429, 503 and network failures yes; 5xx from the Meta side only after checking whether the message went out; 400, 401, 403 and 404 never.

ResponseMeaningAction
400 INVALID_MESSAGE, IDEMPOTENCY_KEY_*The request is wrong or the key is missing or malformedFix the request. Do not retry unchanged
409 IDEMPOTENCY_KEY_REUSEDSame key, different bodyUse a new key for a new message
429 RATE_LIMITEDA rate limit was exceededBack off, then retry with the same key
500 INTERNAL_ERRORUnexpected error in the HubRetry a few times; contact support with x-request-id if it persists
502 META_UNREACHABLE, 504 META_TIMEOUTMeta may or may not have received the requestCheck the message status before sending again
503 HUB_TEMPORARILY_UNAVAILABLEThe Hub lacked capacityWait for the retry-after header, then retry

The Hub's error guide gives the key detail for 503: when the response carries x-hub-retry-safe true, the request did not reach Meta, so repeating a send does not duplicate the message. For 502 and 504, the guide tells you not to repeat a send blindly, because Meta might have processed it. The reference for every code is in the documentation.

What limits trigger a 429?

Short answer: 60 messages per minute per channel, 120 requests per minute per organization and credential, and Meta's own throughput limit behind them.

Those two figures come from the Hub's limits table. Behind the Hub, Meta states that a business phone number can send up to 80 messages per second by default, with capacity upgrades available, and that sending more than one message every 6 seconds to the same user triggers error 131056. The Hub's per-channel cap of 60 per minute is far lower than 80 per second, so in practice you meet the Hub's limit first. A queue that releases messages at a steady pace avoids most 429s better than any retry logic.

How should the wait between attempts grow?

Short answer: exponentially, with a cap and some randomness. Meta's own guidance for failed sends is to retry after 4 to the power X seconds, starting at X=0 and increasing X after each failure.

That gives waits of 1, 4, 16 and 64 seconds. Adding jitter, a random fraction of the wait, is a common refinement and is not part of Meta's guidance; it prevents many workers from retrying at the same instant after a shared failure. When the response includes a retry-after header, as the Hub documents for 503, honour it instead of your own schedule.

const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) async function sendWithRetry(channelId, body, key, { maxAttempts = 5 } = {}) { for (let attempt = 0; attempt < maxAttempts; attempt++) { let res try { res = await fetch(`https://crprohub.com/api/v1/channels/${channelId}/messages`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.CRPRO_API_KEY}`, 'Idempotency-Key': key, // the SAME key on every attempt 'Content-Type': 'application/json', }, body: JSON.stringify(body), signal: AbortSignal.timeout(15_000), }) } catch { res = null // network failure or timeout } if (res?.status === 202) return (await res.json()).data.message_id if (res && [400, 401, 403, 404, 409].includes(res.status)) { throw new Error(`not retryable: ${res.status} ${await res.text()}`) } if (res && [502, 504].includes(res.status)) { throw new Error('unknown outcome: check message status before resending') } const retryAfter = Number(res?.headers.get('retry-after')) const base = Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter * 1000 : 4 ** attempt * 1000 await sleep(base + Math.random() * base * 0.25) // jitter } throw new Error('gave up after retries') }

Why must the Idempotency-Key stay the same?

Short answer: because it is what turns a retry into a safe operation. The same key with the same body returns the original response instead of sending again.

If a request succeeded but the response was lost, the next attempt with the same key returns the stored result, and the customer receives one message. A new key per attempt would send duplicates. The opposite mistake matters too: reusing a key with a different body returns 409 IDEMPOTENCY_KEY_REUSED, so a deliberate resend of changed content needs a new key. Derive the key from the business event, such as an order id plus notification type, so any worker or restart arrives at the same value.

What about retries on the receiving side?

Short answer: your endpoint is retried too. The Hub resends a failed delivery up to nine times, ten attempts over about four days, and waits up to 10 seconds for a 2xx. Meta retries its own deliveries for up to seven days.

Only a 2xx counts as success at the Hub. A timeout, an error or even a redirect is a failure and the delivery returns to the queue. After 20 consecutive failures the endpoint pauses automatically. Reply 2xx quickly, process in a queue and deduplicate on the event id, as the webhooks reference recommends. Retries can produce duplicates, so the receiving side needs the same idempotent mindset as the sending side.

How long does WhatsApp keep trying to deliver?

Short answer: Meta's Cloud API retries delivery of an undeliverable message for a time-to-live, 30 days by default. Authentication templates have a 10-minute TTL.

A 202 is therefore not a promise of immediate delivery. If no delivered status arrives before the TTL ends, assume the message was dropped. If the status is failed, allow a small buffer before assuming it was dropped. Track outcomes through message.status events rather than polling, and set your own business deadline for messages that become useless after a short time, such as a one-time code. The quickstart covers the first authenticated call.

What should be monitored around retries?

Short answer: the retry rate, the final failures and the age of the oldest queued message.

Retries hide problems as well as solving them. A send that succeeds on the third attempt looks fine in the result, while the retry counter tells you that a limit is being hit or a dependency is flaky. Track the share of sends that needed a retry, the number that gave up and the time between creation and acceptance. Alert when the oldest message in the queue is older than the business can tolerate, which catches a stuck worker long before customers complain. Keep the x-request-id header from failed responses in your logs; it is what support asks for.

  • Count attempts per message and the share that needed a retry.
  • Alert on final failures and on queue age.
  • Log x-request-id for every non-2xx response.
  • Cap total retry time so useless messages are dropped.
  • Review the pacing if 429 responses appear regularly.

How can retry logic be tested without hurting production?

Short answer: replace the network with a fake that returns a scripted sequence of responses, and assert on the attempts, the waits and the keys.

The wrapper is deterministic once the clock and the transport are injected. A test can script 429, 429, 202 and verify that the function made three attempts with the same Idempotency-Key and waited longer each time. Another scripts a 400 and checks that exactly one attempt was made. A third scripts a 502 and checks that the function stops and reports an unknown outcome instead of resending. Replace the sleep function with one that records the requested delay, so the suite runs in milliseconds and still verifies the backoff schedule.

Finally, try the whole path against a test channel at a low pace, and use the webhook test endpoint, limited to 10 per hour per endpoint, to rehearse the receiving side. Never provoke real rate limits on a production channel to see what happens.

  • Inject the transport and the sleep function.
  • Script 429, 503, 502, 400 and network errors.
  • Assert the same Idempotency-Key on every attempt.
  • Assert that non-retryable errors make one attempt only.
  • Check that the total retry time is capped.

Frequently asked questions

Is it safe to retry a send after a timeout?

It is safe when you reuse the same Idempotency-Key and body, because the Hub returns the original response for a repeated key. For 502 and 504 the Hub's guide advises checking the message status first, since Meta may have processed the request.

Is waiting for the retry-after header necessary?

Yes, when it is present. The Hub documents it for 503 responses. Use it in place of your own schedule, and fall back to exponential backoff otherwise.

What backoff formula does Meta suggest?

For failed sends, retry after 4 to the power X seconds, starting with X=0 and increasing X by 1 after each failure, until the request succeeds. Adding jitter is a common extra and not part of Meta's text.

How many times does the Hub retry a webhook delivery?

Up to nine retries after the first attempt, ten attempts in total over about four days. Only a 2xx response counts as success, and the endpoint pauses after 20 consecutive failures.

Fontes

Documentação oficial da Meta, conferida em 31 de outubro de 2026.