PiratProxies

Web scraping

Proxies in Axios: working examples

Axios is the most used HTTP client for Node.js. For https sites through a proxy with a login, the reliable way is a proxy agent instead of Axios's own proxy option.

The right proxy type for Axios

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

The values to enter in Axios

Every PiratProxies plan comes down to four values: host, port, username and password. Here they are for Residential; your own username and password are on your plan page.

Hostresi.piratproxies.com
HTTP port8080
SOCKS5 port1080
UsernameUSERNAME
Passwordshown on your plan page

Written as a single address:

http://USERNAME:PASSWORD@resi.piratproxies.com:8080

There is no separate country or session field: both are part of the username. Your plan page has a generator that writes it, and Targeting and sessions describes each piece.

Setting it up in Axios

  1. Install https-proxy-agent.
  2. Create the agent from the proxy address and pass it as httpsAgent.
  3. Set proxy: false, so Axios does not also apply its own proxy handling.

Code: Axios through a proxy

Node.js

const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');

const agent = new HttpsProxyAgent('http://USERNAME:PASSWORD@resi.piratproxies.com:8080');
axios.get('https://api.ipify.org', { httpsAgent: agent, proxy: false })
  .then((res) => console.log(res.data));

Put your own login from the plan page in place of USERNAME and PASSWORD. The example calls an IP-check service, so what it prints is the address your request came from.

Should Axios rotate IPs or keep one?

When Axios is fetching lots of unrelated pages, leave the session out of the username and each request gets a fresh IP address. When requests belong together, such as a login, a basket or paginated results that set cookies, put a session in the username so they all share one address.

Here is what a sticky username looks like:

USERNAME-country-us-time-600-session-a1b2c3d4

When Axios will not connect

  • Axios's built-in proxy option sends https requests to the proxy in a way many proxies reject. The agent opens a proper tunnel.
  • For SOCKS5, use socks-proxy-agent with socks5://USERNAME:PASSWORD@resi.piratproxies.com:1080 in the same place.
  • Error 407 means the login was refused. Copy the username and password from your plan page once more, and make sure no space crept in and the username was not cut short.
  • An IP in the wrong country nearly always comes from the username, since the country code lives there. Other fixes.

Axios and proxies: questions

Can Axios use a SOCKS5 proxy?

Yes. Point it at port 1080 with the same username and password.

Which PiratProxies plan suits Axios best?

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

Words used in this guide: web scraping, rotating proxy, sticky session, rate limiting, headless browser.

Axios belongs to its own developers, and PiratProxies has no connection with them; we name it only to say what this guide covers. Follow the rules of the sites you visit and our acceptable use policy.