async function sendWithRetry(payload, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const response = await fetch(`$https://api.buildwithchirp.com/v1/sms`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (response.status === 429) {
const retryAfter = response.headers.get("Retry-After");
await new Promise((r) => setTimeout(r, retryAfter * 1000));
continue;
}
return await response.json();
} catch (error) {
if (i === maxRetries - 1) throw error;
await new Promise((r) => setTimeout(r, Math.pow(2, i) * 1000));
}
}
}