Guides
Safe retries with idempotency keys
The API supports idempotency for POST, PUT, and PATCH requests, allowing safe retries without duplicating side effects.
Include an Idempotency-Key header with a unique value (e.g., a UUID):
curl -X POST \
-H "Authorization: Bearer $KEY" \
-H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
-H "Content-Type: application/json" \
-d '{"amount": 500, "type": "merchantPayment"}' \
https://api.smartmca.com/api/public/v1/deals/clx.../payments
POST, PUT, and PATCH requestsGET and DELETE are naturally idempotentasync function createPaymentWithRetry(dealId: string, amount: number) {
const idempotencyKey = crypto.randomUUID();
for (let attempt = 0; attempt < 3; attempt++) {
try {
const response = await fetch(
`https://api.smartmca.com/api/public/v1/deals/${dealId}/payments`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey,
},
body: JSON.stringify({ amount, type: 'merchantPayment' }),
},
);
if (response.ok) return response.json();
if (response.status === 429) {
const retryAfter = Number(response.headers.get('Retry-After')) || 5;
await sleep(retryAfter * 1000);
continue;
}
throw new Error(`API error: ${response.status}`);
} catch (err) {
if (attempt === 2) throw err;
await sleep(1000 * Math.pow(2, attempt));
}
}
}