Skip to content
seamless

How to use proxies with Axios

Axios supports a proxy option, but its built-in handling does not tunnel HTTPS reliably. The dependable pattern in Node.js is to pass an https-proxy-agent instead and disable the native proxy option.

Setup

Set up Axios in 4 steps

  1. 01

    Install the agent

    Install Axios together with the proxy agent that handles HTTPS tunnelling correctly.

    npm i axios https-proxy-agent
  2. 02

    Route requests through the agent

    Set httpsAgent and turn off the native proxy option, otherwise Axios tries to handle the proxy itself and the two conflict. Replace PROXY_HOST, PROXY_PORT, USERNAME and PASSWORD with the endpoint and credentials shown in your seamless dashboard.

    const axios = require("axios");
    const { HttpsProxyAgent } = require("https-proxy-agent");
    
    const agent = new HttpsProxyAgent(
      "http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT"
    );
    
    const res = await axios.get("https://example.com", {
      httpsAgent: agent,
      proxy: false, // required: prevents Axios' own proxy handling from interfering
      timeout: 30000,
    });
  3. 03

    Create a preconfigured instance

    An Axios instance keeps the agent and headers in one place instead of repeating them at every call site.

    const client = axios.create({
      httpsAgent: agent,
      proxy: false,
      timeout: 30000,
      headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" },
    });
    
    const { data } = await client.get("https://example.com");
  4. 04

    Rotate per request

    Build a fresh agent with a new session identifier when you want each request to leave from a different IP.

    const { randomUUID } = require("crypto");
    
    function rotatingAgent() {
      const session = randomUUID().slice(0, 8);
      return new HttpsProxyAgent(
        `http://USERNAME-session-${session}:PASSWORD@PROXY_HOST:PROXY_PORT`
      );
    }

FAQ

Questions about Axios proxies

Anything else? Our team answers on live chat, by email and on Discord.

Why does the Axios proxy option not work for HTTPS?

Axios' built-in proxy handling does not establish a CONNECT tunnel correctly in all Node versions, which produces TLS and certificate errors. Passing an https-proxy-agent as httpsAgent and setting proxy: false is the reliable approach.

How do I authenticate a proxy in Axios?

Include the credentials in the URL you hand to HttpsProxyAgent, in the form http://username:password@host:port. The agent sets the Proxy-Authorization header for you.

Can I use one agent for all requests?

Yes, and you should when you want connection reuse. Create a separate agent per session only when you deliberately want each request to leave through a different exit IP.

Start small. Scale when it works.

Buy a little bandwidth or rent a single ISP proxy for a day, test it on your own targets, then grow from there.