Ultimate Guide: Using Proxies with Axios for Enhanced Web Scraping

Ultimate Guide: Using Proxies with Axios for Enhanced Web Scraping
Photo by Daniele Levis Pelusi / Unsplash


In the ever-evolving world of web development, privacy, efficiency, and access to unrestricted data are crucial for success. ProxyTee offers premium proxy solutions that help developers maintain privacy and overcome geo-restrictions with ease. For developers involved in web scraping and data collection, integrating proxies with popular HTTP clients like Axios is a game-changer. This guide will walk you through setting up proxies with Axios and show how ProxyTee’s features can supercharge your projects.


What is Axios, and Why Use Proxies?

Axios is a highly popular JavaScript HTTP client, known for its simplicity and powerful features. Its Promise-based API makes handling HTTP requests straightforward and allows for custom configurations like handling headers, cookies, and more. But why combine Axios with proxies?

When web scraping or collecting data, using proxies helps:

  • Mask your IP address to avoid detection
  • Bypass restrictions and geo-blocks
  • Prevent IP bans from target servers
  • Enhance privacy and anonymity

Without a proxy, target websites can easily detect multiple requests coming from a single IP, leading to potential blocks. ProxyTee’s rotating residential proxies offer an affordable and reliable solution to keep your scraping activities secure and uninterrupted.


How to Use a Proxy with Axios

Let's explore how to set HTTP, HTTPS, and SOCKS proxies in Axios, with added insights on using ProxyTee services.

Setting Up Prerequisites

Begin by ensuring that the `axios` package is installed via npm:

npm install axios

Axios supports HTTP and HTTPS proxies out-of-the-box. However, for other proxy types such as SOCKS, additional npm packages from the ‘Proxy Agents’ project are required.

  • HTTP/HTTPS proxies: Use https-proxy-agent.
  • SOCKS, SOCKS4, and SOCKS5 proxies: Use socks-proxy-agent.

Using HTTP/HTTPS Proxies With Axios

The format for an HTTP or HTTPS proxy URL looks like:

"<PROXY_PROTOCOL>://<PROXY_HOST>:<PROXY_PORT>"
  • <PROXY_PROTOCOL>: Either "http" or "https"
  • <PROXY_HOST>: The proxy’s IP address
  • <PROXY_PORT>: The port number

Here’s how to configure Axios with an HTTP/HTTPS proxy:

axios.get(targetURL, {
  proxy: {
    protocol: "http",
    host: "48.88.62.42",
    port: "80"
  }
});

To confirm the setup is working correctly, a test can be run by retrieving your proxy IP through a service like `httpbin.io/ip`.

Configuring SOCKS Proxies

Since Axios does not natively support SOCKS proxies, you must install socks-proxy-agent using npm:

npm install socks-proxy-agent

Then, import SocksProxyAgent and create a proxy agent:

const SocksProxyAgent = require("socks-proxy-agent");
// Or if you are using ES modules
// import { SocksProxyAgent } from "socks-proxy-agent";

const proxyURL = "socks://183.88.74.73:4153";
const proxyAgent = new SocksProxyAgent(proxyURL);

axios.get(targetURL, {
  httpAgent: proxyAgent,
  httpsAgent: proxyAgent
});

ProxyTee: A Superior Solution

While the setup above is a good foundation, you will likely want a professional, reliable provider. ProxyTee offers an affordable, reliable and easy-to-use solution for rotating residential proxies. ProxyTee delivers superior features such as:

  • Unlimited Bandwidth: Never worry about usage caps with ProxyTee's unlimited bandwidth, crucial for intensive scraping tasks.
  • Extensive Global IP Pool: Access over 20 million IP addresses across 100+ countries for comprehensive geo-targeting with our Global IP Coverage.
  • Multiple Protocol Support: Ensure full compatibility with any project with both HTTP and SOCKS5 support through multiple proxy protocols.
  • User-Friendly Interface: Simple and clean GUI makes setup and configuration straightforward.
  • Auto Rotation: Prevent blocks with automatic IP rotation, which can be customized from 3 to 60 minutes, depending on your needs.
  • API Integration: Integrate our Simple API into any platform easily for automated workflows.
  • Affordable Pricing: ProxyTee is cheaper than many competitors.

ProxyTee is not just a service; it's your partner in advanced data operations, with a primary focus on the most requested product, which is Unlimited Residential Proxies, offering features that include limitless bandwidth, rotating residential IPs, precision geo-targeting and budget-friendly costs – significantly cheaper than competitors. Unlike many providers that offer random proxies or continent-based selections, ProxyTee lets you pick exact locations.


Advanced Axios Proxy Techniques

Enhance your usage of proxies using these techniques:

1️⃣ Setting Proxies Globally

To set up global proxies in Axios:

const axiosInstance = axios.create({
    proxy: {
    protocol: "<PROXY_PROTOCOL>",
    host: "<PROXY_HOST>",
    port: "<PROXY_PORT>"
  }
});

Or, if using Proxy Agents:

const axiosInstance = axios.create({
  httpAgent: proxyAgent,
  httpsAgent: proxyAgent
});

All calls via axiosInstance will now utilize these proxy settings.

2️⃣ Handling Authentication for Proxies

Secure your premium proxies that often require authentication with:

axios.get(targetURL, {
  proxy: {
    protocol: "http",
    host: "156.127.0.192",
    port: "8381",
    auth: {
      username: "admin",
      password: "lK4w90MEe45YIkOpk"
     }
   }
});

For Proxy Agents, add your credentials to the proxy URL or use a `URL` object with username and password properties:

var proxyAgent = new SocksProxyAgent("http://admin:[email protected]:8391");

const proxyOpts = new URL("http://156.127.0.192:8391");
proxyOpts.username = "admin";
proxyOpts.password = "lK4w90MEe45YIkOpk";
const proxyAgent = new SocksProxyAgent(proxyOpts);

3️⃣ Setting Proxies Using Environment Variables

Globally set proxies by environment variables such as `HTTP_PROXY` and `HTTPS_PROXY`, allowing Axios to auto-detect settings. Remember, `NO_PROXY` variable can be used to set exclusion for domains to avoid using proxies.

4️⃣ Implementing Rotating Proxies

Avoid IP blocks by switching between proxy servers with every request:

  • Create a list of proxies with detailed settings.
  • Randomly choose a proxy before each request.
  • Configure your Axios call with the chosen proxy.

ProxyTee's rotating proxies solution, however, provides seamless automatic IP rotation with a high success rate.


Conclusion

This post has covered the key concepts for setting up proxies with Axios. ProxyTee provides the best infrastructure to handle any task, with specialized features that improve efficiency and scalability.

Choose ProxyTee, and leverage features such as unlimited bandwidth, extensive global IP coverage, and more for your projects. Explore ProxyTee’s affordable pricing and see how to level up your operations.