Skip to main content

index/functions/retryableHttpRequest.md

retryableHttpRequest<T>(fn, options?): Promise<T>

HTTP request wrapper with automatic retry logic

Wraps async functions with exponential backoff retry on specific HTTP status codes. Helps handle transient failures like rate limiting, timeouts, and temporary service issues.

Type Parameters​

Type ParameterDescription
TType of the promised value

Parameters​

ParameterTypeDescription
fn() => Promise<T>Async function to execute with retries
options?{ backoffMs?: number; maxRetries?: number; retryableStatus?: number[]; }Retry configuration
options.backoffMs?numberInitial backoff delay in milliseconds (default: 100)
options.maxRetries?numberMaximum number of retry attempts (default: 3)
options.retryableStatus?number[]HTTP status codes to retry (default: [429, 503, 504])

Returns​

Promise<T>

Result of the function or final error after max retries

Throws​

Error if all retries exhausted or error is not retryable

Examples​

Retry on rate limit (429)

const result = await retryableHttpRequest(
() => sdk.fetchTokenPrice('anime'),
{ maxRetries: 3, backoffMs: 100, retryableStatus: [429, 503] }
);

With default retry options

const result = await retryableHttpRequest(
() => http.get('/api/pool-data')
);
// Retries on 429, 503, 504 with default exponential backoff

Fetch with custom retry behavior

try {
const data = await retryableHttpRequest(
() => fetch(url).then(r => r.json()),
{ maxRetries: 5, backoffMs: 200, retryableStatus: [429, 500, 502, 503, 504] }
);
} catch (error) {
console.log('Failed after all retries:', error);
}

Since​

6.5.0 @adoption_gap Manual retry logic appears in 3+ locations