Get started
Retries & idempotency
Networks fail. When a request times out you can't tell whether it reached Loyvo, so every write can be retried safely without sending an SMS twice or awarding points twice.
SMS: the Idempotency-Key header
Send an Idempotency-Key header on every POST: any unique string up to 100 characters, such as a UUID or your own order ID plus the event.
Repeating the request with the same key and body within 24 hours returns the original response, with the header Idempotent-Replayed: true. The message is never sent twice. Reusing a key with a different body returns 422 idempotency_key_reused; retrying while the first request is still running returns 409 request_in_progress (retry shortly).
curl -X POST https://api.loyvo.lk/v1/sms/send \
-H "Authorization: Bearer lvo_test_xxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Idempotency-Key: order-1055-ready" \
-H "Content-Type: application/json" \
-d '{"sender_id": "LOYVO", "to": "0771234567", "message": "Order A-101 is ready."}'Loyalty and Wallet: check before retrying a purchase
Purchase and redemption calls don't take an Idempotency-Key yet, and a repeated invoice number is not rejected. If a purchase call times out, check before sending it again: look the transaction up with Get transaction details or look the customer up and compare their balance.
Always send your invoice number (invoice_id on the Loyalty API, invoiceId on the Wallet API) so every purchase can be traced and corrected later.
Redemptions are two steps (start, then confirm with the customer's PIN). If a confirm times out, look the customer up before trying again, or cancel the redemption to give the points back.
When to retry
- Retry on network errors, timeouts,
429and5xx. - Don't retry other
4xxerrors: fix the request first. - On
429, wait for theRetry-Afterseconds. Otherwise back off exponentially (1, 2, 4, 8 seconds). - Rate limits apply per API key. Spread large sends with
/v1/sms/bulk(up to 10,000 numbers in one request) instead of many single sends.
async function withRetry(send, attempts = 4) {
for (let i = 0; ; i++) {
const res = await send();
const retryable = res.status === 429 || res.status >= 500;
if (!retryable || i === attempts - 1) return res;
const wait = Number(res.headers.get("Retry-After")) || 2 ** i; // seconds
await new Promise((r) => setTimeout(r, wait * 1000));
}
}