const BASE_URL = "https://api.suwappu.bot/v1/agent";
const API_KEY = "suwappu_sk_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6";
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`,
};
async function requestWithRetry(
method: string,
url: string,
body?: object,
maxRetries = 3
): Promise<any> {
for (let attempt = 0; attempt < maxRetries; attempt++) {
const response = await fetch(url, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
// Log rate limit status
const remaining = response.headers.get("X-RateLimit-Remaining");
const limit = response.headers.get("X-RateLimit-Limit");
if (remaining !== null) {
console.log(`Rate limit: ${remaining}/${limit} requests remaining`);
}
// If not rate limited, return the response
if (response.status !== 429) {
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return response.json();
}
// Rate limited — wait and retry
const retryAfter = parseInt(response.headers.get("Retry-After") ?? "5", 10);
console.log(
`Rate limited. Retrying in ${retryAfter}s (attempt ${attempt + 1}/${maxRetries})`
);
await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
}
throw new Error("Max retries exceeded due to rate limiting");
}
// Usage
const quote = await requestWithRetry("POST", `${BASE_URL}/quote`, {
from_token: "USDC",
to_token: "ETH",
amount: "500.00",
chain: "ethereum",
});
console.log(quote);