services
Get a key

library / Integrations / Web frameworks / I0101

Node outbound HTTP proxy agent

Set two environment variables and Node 24 sends every @solana/kit RPC call through your HTTP proxy. The script starts a local CONNECT proxy, makes two reads through it, and prints the tunnels the proxy opened, so you see the route is used.

By Carbium · Web frameworks · I0101 · updated 2026-11-23

01

Where the proxy setting lives

A server in a locked-down network often reaches the internet only through a forward proxy. @solana/kit sends every RPC call with the global fetch, so the proxy is a setting of the Node process. The RPC transport takes a URL and headers, and nothing in that call has to change for the proxy.

Node 24 reads the proxy from the environment. Start the process with NODE_USE_ENV_PROXY=1 and HTTPS_PROXY set, and each fetch to an https:// address goes through a tunnel opened on that proxy. Your RPC code stays as it was: the RPC endpoint is still https://services.carbium.sh and the key still travels in the x-api-key header.

The switch is read once, when the process starts. Setting it from inside a running script is too late, which is why the demo below starts a second copy of itself with the variables in place.

The question often comes from @solana/web3.js code. Its Connection constructor takes a fetch option in its config, so pass fetch: globalThis.fetch there, with the key in httpHeaders, and its calls take the same route as kit's.

02

What the proxy sees

An HTTPS call through a forward proxy begins with a plain-text CONNECT services.carbium.sh:443 request. The proxy answers 200, and from then on it copies bytes in both directions. TLS runs between your process and Carbium inside that tunnel, so the proxy learns the host name and the traffic volume. It cannot read the key header, the method names or the replies.

Four variables control the route.

  • NODE_USE_ENV_PROXY=1 makes Node read the other three at startup. With it unset they are ignored.
  • HTTPS_PROXY names the proxy for https:// URLs, for example http://proxy.internal:3128. Credentials written into the URL, as http://user:[email protected]:3128, go to the proxy as basic authentication.
  • HTTP_PROXY does the same for plain http:// URLs, which matters if CARBIUM_RPC_URL points at an internal relay.
  • NO_PROXY lists hosts that skip the proxy, comma separated, such as localhost,127.0.0.1.
03

The script

Save it as main.ts and run it with Node 24, which strips the types on load. It needs CARBIUM_KEY and exits with a message when the key is missing. CARBIUM_RPC_URL is optional.

When HTTPS_PROXY is already in the environment, the script makes two reads and exits. When it is not, the script starts a small CONNECT proxy on a free loopback port, runs itself again with HTTPS_PROXY and HTTP_PROXY pointed at it and NO_PROXY cleared, and prints what the proxy saw once the child exits.

main.ts
// I0101 · Node outbound HTTP proxy agent// https://carbium.sh/library/integrate/node-outbound-http-proxy-agent// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { spawn } from "node:child_process";import { createServer } from "node:http";import { connect, type AddressInfo } from "node:net";import { createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";const key = process.env.CARBIUM_KEY ?? "";if (!key) {  console.error("Set CARBIUM_KEY.");  process.exit(1);}const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";// Two reads with plain kit code: the proxy is picked up from the environment, not from anything here.async function read() {  const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));  const [slot, version] = await Promise.allSettled([rpc.getSlot().send(), rpc.getVersion().send()]);  console.log(slot.status === "fulfilled" ? `getSlot (confirmed): ${slot.value}` : `getSlot failed: ${String(slot.reason)}`);  console.log(version.status === "fulfilled" ? `getVersion: ${version.value["solana-core"]}` : `getVersion failed: ${String(version.reason)}`);}// A proxy is already configured: use it and finish.if (process.env.HTTPS_PROXY || process.env.https_proxy) {  read().then(() => process.exit(0));} else {  // Demo: a loopback CONNECT proxy that records each tunnel, then this file again with the variables set.  const tunnels: string[] = [];  const proxy = createServer();  proxy.on("connect", (req, client, head) => {    const [host, port] = (req.url ?? "").split(":");    tunnels.push(`${host}:${port}`);    const out = connect(Number(port), host, () => {      client.write("HTTP/1.1 200 Connection Established\r\n\r\n");      out.write(head);      out.pipe(client);      client.pipe(out);    });    out.on("error", () => client.destroy());    client.on("error", () => out.destroy());  });  proxy.listen(0, "127.0.0.1", () => {    const { port } = proxy.address() as AddressInfo;    const p = `http://127.0.0.1:${port}`;    // Cover an http:// CARBIUM_RPC_URL too, and clear any NO_PROXY that would send the calls around the proxy.    const env = { ...process.env, NODE_USE_ENV_PROXY: "1", HTTPS_PROXY: p, HTTP_PROXY: p, http_proxy: p, NO_PROXY: "", no_proxy: "" };    spawn(process.execPath, [process.argv[1]], { stdio: "inherit", env, timeout: 25_000 }).on("exit", (code) => {      console.log(`proxy opened ${tunnels.length} tunnel(s): ${[...new Set(tunnels)].join(", ") || "none"}`);      process.exit(code ?? 1);    });  });}

run: node main.ts

04

Run it

With no proxy variables, node main.ts runs the demo. The output has three lines: getSlot (confirmed) with a slot number, getVersion with the node's version string, and a count of the tunnels the local proxy opened with the host and port they went to. Kit fills in confirmed for getSlot because the call leaves commitment out. Both methods are in the 1-cell class of cached reads.

The two calls start together, so they can open two tunnels and the count may read 2. A count of 0 means the calls went around the proxy, and the script says none for the host list.

Against your own proxy, set the variables yourself and the script goes straight to the reads: NODE_USE_ENV_PROXY=1 HTTPS_PROXY=http://proxy.internal:3128 node main.ts. The proxy's access log should show a CONNECT line for services.carbium.sh:443. In a service, put the variables in the unit file or the container environment so they exist before Node starts.

05

What the proxy has to allow

Many proxies permit CONNECT to port 443 only. The RPC and WebSocket endpoints share that port, so one rule covers them: https://services.carbium.sh for RPC and wss://services.carbium.sh for subscriptions. The gRPC stream listens on port 9443 of the same host, and its clients need a CONNECT rule for that port as well.

A proxy that inspects TLS re-signs the certificates it hands out. Node then rejects the chain until NODE_EXTRA_CA_CERTS points at the file holding the proxy's CA. Inspection also lets the proxy read headers, so the key is visible to it in that setup. Decide with the proxy's owners whether that is acceptable before the key goes through.

For a browser page that needs a subscription through your server, see Node WebSocket proxy. For the first read from a fresh machine, First Solana RPC API call shows the plain call.

06

Where it ends

  • The environment switch covers fetch. Node's WebSocket client is a separate path, and libraries that open their own sockets, a gRPC client among them, take their own proxy settings. Test each one from the network it will run in.
  • The demo proxy is a test double. It has no authentication, no allowlist and no logging, and it listens on loopback only. Never point production at it.
  • The second process exists for the demo only. A real service sets the variables before it starts and has no child.
  • A failed CONNECT shows up as a fetch failed error with no HTTP status from Carbium, because Carbium never saw the call. A 403 or 407 in the proxy's own log tells you which rule refused it.
  • Every call gains a hop. Carbium receives the same request on either route, so each method costs the same cells.
07

When it goes wrong

what goes wrongwhat to do
Every call ends with fetch failed and the cause is ECONNREFUSED or a timeout at the proxy address.The proxy host or port is wrong, or the machine cannot reach it. Test the same address from that host with curl -x http://proxy.internal:3128 https://services.carbium.sh before changing code.
Calls succeed, but the proxy's access log shows no CONNECT line.NODE_USE_ENV_PROXY=1 is missing or was set after the process started. Put it in the environment that launches Node, such as the systemd unit or the container spec.
A certificate error such as UNABLE_TO_VERIFY_LEAF_SIGNATURE or SELF_SIGNED_CERT_IN_CHAIN appears once the proxy is on.The proxy re-signs TLS. Export its CA certificate and start Node with NODE_EXTRA_CA_CERTS=/path/to/ca.pem.
The proxy answers 403 or 407 to the CONNECT request.The proxy refuses the host, the port or the caller. Ask for services.carbium.sh:443 on its allowlist, or add the credentials to the proxy URL and percent-encode any special characters in the password.
Calls reach Carbium through the proxy and come back 401.An unknown key gets 401 at the door. Check that CARBIUM_KEY holds the whole key and that no proxy rule strips the x-api-key header.
Calls to an internal service on the same host start failing after the proxy is enabled.They now go through the proxy too. List those hosts in NO_PROXY so they connect directly.
08

Questions

Can I give the RPC transport its own proxy agent instead of using the environment?

Kit's HTTP transport has a Node-only dispatcher option that takes an undici proxy agent, which suits a proxy for one client only. It means adding undici as a dependency, and the environment switch needs nothing extra.

Does the proxy see my API key?

With a plain CONNECT tunnel it sees the host and port and nothing inside the TLS stream, so the key header stays hidden. A proxy that inspects TLS can read headers, key included.

Does going through a proxy change what Carbium charges?

No. The proxy only adds a hop in front, and Carbium receives the same request it would get on a direct route, so each method costs the same cells.

Does this work on Node versions older than 24?

The NODE_USE_ENV_PROXY switch for fetch is available from Node 24, and the script here targets it. On older releases, use the dispatcher option with an undici proxy agent.