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 environment variables. Bun's fetch does not accept a SOCKS5 proxy via HTTP_PROXY, so use undici's ProxyAgent pointed at the KnoxProxy HTTP gateway on port 7000 instead, or wrap Bun with a separate SOCKS5 tunneling library when your workflow truly needs the SOCKS5 protocol.
Yes. Bun includes undici as a global module, so you never need to run bun add undici or npm install anything extra. Just write import { ProxyAgent } from "undici" directly in your script, then pass a KnoxProxy-configured ProxyAgent as the dispatcher option on your fetch() calls.
Bun's fetch is built on native code and runs faster than Node.js for raw request handling. Proxy overhead stays minimal either way, since the real bottleneck is the KnoxProxy gateway itself and the target server's response time, not the JavaScript runtime issuing the request.
Yes. Use the ProxyAgent in your server request handlers to make outbound calls through KnoxProxy, while Bun still serves incoming traffic directly without any proxy involved. This keeps your server's inbound and outbound traffic paths fully separate and easy to reason about.
Rotating residential proxies -- 2 minutes setup, instant activation, 14-day money-back guarantee.