axios proxy configuration for rotating residential, mobile, datacenter, and ISP IPs
To use an axios proxy with RotatingProxy, you configure an HTTP agent for the CONNECT tunnel on port 7070 or a SOCKS agent for port 7071. Authentication, geo-targeting, and session control are all passed through the proxy username.
- Use the `https-proxy-agent` package for HTTP and HTTPS requests via port 7070.
- For SOCKS5 support on port 7071, install and use `socks-proxy-agent`.
- Build the proxy username with options like `net.res_cc.us_city.new-york`.
- Handle 407 errors by verifying the username and password format.
axios proxy setup overview
When people search for an "axios proxy" solution, they typically need to route HTTP and HTTPS requests from their Node.js application through an intermediary. Axios itself does not have built-in proxy support for authenticated proxies, so you use an agent. For RotatingProxy, you will create an agent that points to the gateway gw.rotatingproxy.cc on port 7070 for HTTP CONNECT or port 7071 for SOCKS5, embedding your authentication and targeting options in the username. This method works for all four rotating networks: residential, mobile, datacenter, and ISP.
Where to put proxy settings in axios
You configure the proxy at the level of the HTTP/HTTPS agent, not directly in axios options like baseURL or headers. The agent handles the connection to the proxy gateway and the CONNECT tunnel for HTTPS traffic. You pass this agent to axios via the httpsAgent property (for HTTPS requests) or httpAgent property (for HTTP requests) in the request config. For SOCKS5, you use a SOCKS agent in the same way. This approach ensures all DNS resolution and TCP traffic flows through the proxy exit node, preventing DNS leaks.
HTTP and HTTPS proxy on port 7070
Use the https-proxy-agent package to create an agent for the HTTP proxy port 7070. First, install it:
npm install https-proxy-agent
Then, construct the proxy URL with authentication and create the agent. The URL format is http://USERNAME:PASSWORD@gw.rotatingproxy.cc:7070. Remember to replace USERNAME with your full proxy user login plus any options, and PASSWORD with the proxy user's password.
const { HttpsProxyAgent } = require('https-proxy-agent');
const axios = require('axios');
// Build your proxy username with options
const proxyUsername = 'zq4k8m2tva_net.res_cc.us_st.ca_city.los-angeles';
const proxyPassword = 'your_proxy_user_password';
const proxyUrl = `http://${proxyUsername}:${proxyPassword}@gw.rotatingproxy.cc:7070`;
const agent = new HttpsProxyAgent(proxyUrl);
async function makeRequest() {
try {
const response = await axios.get('https://api.example.com/data', {
httpsAgent: agent,
httpAgent: agent, // Use same agent for HTTP URLs
});
console.log(response.data);
} catch (error) {
console.error('Request failed:', error.message);
}
}
makeRequest();
This agent works for both HTTP and HTTPS targets because it uses the HTTP CONNECT method. For projects involving web scraping or price monitoring, this is the standard setup.
SOCKS5 proxy on port 7071
If your tool or library supports SOCKS5, you can use port 7071. Axios can use a SOCKS agent via the socks-proxy-agent package. Install it:
npm install socks-proxy-agent
The setup is similar, but you construct a SOCKS agent. Note that the SOCKS protocol does not embed credentials in the URL in the same way; you pass them as options.
const { SocksProxyAgent } = require('socks-proxy-agent');
const axios = require('axios');
const proxyOptions = {
hostname: 'gw.rotatingproxy.cc',
port: 7071,
userId: 'zq4k8m2tva_net.mob_cc.de',
password: 'your_proxy_user_password'
};
const agent = new SocksProxyAgent(proxyOptions);
async function makeRequest() {
try {
const response = await axios.get('https://api.ipify.org?format=json', {
httpsAgent: agent,
httpAgent: agent,
});
console.log('Exit IP:', response.data.ip);
} catch (error) {
console.error('Request failed:', error.message);
}
}
makeRequest();
If your use case involves social media management or Instagram proxies, SOCKS5 can be a reliable choice.
Passing targeting and session options in the username
All geo-targeting and session control is done through the username string. The basic format is your proxy user login, followed by underscores and key.value options. For example, to use a rotating residential IP in France with a sticky session for 30 minutes:
const proxyUsername = 'zq4k8m2tva_net.res_cc.fr_sid.abc123_ttl.30';
To rotate the IP on every new connection, simply omit the sid option. For city-level targeting, use the city token from our published list (e.g., city.paris). For US state targeting, you must also set cc.us (e.g., zq4k8m2tva_net.res_cc.us_st.ny_city.new-york). This granular targeting is useful for localization testing and checking geo-restricted content. Full syntax details are on the username options page.
Verifying the exit IP and location
After setting up your axios proxy, verify that traffic is exiting from the expected location. Make a request to an IP checking service using the same agent configuration.
async function checkIp() {
try {
const response = await axios.get('https://ipinfo.io/json', { httpsAgent: agent });
console.log('IP:', response.data.ip);
console.log('Country:', response.data.country);
console.log('City:', response.data.city);
} catch (error) {
console.error('Could not verify IP:', error.message);
}
}
You can also use https://api.ipify.org?format=json for a simple IP response. If the IP or location is incorrect, double-check your username options, especially the country (cc) and network (net) codes. For more tools, see checking the exit IP and location.
Common axios proxy errors and fixes
You may encounter a few specific errors when configuring an axios proxy.
Error 407: Proxy Authentication Required
Ensure the username string is correctly URL-encoded if it contains special characters (though underscores and periods are safe). Verify the proxy user's password and that the proxy user is active and allowed to use the chosen network. For a dedicated troubleshooting page, see proxy error 407.
Timeouts or slow responses
Timeouts can occur if the target server is slow or if there's network congestion. Increase axios's timeout in the request config.
const response = await axios.get(url, {
httpsAgent: agent,
timeout: 30000, // 30 seconds
});
Also, consider that residential and mobile networks inherently have higher latency than datacenter proxies. If speed is critical for website performance testing, you might choose the rotating datacenter or ISP networks. General tips are at proxy timeouts and slow responses.
TLS/SSL errors with HTTPS targets
Errors like EPROTO or CERT_HAS_EXPIRED can sometimes arise from the proxy's CONNECT tunnel. The https-proxy-agent should handle TLS correctly. Ensure you are using the latest version of the agent package. You can also try setting rejectUnauthorized: false on the agent for testing, but this is not recommended for production.
const agent = new HttpsProxyAgent({
...new URL(proxyUrl),
rejectUnauthorized: false // Use with caution for debugging
});
Tips for running axios at scale with proxies
When making many concurrent requests, manage resources and avoid bans.
- Reuse the agent: Create one agent instance and reuse it across multiple requests. This allows connection pooling and reduces overhead.
- Control concurrency: RotatingProxy allows up to 1,000 concurrent connections per account. Use a library like
p-limitorbottleneckto limit concurrent axios requests and avoid hitting TCP limits. - Use different sessions: For tasks that require multiple simultaneous unique IPs, generate different
sidvalues for different groups of requests. This is essential for rank tracking across locations. - Monitor traffic: Track your usage in the dashboard to estimate costs. Use compression (
Accept-Encoding: gzip) and caching to reduce bandwidth. Learn more at how to reduce proxy traffic costs. - Add retries: Write a retry mechanism with exponential backoff for temporary proxy or network failures. A guide is available at retry strategies for proxy requests.
Alternatives and related integrations
If axios does not fit your project, other Node.js HTTP clients work similarly. For the native fetch API, see Node.js fetch proxy. For the got library, check got proxy. If you are using headless browsers, Playwright Node.js proxy and Puppeteer proxy guides cover that. For Python-based scraping, Python requests proxy and Scrapy proxy are popular choices.
Next steps and resources
Test your setup with a simple IP check. Then, integrate the proxy into your application for tasks like ecommerce data collection or news monitoring. Use the proxy URL builder to generate connection strings. Understand how pay as you go proxy billing works with automatic volume discounts. For programmatic control, explore the REST API to check balance and manage proxy users.
Questions
Can I use axios with a proxy without an external agent package?
No, axios's built-in `proxy` config option does not support authenticated proxies correctly for HTTPS targets. You must use an agent like `https-proxy-agent` or `socks-proxy-agent` to handle the CONNECT tunnel and authentication.
How do I get a fresh IP address for every axios request?
Omit the `sid` (session ID) option from your proxy username. Every new TCP connection to the gateway will then receive a new IP from the selected network and location.
My axios request works without a proxy but fails with the proxy. What should I check first?
First, verify your basic connectivity to `gw.rotatingproxy.cc:7070` using a tool like `curl`. Then, check that your proxy user's login and password are correct, and that the user is not paused or restricted from the network you're trying to use (e.g., `net.mob`).
Does the `https-proxy-agent` work for HTTP (non-HTTPS) target URLs?
Yes, the same `HttpsProxyAgent` instance can be used for both HTTP and HTTPS target URLs when set as both the `httpAgent` and `httpsAgent` in the axios config. It will use the HTTP proxy protocol correctly for both.
How can I target a specific US city like Chicago for my axios requests?
Append `city.chicago` to your username, and also include `cc.us`. For example: `USERNAME_net.res_cc.us_city.chicago`. Ensure the city token is spelled correctly and uses hyphens for spaces (e.g., `city.new-york`).
I'm getting a "socket hang up" error with axios and the proxy. What does this mean?
This often indicates the proxy gateway or the target server closed the connection unexpectedly. It can be due to a timeout, a network issue, or the proxy user's balance reaching zero. Check your account balance and try increasing the request timeout.
Start with one top-up, use any network
Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.