Axios Proxy Setup
Axios is one of the most popular HTTP clients for Node.js and browser environments. In Node.js, Axios supports proxy configuration through both its built-in `proxy` option and external proxy agents. For HTTPS targets, using a proxy agent is the recommended approach with Hex Proxies.
Installation
npm install axios https-proxy-agentRecommended: Proxy Agent for HTTPS
import axios from 'axios';const agent = new HttpsProxyAgent('http://user:pass@gate.hexproxies.com:8080');
const res = await axios.get('https://httpbin.org/ip', { httpsAgent: agent, timeout: 30000, }); console.log(res.data); ```
IP Whitelist Authentication
Whitelist your server IP in the Hex Proxies dashboard and omit credentials:
const agent = new HttpsProxyAgent('http://gate.hexproxies.com:8080');Using Axios Built-in Proxy Config (HTTP only)
Axios has a built-in `proxy` option, but it only works reliably for HTTP (not HTTPS) targets:
const res = await axios.get('http://httpbin.org/ip', {
proxy: {
host: 'gate.hexproxies.com',
port: 8080,
auth: { username: 'user', password: 'pass' },
},
});For HTTPS targets, always use the `httpsAgent` approach shown above.
SOCKS5 Proxy Support
npm install socks-proxy-agentconst agent = new SocksProxyAgent('socks5://user:pass@gate.hexproxies.com:1080'); const res = await axios.get('https://httpbin.org/ip', { httpsAgent: agent }); ```
Global Proxy Configuration
Apply a proxy to all Axios requests using defaults:
const proxyAgent = new HttpsProxyAgent('http://user:pass@gate.hexproxies.com:8080');
axios.defaults.httpsAgent = proxyAgent;// All subsequent requests use the proxy const res = await axios.get('https://httpbin.org/ip'); ```
Best Practices
- **Prefer proxy agents** over the built-in `proxy` option for HTTPS requests — the agent approach handles TLS correctly.
- **Set timeouts** on every request to avoid hanging calls: `timeout: 30000`.
- **Reuse the agent** for sticky sessions or create a new agent per request for IP rotation.
- **Handle errors** with Axios interceptors for centralized retry logic.
Troubleshooting
- **ECONNREFUSED**: Verify proxy host and port. Check firewall rules for outbound port 8080.
- **403 responses**: The target site is blocking your request. Switch to residential proxies and add realistic headers.
- **Proxy auth failures**: Ensure credentials are correct. URL-encode special characters in the password.
- **HTTPS not working with built-in proxy**: Use `httpsAgent` with `https-proxy-agent` instead of the `proxy` config option.