# retry-request

> Retry a request.

Latest version **9.0.1** (published 2026-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install retry-request
pnpm add retry-request
yarn add retry-request
bun add retry-request
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 9.0.1 |
| Published | 2026-08-11 |
| First published | 2015-07-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=22 |
| Dependencies | 2 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3195 |
| Author | Stephen Sawchuk |
| Maintainers | stephenplusplus, google-wombot |
| Keywords | request, retry, stream |

## Links

- npm: https://www.npmjs.com/package/retry-request
- Repository: https://github.com/googleapis/google-cloud-node
- Homepage: https://github.com/googleapis/google-cloud-node/tree/main/core/packages/retry-request
- Issues: https://github.com/googleapis/google-cloud-node/issues
- npm.io page: https://npm.io/package/retry-request

## Dependencies (2)

- [extend](https://npm.io/package/extend.md) ^3.0.2
- [teeny-request](https://npm.io/package/teeny-request.md) ^11.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 9.0.1 (latest) — 2026-08-11
- 8.0.4 (legacy-18) — 2026-07-23
- 7.0.2 (legacy-14) — 2024-01-19
- 5.0.2 (legacy-12) — 2022-09-14
- 4.2.2 (legacy-10) — 2021-07-09
- 9.0.0 — 2026-07-30
- 8.0.3 — 2026-06-04
- 8.0.2 — 2025-08-07
- 8.0.1 — 2025-08-05
- 8.0.0 — 2025-02-19
- 7.0.1 — 2023-10-12
- 7.0.0 — 2023-10-11
- 6.0.0 — 2023-07-20
- 5.0.1 — 2022-06-13
- 5.0.0 — 2022-05-09
- … 32 more at https://npm.io/package/retry-request/versions

## README

**_THIS REPOSITORY AND PACKAGE WILL BE DEPRECATED IN JULY 2024. RELEVANT FUNCTIONALITIES HAVE BEEN MOVED TO [GOOGLEAPIS/GAXIOS](https://github.com/googleapis/gaxios)_**

|![retry-request](logo.png)
|:-:
|Retry a [request][request] with built-in [exponential backoff](https://developers.google.com/analytics/devguides/reporting/core/v3/coreErrors#backoff).

```sh
$ npm install --save teeny-request
$ npm install --save retry-request
```

```js
var request = require('retry-request', {
  request: require('teeny-request'),
});
```

It should work the same as `request` and `teeny-request` in both callback mode and stream mode.

Note: This module only works when used as a readable stream, i.e. POST requests aren't supported ([#3](https://github.com/googleapis/retry-request/issues/3)).

## Do I need to install `request`?

Yes! You must independently install `teeny-request` OR `request` (_deprecated_) and provide it to this library:

```js
var request = require('retry-request', {
  request: require('teeny-request'),
});
```

#### Callback

`urlThatReturns503` will be requested 3 total times before giving up and executing the callback.

```js
request(urlThatReturns503, function (err, resp, body) {});
```

#### Stream

`urlThatReturns503` will be requested 3 total times before giving up and emitting the `response` and `complete` event as usual.

```js
request(urlThatReturns503)
  .on('error', function () {})
  .on('response', function () {})
  .on('complete', function () {});
```

## Can I monitor what retry-request is doing internally?

Yes! To enable the debug mode, set the environment variable `DEBUG` to _retry-request_.

(Thanks for the implementation, @yihaozhadan!)

## request(requestOptions, [opts], [cb])

### requestOptions

Passed directly to `request` or `teeny-request`. See the list of options supported:

- https://github.com/request/request/#requestoptions-callback
- https://github.com/googleapis/teeny-request#teenyrequestoptions-callback

### opts _(optional)_

#### `opts.noResponseRetries`

Type: `Number`

Default: `2`

The number of times to retry after a response fails to come through, such as a DNS resolution error or a socket hangup.

```js
var opts = {
  noResponseRetries: 0,
};

request(url, opts, function (err, resp, body) {
  // url was requested 1 time before giving up and
  // executing this callback.
});
```

#### `opts.objectMode`

Type: `Boolean`

Default: `false`

Set to `true` if your custom `opts.request` function returns a stream in object mode.

#### `opts.retries`

Type: `Number`

Default: `2`

```js
var opts = {
  retries: 4,
};

request(urlThatReturns503, opts, function (err, resp, body) {
  // urlThatReturns503 was requested a total of 5 times
  // before giving up and executing this callback.
});
```

#### `opts.currentRetryAttempt`

Type: `Number`

Default: `0`

```js
var opts = {
  currentRetryAttempt: 1,
};

request(urlThatReturns503, opts, function (err, resp, body) {
  // urlThatReturns503 was requested as if it already failed once.
});
```

#### `opts.shouldRetryFn`

Type: `Function`

Default: Returns `true` if [http.incomingMessage](https://nodejs.org/api/http.html#http_http_incomingmessage).statusCode is < 200 or >= 400.

```js
var opts = {
  shouldRetryFn: function (incomingHttpMessage) {
    return incomingHttpMessage.statusMessage !== 'OK';
  },
};

request(urlThatReturnsNonOKStatusMessage, opts, function (err, resp, body) {
  // urlThatReturnsNonOKStatusMessage was requested a
  // total of 3 times, each time using `opts.shouldRetryFn`
  // to decide if it should continue before giving up and
  // executing this callback.
});
```

#### `opts.request`

Type: `Function`

If we not provided we will throw an error advising you to provide it.

_NOTE: If you override the request function, and it returns a stream in object mode, be sure to set `opts.objectMode` to `true`._

```js
var originalRequest = require('teeny-request').defaults({
  pool: {
    maxSockets: Infinity,
  },
});

var opts = {
  request: originalRequest,
};

request(urlThatReturns503, opts, function (err, resp, body) {
  // Your provided `originalRequest` instance was used.
});
```

#### `opts.maxRetryDelay`

Type: `Number`

Default: `64`

The maximum time to delay in seconds. If retryDelayMultiplier results in a delay greater than maxRetryDelay, retries should delay by maxRetryDelay seconds instead.

#### `opts.retryDelayMultiplier`

Type: `Number`

Default: `2`

The multiplier by which to increase the delay time between the completion of failed requests, and the initiation of the subsequent retrying request.

#### `opts.totalTimeout`

Type: `Number`

Default: `600`

The length of time to keep retrying in seconds. The last sleep period will be shortened as necessary, so that the last retry runs at deadline (and not considerably beyond it). The total time starting from when the initial request is sent, after which an error will be returned, regardless of the retrying attempts made meanwhile.

### cb _(optional)_

Passed directly to `request`. See the callback section: https://github.com/request/request/#requestoptions-callback.

[request]: https://github.com/request/request

---
_Source: https://npm.io/package/retry-request · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
