Core concepts
Safe retries with Idempotency-Key
Repeat a request after a timeout without anything happening twice.
Networks fail in the middle of things. Your request may have reached RadiPay and been carried out even though you never saw the answer. If you simply send it again, you could create two payment links or two orders. An Idempotency-Key removes that risk: the same key always produces the same single result.
How it works
Add the header to any request that changes something (POST, PUT, PATCH, DELETE). Choose a value that identifies the thing you are doing, such as your own order number, or generate a UUID and store it with the order before you send.
curl -X POST https://prod.pay.radingtechnologies.co.ke/payment-links \
-H "Authorization: Bearer $RADIPAY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1182-link" \
-d '{
"title": "Order 1182",
"amountMinor": 150000
}'- The first request with a key is carried out and its answer is kept.
- A repeat with the same key and the same body is not carried out again. You get the kept answer, with the header
Idempotent-Replayed: trueso you can tell. - Answers are kept for at least 24 hours. After that the key is forgotten and would run as new.
- If the first request failed, nothing is kept. Fix the problem and send again with the same key.
The key
| Rule | Detail |
|---|---|
| Length | 8 to 100 characters. |
| Characters | Letters, digits, dashes and underscores. |
| Belongs to | Your organisation. Make the value unique across all your systems, for example by starting it with the system name. |
| Is tied to | The method, the path and the exact body of the first request. |
When it is refused
| Status | Code | What to do |
|---|---|---|
| 422 | IDEMPOTENCY_KEY_INVALID | The key is too short, too long, or has other characters in it. Fix the key. |
| 422 | IDEMPOTENCY_KEY_REUSED | This key was already used for a different request (another path or another body). Use a new key for a new action. |
| 409 | IDEMPOTENCY_IN_PROGRESS | The first request with this key is still being carried out. Wait a second or two and send the same request again. |
A retry loop that is safe
async function createLink(order) {
const key = 'order-' + order.id + '-link'; // the same for every attempt
for (let attempt = 1; attempt <= 4; attempt++) {
try {
const res = await fetch('https://prod.pay.radingtechnologies.co.ke/payment-links', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.RADIPAY_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': key,
},
body: JSON.stringify({ title: 'Order ' + order.id, amountMinor: order.totalMinor }),
signal: AbortSignal.timeout(15000),
});
const body = await res.json();
if (res.ok) return body.data;
// Worth another try: still running, too many requests, or a fault on our side.
const again = res.status === 409 || res.status === 429 || res.status >= 500;
if (!again) throw new Error(body.error.code + ': ' + body.error.message);
} catch (e) {
if (attempt === 4) throw e; // a timeout or dropped connection lands here
}
await new Promise((r) => setTimeout(r, 500 * 2 ** attempt)); // 1s, 2s, 4s
}
}Reading is always safe to repeat
A
GET changes nothing, so it needs no key. The header is ignored on GET requests.