How to use proxies with NodeJS axios?

by scrapecrow May 16, 2023

Javascript's axios is a popular HTTP client often used when web scraping with nodejs.
To use proxies with axios we can use the proxy parameter in get and postj methods:

const axios = require('axios');
axios.get('https://httpbin.dev/ip', {
  proxy: {
    host: "160.11.12.13",
    port: "8080",
    auth: {
      username: "username",
      password: "password",
    }
  }
})
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error(error);
});

Note that axios does not support automatic proxy use through terminal environment variables HTTP_PROXY, HTTPS_PROXY or ALL_PROXY.

Axios also does not support SOCKS proxies directly but it can be enabled through socks-proxy-agent library:

const axios = require('axios');
const SocksProxyAgent = require('socks-proxy-agent');

const proxy = 'socks5://localhost:1080';

const httpAgent = new SocksProxyAgent(proxy);
const httpsAgent = new SocksProxyAgent(proxy);

axios.get('http://example.com', {
  httpAgent,
  httpsAgent,
})
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error(error);
});

Finally, when web scraping, it's best to rotate proxies for each request. For that see our article: How to Rotate Proxies in Web Scraping

Related Articles

Guide to Axios Headers

Learn about Javascript's Axios headers. How to configure, update, inspect headers in request and responses, how to set defaults and useful tips

HTTP
NODEJS
Guide to Axios Headers

Axios vs Fetch: Which HTTP Client to Choose in JS?

Explore the differences between Fetch and Axios - two essential HTTP clients in JavaScript - and discover which is best suited for your project.

HTTP
NODEJS
API
Axios vs Fetch: Which HTTP Client to Choose in JS?

Web Scraping With NodeJS and Javascript

In this article we'll take a look at scraping using Javascript through NodeJS. We'll cover common web scraping libraries, frequently encountered challenges and wrap everything up by scraping etsy.com

NODEJS
HTTP
DATA-PARSING
CSS-SELECTORS
INTRO
Web Scraping With NodeJS and Javascript

What is Rate Limiting? Everything You Need to Know

Discover what rate limiting is, why it matters, how it works, and how developers can implement it to build stable, scalable applications.

BLOCKING
CRAWLING
HTTP
What is Rate Limiting? Everything You Need to Know

How to Capture and Convert a Screenshot to PDF

Quick guide on how to effectively capture web screenshots as PDF documents

SCREENSHOTS
PYTHON
NODEJS
How to Capture and Convert a Screenshot to PDF

Playwright Examples for Web Scraping and Automation

Learn Playwright with Python and JavaScript examples for automating browsers like Chromium, WebKit, and Firefox.

PLAYWRIGHT
PYTHON
NODEJS
Playwright Examples for Web Scraping and Automation