A lightweight HTTP client that works seamlessly with node and bun:
- http2 support with per-origin session
- http1 keepalive-agent by default
- compression support with fallback zstd support via fzstd when installed
- optional decode support via iconv-lite when installed
- works with async/await, promises and callbacks
- tiny and comes with no required dependencies
- 200% test coverage (we run every test at least twice for good measure)
phn is an interface-compatible, drop-in replacement for the abandoned phin
npm i phn
const phn = require("phn");
const res = await phn({
url: 'https://example.org'
});method- string; default:GETurl- string or URL objectcore- object; passed on tohttp(s).requesthttp2- object; passed on tohttp2.request;falseto disable http2 supportheaders- object; request headersquery- object; added tourlas query stringdata- object, buffer, typed array; sent as data in POST requestform- object; sent asapplication/x-www-form-urlencodedparse-"json","string", orfunction(body); parse response bodyfollow- follow redirects iftrue, limit if Number (default: 20)stream- return stream asres.streaminstead ofres.bodycompression- bool or string, string overridesaccept-encodingheader, default:truedecode- bool or string; useiconv-liteto decode stream if availabletimeout- request timeout in millisecondsmaxBuffer- maximum response buffer size
consume http response as stream
const phn = require("phn");
const resp = await phn({
url: 'https://example.org/',
compression: true,
stream: true,
});
resp.stream.pipe(/* ... */)use a custom agent for http and https
const phn = require("phn");
const https = require("https");
const agent = new SocksProxyAgent(/* ... */);
await phn({
url: 'https://example.org/',
core: { agent },
http2: false
});builtin classic callback interface
const phn = require("phn");
phn('https://example.org/', (err, res) => {
if (!err) console.log(res.body);
});set options for any subsequent request
const phn = require("phn").defaults({
method: 'POST',
parse: 'json',
timeout: 2000
});
const res = await phn('https://example.org/');bun and node <=22 don't support zstd compression, but phn can handle zstd when fzstd is available
npm i fzstd
phn can decode various character encodings via the decode option when iconv-lite is installed
npm i iconv-lite
phn has evolved from a fork of phin and centra by Ethan Davis