Residential proxies with Node.js axios
axios has a built-in proxy option, but it is unreliable for HTTPS targets. The dependable approach is to pass an https-proxy-agent as httpsAgent and turn axios' own proxy handling off.
Your username and password are on the Proxy generator page after you sign in. Host gw.nodelighthouse.com: port 9000 rotating, 10000 sticky, 11000 SOCKS5.
Install the agent and get your credentials
Your username and password are on the Proxy generator page of the dashboard. Use the password exactly as shown there; country, state, city and session options all go in the username.
The gateway is gw.nodelighthouse.com. Port 9000 rotates (a new IP per request), port 10000 is sticky (the username carries -lifetime-N-session-xxxxxxxx), and port 11000 is SOCKS5.
npm i axios https-proxy-agentSend a request through a rotating IP
Create the agent once and pass it as httpsAgent. Set proxy: false so axios doesn't also apply an HTTP_PROXY environment variable.
const axios = require("axios");
const { HttpsProxyAgent } = require("https-proxy-agent");
const agent = new HttpsProxyAgent("http://<USERNAME>-type-residential-country-us:<PASSWORD>@gw.nodelighthouse.com:9000");
(async () => {
for (let i = 0; i < 3; i++) {
const res = await axios.get("https://api.ipify.org?format=json", {
httpsAgent: agent,
proxy: false,
timeout: 30000,
});
console.log(res.data.ip);
}
})();Keep one IP with a sticky session
Use port 10000 and a session id of 8 characters from a-z and 0-9. Create one agent per session and reuse it for the requests that need the same IP.
const axios = require("axios");
const crypto = require("crypto");
const { HttpsProxyAgent } = require("https-proxy-agent");
const sessionId = crypto.randomBytes(4).toString("hex"); // 8 chars, a-f0-9
const user = "<USERNAME>-type-residential-country-us-lifetime-10-session-" + sessionId;
const agent = new HttpsProxyAgent(
"http://" + user + ":" + encodeURIComponent("<PASSWORD>") + "@gw.nodelighthouse.com:10000"
);
const client = axios.create({ httpsAgent: agent, proxy: false, timeout: 30000 });
(async () => {
console.log((await client.get("https://api.ipify.org")).data);
console.log((await client.get("https://api.ipify.org")).data); // same IP
})();Use SOCKS5
socks-proxy-agent works the same way. Use socks5h so DNS resolves through the proxy.
// npm i socks-proxy-agent
const axios = require("axios");
const { SocksProxyAgent } = require("socks-proxy-agent");
const agent = new SocksProxyAgent("socks5h://<USERNAME>-type-residential-country-us:<PASSWORD>@gw.nodelighthouse.com:11000");
axios
.get("https://api.ipify.org", { httpAgent: agent, httpsAgent: agent, proxy: false })
.then((res) => console.log(res.data));Questions
- Why not use axios' proxy option?
- axios' built-in proxy option has long-standing problems with HTTPS targets: it can send the request to the proxy without opening a proper tunnel. An agent such as https-proxy-agent opens a CONNECT tunnel, which is what the gateway expects for HTTPS.
- Does this work with fetch or got?
- got accepts the same agent objects (agent: { https: agent }). Node's built-in fetch uses undici, which needs undici's ProxyAgent instead of https-proxy-agent.
- My password has special characters. What should I do?
- Wrap it in encodeURIComponent() when building the proxy URL, as in the sticky example. Otherwise characters such as @ or : break the URL.
Related guides: Python requests · Scrapy · Go (net/http)
Try it on your own targets
A flat $1.65/GB on every plan, with state and city targeting included. Start with the $2 for 500 MB test pack, paid in crypto.