# agent-base

> Turn a function into an `http.Agent` instance

Latest version **9.0.0** (published 2026-04-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install agent-base
pnpm add agent-base
yarn add agent-base
bun add agent-base
```

## Health

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

Positive: has types package; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 9.0.0 |
| Published | 2026-04-01 |
| First published | 2013-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/agent-base) |
| Module format | ESM |
| Node | >= 20 |
| Dependencies | 0 |
| Unpacked size | 20.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1162 |
| Author | Nathan Rajlich |
| Maintainers | tootallnate |
| Keywords | http, agent, base, barebones, https |

## Links

- npm: https://www.npmjs.com/package/agent-base
- Repository: https://github.com/TooTallNate/proxy-agents
- Homepage: https://github.com/TooTallNate/proxy-agents#readme
- Issues: https://github.com/TooTallNate/proxy-agents/issues
- npm.io page: https://npm.io/package/agent-base

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 9.0.0 (latest) — 2026-04-01
- 8.0.0 — 2026-03-11
- 7.1.4 — 2025-07-07
- 7.1.3 — 2024-12-08
- 7.1.2 — 2024-12-07
- 7.1.1 — 2024-03-30
- 7.1.0 — 2023-05-24
- 7.0.2 — 2023-05-18
- 7.0.1 — 2023-05-05
- 7.0.0 — 2023-05-04
- 6.0.2 — 2020-10-23
- 6.0.1 — 2020-07-02
- 6.0.0 — 2020-01-23
- 5.1.1 — 2019-12-11
- 5.1.0 — 2019-12-11
- … 18 more at https://npm.io/package/agent-base/versions

## README

agent-base
==========
### Turn a function into an [`http.Agent`][http.Agent] instance

This module is a thin wrapper around the base `http.Agent` class.

It provides an abstract class that must define a `connect()` function,
which is responsible for creating the underlying socket that the HTTP
client requests will use.

The `connect()` function may return an arbitrary `Duplex` stream, or
another `http.Agent` instance to delegate the request to, and may be
asynchronous (by defining an `async` function).

Instances of this agent can be used with the `http` and `https`
modules. To differentiate, the options parameter in the `connect()`
function includes a `secureEndpoint` property, which can be checked
to determine what type of socket should be returned.

#### Some subclasses:

Here are some more interesting uses of `agent-base`.
Send a pull request to list yours!

 * [`http-proxy-agent`][http-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTP endpoints
 * [`https-proxy-agent`][https-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTPS endpoints
 * [`pac-proxy-agent`][pac-proxy-agent]: A PAC file proxy `http.Agent` implementation for HTTP and HTTPS
 * [`socks-proxy-agent`][socks-proxy-agent]: A SOCKS proxy `http.Agent` implementation for HTTP and HTTPS

Example
-------

Here's a minimal example that creates a new `net.Socket` or `tls.Socket`
based on the `secureEndpoint` property. This agent can be used with both
the `http` and `https` modules.

```ts
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { Agent } from 'agent-base';

class MyAgent extends Agent {
  connect(req, opts) {
    // `secureEndpoint` is true when using the "https" module
    if (opts.secureEndpoint) {
      return tls.connect(opts);
    } else {
      return net.connect(opts);
    }
  }
});

// Keep alive enabled means that `connect()` will only be
// invoked when a new connection needs to be created
const agent = new MyAgent({ keepAlive: true });

// Pass the `agent` option when creating the HTTP request
http.get('http://nodejs.org/api/', { agent }, (res) => {
  console.log('"response" event!', res.headers);
  res.pipe(process.stdout);
});
```

[http-proxy-agent]: ../http-proxy-agent
[https-proxy-agent]: ../https-proxy-agent
[pac-proxy-agent]: ../pac-proxy-agent
[socks-proxy-agent]: ../socks-proxy-agent
[http.Agent]: https://nodejs.org/api/http.html#http_class_http_agent

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