NodeJS: How to Use & Rotate Proxies
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
To use proxies with Node-Fetch you need to install https-proxy-agent
and create a proxy agent using HttpProxyAgent
and pass that into our fetch request using the agent
parameter.
import fetch from 'node-fetch';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
const proxyAgent = new HttpProxyAgent.HttpsProxyAgent('http://proxy.example.com:8080');
try{
const response = await fetch('https://httpbin.org/ip', { agent: proxyAgent});
const body = await response.text();
console.log(body);
} catch (error){
console.log('error', error)
}
})();
To configure NodeJs Got to use proxy, you first need to import HttpsProxyAgent
class from hpagent library. HttpsProxyAgent
is used to create a proxy agent, which is used by got
send our requests through proxy server.
To use proxy with got
, simply set agent
field inside your request options
to an object with https
attribute. https
attribute should be set to a new HttpsProxyAgent
instance initialized with proxyUrl
as proxy
param.
import got from 'got';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
const requestUrl = 'https://httpbin.org/ip';
const proxyUrl = 'http://proxy.example.com:8080';
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: proxyUrl
})
}
};
(async () => {
try {
const response = await got.get(requestUrl, options);
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
Note that if you're making http
requests instead of https
, you have to set options.agent.http
attribute to a new HttpProxyAgent
instance.
To use proxies with a NodeJs Request-Promise add the proxy string to the request options
using the proxy
attribute.
import request from 'request-promise';
(async () => {
const options = {
method: 'GET',
url: 'https://httpbin.org/ip',
proxy:'http://proxy.example.com:8080'
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
There a couple of ways to use proxies with Axios, however, one of the best ways is to install https-proxy-agent
and create a proxy agent using HttpProxyAgent
and pass that into our axios request using the httpAgent
parameter. Then set proxy
to false.
import axios from 'axios';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
try{
const response = await axios.get('https://httpbin.org/ip', {
proxy: false,
httpAgent: new HttpProxyAgent.HttpsProxyAgent('https://proxy.example.com:8080')
});
console.log(response.data);
} catch (error){
console.log('error', error)
}
})();
Before you can use proxies with NodeJS SuperAgent, you need to add proxy support to it. To do so, first import addProxy
from superagent-proxy and then call addProxy
function with request
object from superagent.
Now in order to use proxies with a NodeJs SuperAgent, you simply have to call proxy
method with proxyUrl
string, right after you set request url with request.get
method.
const request = require('superagent');
const addProxy = require('superagent-proxy');
addProxy(request);
const requestUrl = 'https://httpbin.org/ip'
const proxyUrl = 'http://proxy.example.com:8080';
(async () => {
try{
const response = await request.get(requestUrl)
.proxy(proxyUrl);
console.log(response.body);
} catch (error){
console.log('error', error)
}
})();
In this guide for The Nodejs Web Scraping Playbook, we will look at how to integrate the 3 most common types of proxies into our Nodejs based web scraper.
Using proxies with the Node-Fetch, Got, Request-Promise, Axios, or SuperAgent Libraries; allows you to spread your requests over multiple IP addresses making it harder for websites to detect & block your web scrapers.
In this guide we will walk you through the 3 most common proxy integration methods and show you how to use them with Nodejs Libraries:
- Using Proxy IPs With NodeJs
- Proxy Authentication With NodeJs
- The 3 Most Common Proxy Formats
- Proxy Integration #1: Rotating Through Proxy IP List
- Proxy Integration #2: Using Proxy Gateway
- Proxy Integration #3: Using Proxy API Endpoint
Let's begin...
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
Using Proxy IPs With NodeJs
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
Using a proxy with Node-Fetch is a bit more tricky than other Nodejs HTTP clients. Node-Fetch doesn't natively support proxies so we first need to install https-proxy-agent
and use it to proxy our requests.
npm install https-proxy-agent
Once installed then we need to create a proxy agent using HttpProxyAgent
and pass that into our fetch request using the agent
parameter.
import fetch from 'node-fetch';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
const proxyAgent = new HttpProxyAgent.HttpsProxyAgent('http://proxy.example.com:8080');
try{
const response = await fetch('https://httpbin.org/ip', { agent: proxyAgent});
const body = await response.text();
console.log(body);
} catch (error){
console.log('error', error)
}
})();
Using a proxy with Got is quite straightforward. We simply need to create a new HttpProxyAgent
instance by passing proxy url as proxy
param and set it to options.agent.https
field.
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: 'http://proxy.example.com:8080';
})
}
};
(async () => {
try {
const response = await got.get('https://httpbin.org/ip', options);
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
This method will work for all request methods Got supports: get
, post
, put
, delete
, patch
, head
.
Using a proxy with Request-Promise is very straight forward. We simply need to add the proxy string to the request options
using the proxy
attribute.
import request from 'request-promise';
(async () => {
const options = {
method: 'GET',
url: 'https://httpbin.org/ip',
proxy:'http://proxy.example.com:8080'
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
This method will work for all request methods Request-Promise supports: GET
, POST
, PUT
, DELETE
, PATCH
, HEAD
.
Using a proxy with Axios is a bit more tricky than other Nodejs HTTP clients. Axios doesn't natively support proxies so we first need to install https-proxy-agent
and use it to proxy our requests.
npm install https-proxy-agent
Once installed then we need to create a proxy agent using HttpProxyAgent
and pass that into our fetch request using the agent
parameter.
import axios from 'axios';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
try{
const response = await axios.get('https://httpbin.org/ip', {
proxy: false,
httpAgent: new HttpProxyAgent.HttpsProxyAgent('https://proxy.example.com:8080')
});
console.log(response.data);
} catch (error){
console.log('error', error)
}
})();
Using a proxy with SuperAgent is very straightforward. We simply need to use proxy
method of request and provide proxy string as an argument.
const request = require('superagent');
const addProxy = require('superagent-proxy');
addProxy(request);
(async () => {
try{
const response = await request.get('https://httpbin.org/ip')
.proxy('http://proxy.example.com:8080');
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
This method will work for all request methods SuperAgent supports: get
, post
, put
, delete
, patch
, head
.
Proxy Authentication With Nodejs Libraries
The easiest way to authenticate a proxy with NodeJs libraries is to add the username
and password
to the proxy string:
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
import fetch from 'node-fetch';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
const proxyAgent = new HttpProxyAgent.HttpsProxyAgent('http://USERNAME:PASSWORD@proxy.example.com:8080');
try{
const response = await fetch('https://httpbin.org/ip', { agent: proxyAgent});
const body = await response.text();
console.log(body);
} catch (error){
console.log('error', error)
}
})();
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';
const proxyUrl = 'http://USERNAME:PASSWORD@proxy.example.com:8080';
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: proxyUrl
})
}
};
(async () => {
try {
const response = await got.get('https://httpbin.org/ip', options);
console.log(response.body);
} catch (error){
console.log('error', error)
}
})();
import request from 'request-promise';
(async () => {
const options = {
method: 'GET',
url: 'https://httpbin.org/ip',
proxy: 'http://USERNAME:PASSWORD@proxy.example.com:8080',
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');
(async () => {
// proxy with embedded credentials
const proxyUrl = 'http://USERNAME:PASSWORD@proxy.example.com:8080';
const httpsAgent = new HttpsProxyAgent(proxyUrl);
try {
const response = await axios.get('https://httpbin.org/ip', {
httpsAgent,
});
console.log(response.data);
} catch (error) {
console.error('error', error);
}
})();
const request = require('superagent');
const addProxy = require('superagent-proxy');
addProxy(request);
(async () => {
try{
const response = await request.get('https://httpbin.org/ip')
.proxy('http://USERNAME:PASSWORD@proxy.example.com:8080');
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
The 3 Most Common Proxy Formats
That covered the basics of integrating a proxy into Node-Fetch, Got, Request-Promise, Axios, and SuperAgent, in the next sections we will show you how to integrate them into the 3 most common proxy formats:
- Rotating Through List of Proxy IPs
- Using Proxy Gateways
- Using Proxy APIs
A couple years ago, proxy providers would sell you a list of proxy IP addresses and you would configure your scraper to rotate through these IP addresses and use a new one with each request.
However, today more and more proxy providers don't sell raw lists of proxy IP addresses anymore. Instead providing access to their proxy pools via proxy gateways or proxy API endpoints.
We will look at how to integrate with all 3 proxy formats.
If you are looking to find a good proxy provider then check out our web scraping proxy comparison tool where you can compare the plans of all the major proxy providers.
Proxy Integration #1: Rotating Through Proxy IP List
Here a proxy provider will normally provide you with a list of proxy IP addresses that you will need to configure your scraper to rotate through and select a new IP address for every request.
The proxy list you recieve will look something like this:
'http://Username:Password@85.237.57.198:20000',
'http://Username:Password@85.237.57.198:21000',
'http://Username:Password@85.237.57.198:22000',
'http://Username:Password@85.237.57.198:23000',
To integrate them into our scrapers we need to configure our code to pick a random proxy from this list everytime we make a request.
In our Nodejs scraper we could do it like this:
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
import fetch from 'node-fetch';
import HttpsProxyAgent from 'https-proxy-agent';
(async () => {
const proxyList = [
'http://Username:Password@85.237.57.198:20000',
'http://Username:Password@85.237.57.198:21000',
'http://Username:Password@85.237.57.198:22000',
'http://Username:Password@85.237.57.198:23000',
];
const randomProxy = proxyList[Math.floor(Math.random() * proxyList.length)];
const agent = new HttpsProxyAgent(randomProxy);
try {
const response = await fetch('https://httpbin.org/ip', { agent });
const data = await response.json();
console.log(data);
} catch (error) {
console.error('error', error);
}
})();
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';
(async () => {
const proxyList = [
'https://Username:Password@85.237.57.198:20000',
'https://Username:Password@85.237.57.198:21000',
'https://Username:Password@85.237.57.198:22000',
'https://Username:Password@85.237.57.198:23000',
];
const randomProxy = proxyList[Math.floor(Math.random() * proxyList.length)];
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: randomProxy
})
}
};
try {
const response = await got.get('https://httpbin.org/ip', options)
console.log(response.body);
} catch (error) {
console.log('error', error)
}
})();
import request from 'request-promise';
(async () => {
const proxyList = [
'http://Username:Password@85.237.57.198:20000',
'http://Username:Password@85.237.57.198:21000',
'http://Username:Password@85.237.57.198:22000',
'http://Username:Password@85.237.57.198:23000',
]
const randomProxy = proxyList[Math.floor(Math.random()*proxyList.length)];
const options = {
method: 'GET',
url: 'https://httpbin.org/ip',
proxy: randomProxy,
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
import axios from 'axios';
import HttpsProxyAgent from 'https-proxy-agent';
(async () => {
const proxyList = [
'http://Username:Password@85.237.57.198:20000',
'http://Username:Password@85.237.57.198:21000',
'http://Username:Password@85.237.57.198:22000',
'http://Username:Password@85.237.57.198:23000',
];
// pick a random proxy
const randomProxy = proxyList[Math.floor(Math.random() * proxyList.length)];
const httpsAgent = new HttpsProxyAgent(randomProxy);
try {
const response = await axios.get('https://httpbin.org/ip', {
httpsAgent,
});
console.log(response.data);
} catch (error) {
console.error('error', error);
}
})();
const request = require('superagent');
const addProxy = require('superagent-proxy');
addProxy(request);
(async () => {
const proxyList = [
'http://Username:Password@85.237.57.198:20000',
'http://Username:Password@85.237.57.198:21000',
'http://Username:Password@85.237.57.198:22000',
'http://Username:Password@85.237.57.198:23000',
];
const randomProxy = proxyList[Math.floor(Math.random()*proxyList.length)];
try{
const response = await request.get('https://httpbin.org/ip')
.proxy(randomProxy);
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
This is a simplistic example, as when scraping at scale we would also need to build a mechanism to monitor the performance of each individual IP address and remove it from the proxy rotation if it got banned or blocked.
Proxy Integration #2: Using Proxy Gateway
Increasingly, a lot of proxy providers aren't selling lists of proxy IP addresses anymore. Instead, they give you access to their proxy pools via a proxy gateway.
Here, you only have to integrate a single proxy into any of your Nodejs scraper and the proxy provider will manage the proxy rotation, selection, cleaning, etc. on their end for you.
This is the most common way to use residential and mobile proxies, and becoming increasingly common when using datacenter proxies too.
Here is an example of how to integrate a BrightData's residential proxy gateway into our NodeJs scraper:
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
import fetch from 'node-fetch';
import HttpProxyAgent from 'https-proxy-agent'
(async () => {
const proxyAgent = new HttpProxyAgent.HttpsProxyAgent('http://USERNAME:PASSWORD@zproxy.lum-superproxy.io:22225');
try{
const response = await fetch('https://httpbin.org/ip', { agent: proxyAgent});
const body = await response.text();
console.log(body);
} catch (error){
console.log('error', error)
}
})();
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';
(async () => {
const options = {
agent: {
https: new HttpsProxyAgent({
proxy: 'http://USERNAME:PASSWORD@zproxy.lum-superproxy.io:22225'
})
}
};
try {
const response = await got.get('https://httpbin.org/ip', options);
console.log(response.body);
} catch (error) {
console.log('error', error);
}
})();
import request from 'request-promise';
(async () => {
const options = {
method: 'GET',
url: 'https://httpbin.org/ip',
proxy: 'http://USERNAME:PASSWORD@zproxy.lum-superproxy.io:22225',
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');
(async () => {
// single gateway proxy URL
const proxyUrl = 'http://USERNAME:PASSWORD@zproxy.lum-superproxy.io:22225';
const httpsAgent = new HttpsProxyAgent(proxyUrl);
try {
const response = await axios.get('https://httpbin.org/ip', {
httpsAgent,
});
console.log(response.data);
} catch (error) {
console.error('error', error);
}
})();
const request = require('superagent');
const addProxy = require('superagent-proxy');
addProxy(request);
(async () => {
try{
const response = await request.get('https://httpbin.org/ip')
.proxy('http://USERNAME:PASSWORD@zproxy.lum-superproxy.io:22225');
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
As you can see, it is much easier to integrate than using a proxy list as you don't have to worry about implementing all the proxy rotation logic.
Proxy Integration #3: Using Proxy API Endpoint
Recently, a lot of proxy providers have started offering smart proxy APIs that take care of managing your proxy infrastructure for you by rotating proxies and headers for you so you can focus on extracting the data you need.
Here you typically, send the URL you want to scrape to their API endpoint and then they will return the HTML response.
Although every proxy API provider has a slightly different API integration, they are all very similar and are very easy to integrate with.
Here is an example of how to integrate with the ScrapeOps Proxy Manager:
- Node-Fetch
- Got
- Request Promise
- Axios
- SuperAgent
import fetch from 'node-fetch';
(async () => {
try{
const response = await fetch('https://proxy.scrapeops.io/v1/?' + new URLSearchParams({
api_key: 'API_KEY',
url: 'https://httpbin.org/ip',
}));
const body = await response.text();
console.log(body);
} catch (error){
console.log('error', error)
}
})();
import got from 'got';
(async () => {
const SCRAPEOPS_API_KEY = 'YOUR_API_KEY';
const url = 'https://httpbin.org/ip';
try{
const response = await got.get(`https://proxy.scrapeops.io/v1?api_key=${SCRAPEOPS_API_KEY}&url=${url}`)
console.log(response.body);
} catch (error){
console.log('error', error);
}
})();
import request from 'request-promise';
(async () => {
const options = {
method: 'GET',
url: `https://proxy.scrapeops.io/v1/`,
qs: {
'api_key': 'YOUR_API_KEY',
'url': 'https://httpbin.org/ip'
},
}
try{
const response = await request(options)
console.log(response);
} catch (error){
console.log('error', error)
}
})();
import axios from 'axios';
(async () => {
const SCRAPEOPS_API_KEY = 'YOUR_API_KEY';
const url = 'https://httpbin.org/ip';
try {
const response = await axios.get('https://proxy.scrapeops.io/v1', {
params: {
api_key: SCRAPEOPS_API_KEY,
url,
},
});
console.log(response.data);
} catch (error) {
console.log('error', error);
}
})();
const request = require('superagent');
(async () => {
const SCRAPEOPS_API_KEY = 'YOUR_API_KEY';
const url = 'https://httpbin.org/ip';
try{
const response = await request
.get(`https://proxy.scrapeops.io/v1?api_key=${SCRAPEOPS_API_KEY}&url=${url}`)
console.log(response.body);
} catch (error){
console.log('error', error)
}
})();
Here you simply send the URL you want to scrape to the ScrapeOps API endpoint in the URL
query parameter, along with your API key in the api_key
query parameter, and ScrapeOps will deal with finding the best proxy for that domain and return the HTML response to you.
You can get your own free API key with 1,000 free requests by signing up here.
More Web Scraping Tutorials
So that's how you can integrate proxies into your Node-Fetch, Got, Request-Promise, Axios & SuperAgent scrapers.
If you would like to learn more about Web Scraping, then be sure to check out The Web Scraping Playbook.
Or check out one of our more in-depth guides: