v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

node-fetch Proxy Integration

Use Hex Proxies in node-fetch with a proxy agent for reliable requests.

node-fetch Proxy Setup

node-fetch is a lightweight HTTP client for Node.js that implements the browser Fetch API. Unlike browsers, Node.js does not natively route fetch requests through system proxies, so you need a proxy agent library to direct traffic through Hex Proxies. The most common choice is `https-proxy-agent`.

Installation

npm install node-fetch https-proxy-agent

Basic Proxy Configuration

import fetch from 'node-fetch';

const agent = new HttpsProxyAgent('http://user:pass@gate.hexproxies.com:8080'); const res = await fetch('https://httpbin.org/ip', { agent }); const data = await res.json(); console.log(data); ```

IP Whitelist Authentication

Whitelist your server IP in the Hex Proxies dashboard and omit credentials:

const agent = new HttpsProxyAgent('http://gate.hexproxies.com:8080');

SOCKS5 Proxy with node-fetch

Use `socks-proxy-agent` for SOCKS5 connections:

npm install socks-proxy-agent

const agent = new SocksProxyAgent('socks5://user:pass@gate.hexproxies.com:1080'); const res = await fetch('https://httpbin.org/ip', { agent }); ```

Geo-Targeted Requests

Append targeting parameters to your username:

const agent = new HttpsProxyAgent('http://user-country-de:pass@gate.hexproxies.com:8080');

Best Practices

  • **Reuse the agent instance** across multiple requests to avoid creating new TCP connections for each call.
  • Use rotation for scraping at scale — create a new agent per request if you want a fresh IP each time, or reuse for sticky sessions.
  • **Set timeouts** using AbortController to prevent hanging requests.
  • **Handle errors** with try/catch around fetch calls — proxy connection failures throw network errors.

Troubleshooting

  • **FetchError: request to ... failed**: Verify proxy host, port, and credentials. Ensure outbound port 8080 is accessible.
  • **407 Proxy Authentication Required**: Check username and password. URL-encode special characters.
  • **UNABLE_TO_VERIFY_LEAF_SIGNATURE**: Update Node.js or set `NODE_TLS_REJECT_UNAUTHORIZED=0` for testing only.
  • **Slow responses**: Residential proxies have 1-3 second latency. Use AbortController with a 30-second timeout.

Integration Steps

1

Install proxy agent

Use https-proxy-agent or similar.

2

Create agent

Pass the proxy URL with credentials.

3

Attach agent

Provide the agent to fetch options.

Operational Tips

Keep sessions stable for workflows that depend on consistent identity. For high-volume collection, rotate IPs and reduce concurrency if you see timeouts or 403 responses.

  • Prefer sticky sessions for multi-step flows (auth, checkout, forms).
  • Rotate per request for scale and broad coverage.
  • Use timeouts and retries to handle transient failures.

Frequently Asked Questions

Do I need a proxy agent?

Yes. node-fetch requires an agent to route through a proxy.

Ready to Integrate?

Start using residential proxies with node-fetch today.