This Axios proxy guide covers the following topics:
Axios is one the most widely-used HTTP clients in the JavaScript ecosystem. It offers a Promise-based, easy-to-use, intuitive API for performing HTTP requests and dealing with custom headers, configurations, and cookies.
By routing your Axios requests through a proxy, you can mask your IP address, making it more challenging for the target server to identify and block you.
Let's set up an HTTP, HTTPS, or SOCKS proxy in Axios. Install the axios
npm package:
npm install axios
In Node.js, Axios natively supports HTTP and HTTPS proxies via the proxy
config. So, if you want to use HTTP/HTTPS proxies with Axios in a Node.js application, there is nothing else todo here.
If you instead want to use a non-HTTP/S proxy, you need to rely on the Proxy Agents project. This provides http.Agent
implementations to integrate Axios with proxies in different protocols:
- HTTP and HTTPS proxies:
https-proxy-agent
- SOCKS, SOCKS5, and SOCKS4:
socks-proxy-agent
- PAC-*:
pac-proxy-agent
The URL of your HTTP/HTTPS proxy should look like this:
"<PROXY_PROTOCOL>://<PROXY_HOST>:<PROXY_PORT>"
<PROXY_PROTOCOL>
will be “http” for HTTP proxies and “https” for HTTPS proxies.<PROXY_HOST>
is generally a raw IP.<PROXY_PORT>
is the port the proxy server listens to.
For example, suppose this is the URL of your HTTP proxy:
"http://47.88.62.42:80"
You can set this proxy in Axios as follows:
axios.get(targetURL, {
proxy: {
protocol: "http",
host: "48.88.62.42",
port: "80"
}
})
To verify that the above Axios proxy approach works, retrieve the URL of a free HTTP or HTTPS proxy server. Try this example:
Protocol: HTTP; IP Address: 52.117.157.155; Port: 8002
The complete proxy URL will be http://52.117.157.155:8002
.
To verify that the proxy works as expected, target the /ip endpoint from the HTTPBin project. This public API returns the IP of the incoming request, so it should return the IP of the proxy server.
The snippet of the Node.js script will be:
import axios from "axios"
async function testProxy() {
// perform the desired request through the HTTP proxy
const response = axios.get("https://httpbin.io/ip", {
proxy: {
protocol: "http",
host: "52.117.157.155",
port: "8002"
}
})
// print the result
console.log(response.data)
}
testProxy()
Execute the script, and it should log:
{ "origin": "52.117.157.155" }
Warning:
You will not get the same result if you run the script, because free proxy services are unreliable, slow, error-prone, data-greedy, and short-lived.
If you try to set the “socks” string in the protocol field of the proxy config object, you will get the following error:
AssertionError [ERR_ASSERTION]: protocol mismatch
// ...
{
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: 'dada:',
expected: 'http:',
operator: '=='
}
That is because Axios does not natively support SOCKS proxies. Add the socks-proxy-agent
npm library to your project’s dependencies:
npm install socks-proxy-agent
This package allows you to connect to a SOCKS proxy server while making HTTP or HTTPS requests in Axios.
Then, import the SOCKS proxy agent implementation from the library:
const SocksProxyAgent = require("socks-proxy-agent")
Or if you are an ESM user:
import { SocksProxyAgent } from "socks-proxy-agent"
Suppose this is the URL of your SOCKS proxy:
"socks://183.88.74.73:4153"
Note:
The proxy protocol can be either “socks”, “socks5”, or “socks4”.
Store it in a variable and pass it to the SocksProxyAgent
constructor:
const proxyURL = "socks://183.88.74.73:4153"
const proxyAgent = new SocksProxyAgent(proxyURL)
SocksProxyAgent()
initializes an http.Agent
instance to perform HTTP/HTTPS requests through the proxy URL.
You can now use a SOCKS proxy with Axios as follows:
axios.get(targetURL, {
httpAgent: proxyAgent,
httpsAgent: proxyAgent
})
httpAgent
and httpsAgent
define the custom agent to use when performing HTTP and HTTPS requests, respectively. In other words, the HTTP or HTTPS request made by Axios will go through the specified SOCKS proxy. In a similar way, you can use the https-proxy-agent
npm package as an alternative way to set HTTP/HTTPS proxies in Axios.
Put it all together:
import axios from "axios"
import { SocksProxyAgent } from "socks-proxy-agent"
async function testProxy() {
// replace with the URL of your SOCKS proxy
const proxyURL = "socks://183.88.74.73:4153"
// define the HTTP/HTTPS proxy agent
const proxyAgent = new SocksProxyAgent(proxyURL)
// perform the request via the SOCKS proxy
const response = await axios.get("https://httpbin.io/ip", {
httpAgent: proxyAgent,
httpsAgent: proxyAgent
})
// print the result
console.log(response.data) // { "origin": "183.88.74.73" }
}
testProxy()
Follow the link for other examples of how to configure a SOCKS proxy in Axios.
You can set a proxy globally by specifying it directly in an Axios instance:
const axiosInstance = axios.create({
proxy: {
protocol: "<PROXY_PROTOCOL>",
host: "<PROXY_HOST>",
port: "<PROXY_PORT>"
},
// other configs...
})
Or if you are a Proxy Agents user:
// proxy Agent definition ...
const axiosInstance = axios.create({
httpAgent: proxyAgent,
httpsAgent: proxyAgent
})
All requests made with axiosInstance
will now automatically go through the specified proxy.
To allow only paying users access to premium proxies, proxy providers protect them with authentication. Trying to connect to an authenticated proxy without a username and password will result in a 407 Proxy Authentication Required error.
In particular, here is the syntax of the URL of an authenticated proxy:
[<PROTOCOL>://]<USERNAME>:<PASSWORD>@<HOST>[:<PORT>]
For example, a real-world URL to connect to an authenticated proxy might be:
http://admin:[email protected]:8391
In this case, the proxy URL field would be:
<PROTOCOL>:HTTP
<HOST>:156.127.0.192
<PORT>:8391
<USERNAME>:admin
<PASSWORD>:lK4w90MEe45YIkOpk
To deal with proxy authentication in Axios, specify the username and password in the authfield
of proxy
:
axios.get(targetURL, {
proxy: {
protocol: "http",
host: "156.127.0.192",
port: "8381",
auth: {
username: "admin",
password: "lK4w90MEe45YIkOpk"
}
}
})
If you are instead a Proxy Agents user, you have two ways to deal with authentication:
- Add the credentials directly in the proxy URL:
var proxyAgent = new SocksProxyAgent("http://admin:[email protected]:8391")
- Set the
username
andpassword
options in a URL object:
const proxyOpts = new URL("http://156.127.0.192:8391")
proxyOpts.username = "admin"
proxyOpts.password = "lK4w90MEe45YIkOpk"
const proxyAgent = new SocksProxyAgent(proxyOpts)
The same approaches also work with HttpsProxyAgent.
Another way to configure a proxy globally in Axios is by setting the following environment variables:
HTTP_PROXY
: The URL of the proxy server to use for HTTP requests.HTTPS_PROXY
: The URL of the proxy server to use for HTTPS requests.
On Linux or macOS, you can set them like this:
export HTTP_PROXY = "[<PROTOCOL>://]<USERNAME>:<PASSWORD>@<HOST>[:<PORT>]"
export HTTPS_PROXY = "[<PROTOCOL>://]<USERNAME>:<PASSWORD>@<HOST>[:<PORT>]"
When Axios detects these environment variables, it reads from them the proxy settings, including the credentials for authentication. Set the proxy
field to false
to make Axios ignore those environment variables. Keep in mind that you can also define a NO_PROXY
env as a comma-separated list of domains that should not be proxied.
Note that the same mechanism also works when using proxies in cURL.
To prevent the target site from blocking your proxy's IP address, ensure that each request you perform originates from a different proxy server:
- Define a list of objects, each containing the information to connect to a different proxy.
- Randomly select a proxy object before each request.
- Configure the selected proxy in Axios.
The approach outlined above assumes that you have access to a pool of reliable proxy servers, such as the rotating proxies that Bright Data offers.
Bright Data controls the best proxy servers in the world, serving Fortune 500 companies and over 20,000 customers. Its worldwide proxy network involves:
- Datacenter proxies – Over 770,000 datacenter IPs.
- Residential proxies – Over 72M residential IPs in more than 195 countries.
- ISP proxies – Over 700,000 ISP IPs.
- Mobile proxies – Over 7M mobile IPs.
Create a free Bright Data account today to try our proxy servers.