Bun's ultra-fast runtime supports proxies through environment variables and the undici ProxyAgent. Route fetch() through KnoxProxy with zero config or full programmatic control.
The fastest way: set HTTP_PROXY and Bun respects it automatically.
# Terminal
export HTTP_PROXY=http://USER:PASS@gw.knoxproxy.com:7000
export HTTPS_PROXY=http://USER:PASS@gw.knoxproxy.com:7000
bun run script.tsWith env vars set, fetch() routes through KnoxProxy automatically.
const resp = await fetch("https://httpbin.org/ip");
console.log(await resp.json());Use undici ProxyAgent for fine-grained proxy control.
import { ProxyAgent } from "undici";
const dispatcher = new ProxyAgent({
uri: "http://gw.knoxproxy.com:7000",
token: `Basic ${btoa("USER:PASS")}`,
});
const resp = await fetch("https://httpbin.org/ip", { dispatcher });
console.log(await resp.json());Target a specific country in the proxy credentials.
const dispatcher = new ProxyAgent({
uri: "http://gw.knoxproxy.com:7000",
token: `Basic ${btoa("USER-country-br:PASS")}`,
});
const resp = await fetch("https://httpbin.org/ip", { dispatcher });
console.log(await resp.json());Maintain the same exit IP across requests.
const dispatcher = new ProxyAgent({
uri: "http://gw.knoxproxy.com:7000",
token: `Basic ${btoa("USER-session-myid:PASS")}`,
});
const r1 = await fetch("https://httpbin.org/ip", { dispatcher });
const r2 = await fetch("https://httpbin.org/ip", { dispatcher });
console.log(await r1.json(), await r2.json());Run multiple proxied requests with Bun's fast runtime.
const urls = Array.from({ length: 5 }, () => "https://httpbin.org/ip");
const results = await Promise.all(
urls.map((url) =>
fetch(url, { dispatcher }).then((r) => r.json())
)
);
console.log(results);// KnoxProxy + Bun
// Run: HTTP_PROXY=http://USER:PASS@gw.knoxproxy.com:7000 bun run knoxproxy_bun.ts
// Or use the ProxyAgent approach below:
import { ProxyAgent } from "undici";
const dispatcher = new ProxyAgent({
uri: "http://gw.knoxproxy.com:7000",
token: `Basic ${btoa("USER:PASS")}`,
});
const response = await fetch("https://httpbin.org/ip", { dispatcher });
const data = await response.json();
console.log("Exit IP:", data.origin);Each fetch() call gets a new exit IP. For sticky sessions, use USER-session-{id} in credentials. ProxyAgent keeps sessions within a single dispatcher instance.
| Problem | Fix |
|---|---|
| ConnectionRefused: Unable to connect to proxy | Verify gw.knoxproxy.com:7000 is reachable. Check firewall rules for outbound port 7000. |
| fetch() ignoring HTTP_PROXY | Set the variable in the same terminal session. Alternatively, use the ProxyAgent approach for explicit proxy control. |
USER-country-de-city-berlin-session-profile07Order matters -- geo flags before the session flag. The session name is free text; use the profile ID so the mapping is self-documenting. Password stays as issued; no flags belong there. HTTP on :7000, SOCKS5 on :7001, same credentials.
Not natively through env vars. Use undici's ProxyAgent with the HTTP gateway on port 7000, or use a SOCKS5 wrapper library.
Yes. Bun includes undici as a global. You can import { ProxyAgent } from "undici" without installing anything.
Bun's fetch is built on native code and is faster than Node.js. Proxy overhead is minimal since the bottleneck is the proxy gateway, not the runtime.
Yes. Use the ProxyAgent in your server handlers to make outbound requests through KnoxProxy while serving incoming traffic directly.
Free trial on rotating residential -- 2 minutes setup, no credit card.