OptionalafterStatusCodes?: number[]OptionalbackoffLimit?: numberThe upper limit of the delay per retry in milliseconds.
To clamp the delay, set backoffLimit to 1000, for example.
By default, the delay is calculated in the following way:
0.3 * (2 ** (attemptCount - 1)) * 1000
The delay increases exponentially.
Optionaldelay?: (attemptCount: number) => numberA function to calculate the delay between retries given attemptCount (starts from 1).
Optionaljitter?: boolean | (delay: number) => numberAdd random jitter to retry delays to prevent thundering herd problems.
When many clients retry simultaneously (e.g., after hitting a rate limit), they can overwhelm the server again. Jitter adds randomness to break this synchronization.
Set to true to use full jitter, which randomizes the delay between 0 and the computed delay.
Alternatively, pass a function to implement custom jitter strategies.
import ky from 'ky';
const json = await ky('https://example.com', {
retry: {
limit: 5,
// Full jitter (randomizes delay between 0 and computed value)
jitter: true
// Percentage jitter (80-120% of delay)
// jitter: delay => delay * (0.8 + Math.random() * 0.4)
// Absolute jitter (±100ms)
// jitter: delay => delay + (Math.random() * 200 - 100)
}
}).json();
Optionallimit?: numberThe number of times to retry failed requests.
OptionalmaxRetryAfter?: numberIf the Retry-After header is greater than maxRetryAfter, the request will be canceled.
Optionalmethods?: HttpMethod[]The HTTP methods allowed to retry.
OptionalretryOnTimeout?: booleanWhether to retry when the request times out.
OptionalshouldRetry?: (state: ShouldRetryState) => boolean | undefined | Promise<boolean | undefined>A function to determine whether a retry should be attempted.
This function takes precedence over all other retry checks and is called first, before any other retry validation.
Note: This is different from the beforeRetry hook:
shouldRetry: Controls WHETHER to retry (called before the retry decision is made)beforeRetry: Called AFTER retry is confirmed, allowing you to modify the requestShould return:
true to force a retry (bypasses retryOnTimeout, status code checks, and other validations)false to prevent a retry (no retry will occur)undefined to use the default retry logic (retryOnTimeout, status codes, etc.)import ky, {HTTPError} from 'ky';
const json = await ky('https://example.com', {
retry: {
limit: 3,
shouldRetry: ({error, retryCount}) => {
// Retry on specific business logic errors from API
if (error instanceof HTTPError) {
const status = error.response.status;
// Retry on 429 (rate limit) but only for first 2 attempts
if (status === 429 && retryCount <= 2) {
return true;
}
// Don't retry on 4xx errors except rate limits
if (status >= 400 && status < 500) {
return false;
}
}
// Use default retry logic for other errors
return undefined;
}
}
}).json();
OptionalstatusCodes?: number[]The HTTP status codes allowed to retry.
The HTTP status codes allowed to retry with a
Retry-Afterheader.