a lightweight http client for node and bun, replacement for phin
npm install phnA 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
`` js
const phn = require("phn");
const res = await phn({
url: 'https://example.org'
});
`
* method - string; default: GETurl
* - string or URL objectcore
* - object; passed on to http(s).requesthttp2
* - object; passed on to http2.request; false to disable http2 supportheaders
* - object; request headersquery
* - object; added to url as query stringdata
* - object, buffer, typed array; sent as data in POST requestform
* - object; sent as application/x-www-form-urlencodedparse
* - "json", "string", or function(body); parse response bodyfollow
* - follow redirects if true, limit if Number (default: 20)stream
* - return stream as res.stream instead of res.bodycompression
* - bool or string, string overrides accept-encoding header, default: truedecode
* - bool or string; use iconv-lite to decode stream if availabletimeout
* - request timeout in millisecondsmaxBuffer
* - maximum response buffer size
consume http response as stream
` js
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
` js
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
` js
const phn = require("phn");
phn('https://example.org/', (err, res) => {
if (!err) console.log(res.body);
});
`
set options for any subsequent request
` js
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