How to Use Proxies with node-fetch

The no-brainer choice for making HTTP requests in Node.js is node-fetch. There is just one thing that it's missing – the support for proxies. If you're going to be using proxies for your request, there's just one more package you need to add. And in the section below, you will discover exactly how to set it up with nice copy and paste code.

What Is node-fetch?

Node-fetch is a light-weight module that brings the browser's fetch() to Node.js. Use it to make API calls, fetch pages or send data to a server. It's tiny, simple and very popular in Node.js applications.

Why Use a Proxy with node-fetch?

  • Hide your identity IP: the website may see only the IP of the proxy - not yours.
  • Avoid blocks: some sites block an IP after a certain number of requests.
  • Test from other locations: test how a site performs from another country.
  • Company networks: access to the internet is restricted, in certain offices, to proxy.

Step 1: Install the Packages

Open your project folder in the terminal and run:

npm install node-fetch https-proxy-agent

Node-fetch v3 comes with ES modules. Just put "type": "module" to your package.json. If you forget this, the following import line will raise an error.

Step 2: Forward a Request to the Proxy

Create a file index.js and add this, replacing proxy address with your own.

import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';

const agent = new HttpsProxyAgent('http://123.45.67.89:8080');

// fetch with http agent
const res = await fetch('https://api.ipify.org?format=json', { agent });
const data = await res.json();
console.log(data);

The agent option instructs node-fetch to route the request via your proxy. For additional options, see the https-proxy-agent npm page.

Step 3: Check That It Works

Run node index.js. The IP should be the proxy IP and not yours. Run the same code once without { agent } to see the difference.

Connecting with a Username and Password through a Proxy

A lot of paid proxies have to be logged in. You can put it straight into the proxy url:

const agent = new HttpsProxyAgent('http://username:password@123.45.67.89:8080');

For passwords with special characters, such as @ or #, wrap in encodeURIComponent(). For other tips, see our guide to proxy authentication with node-fetch.

Using Multiple Proxies in Rotation

If an IP gets blocked, you can select another proxy for each request:

const proxies = ['http://1.1.1.1:8080', 'http://2.2.2.2:8080'];
const pick = proxies[Math.floor(Math.random() * proxies.length)];
const agent = new HttpsProxyAgent(pick);

Most Common Mistakes & Solutions

  • ERR_REQUIRE_ESM: add "type": "module" to package.json - OR - add node-fetch version 2 to your package.json.
  • ECONNREFUSED: the proxy is down, or the port is incorrect. Use a different proxy.
  • 407 Proxy Authentication Required: your username or password is incorrect or absent.
  • Time-outs: free proxies tend to be slower. A paid proxy will be more consistent.

Conclusion

Require a proxy with node-fetch? Just one additional package and one more option in your request. Test with one proxy and verify your IP, add login information or rotation as you require. Not familiar with proxies? Go back to basics and start with how proxy servers work.

For further inquiries Contact Us

FAQs

Q: Does node-fetch support proxies by default?
A: No, this is a feature of the proxy package.

Q: Can I use an HTTP proxy for HTTPS websites?
A: Sure, you can use https-proxy-agent with any site (whether it's http or https).

Q: How/where do I add the proxy username/password?
A: Include them within the proxy URL, for example http://user:pass@host:port.

Q: Are free proxies safe to use?
A: Use them only for testing, as the owner of the proxy may see your traffic.

Q: How do I test to see if the proxy is working?
A: Test your IP with api.ipify.org and it will display the proxy IP.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top