Created
August 21, 2026 12:42
-
-
Save quantumproxies/6dca009ab2057ea8ef8af0a1bb0868a8 to your computer and use it in GitHub Desktop.
Puppeteer with an authenticated proxy — page.authenticate, and why the launch flag alone fails https://quanticdata.io/blog/how-to-use-a-proxy-in-puppeteer/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Puppeteer through an authenticated proxy. | |
| * | |
| * The trap: --proxy-server takes a host and port, and NOTHING ELSE. Credentials in | |
| * the URL are ignored by Chrome, and you get a 407 with no obvious explanation. | |
| * The username and password go through page.authenticate() instead — per page, | |
| * before the first navigation. | |
| * | |
| * npm i puppeteer | |
| * QD_PROXY_USER=... QD_PROXY_PASS=... node qd-puppeteer-proxy.mjs | |
| * | |
| * https://quanticdata.io/residential-proxies/ | |
| * Guide: https://quanticdata.io/blog/how-to-use-a-proxy-in-puppeteer/ | |
| */ | |
| import puppeteer from "puppeteer"; | |
| const GATEWAY = "pr.quanticdata.io:7777"; | |
| const USER = process.env.QD_PROXY_USER; | |
| const PASS = process.env.QD_PROXY_PASS; | |
| // Chrome takes one proxy per browser process, so the country is fixed at launch. | |
| // Need several countries at once? Launch several browsers, or use Playwright, | |
| // which sets the proxy per CONTEXT — see | |
| // https://github.com/quantumproxies/quanticdata-proxy-quickstart | |
| const COUNTRY = process.argv[2] || "us"; | |
| const SESSION = process.argv[3] || null; | |
| const browser = await puppeteer.launch({ | |
| headless: "new", | |
| args: [`--proxy-server=${GATEWAY}`, "--no-sandbox"], | |
| }); | |
| const page = await browser.newPage(); | |
| // Targeting lives in the username; the session suffix pins one exit IP. | |
| const username = [USER, "country", COUNTRY, ...(SESSION ? ["session", SESSION, "sessTime", "10"] : [])].join("-"); | |
| await page.authenticate({ username, password: PASS }); | |
| await page.goto("https://ipinfo.io/json", { waitUntil: "domcontentloaded" }); | |
| const body = await page.evaluate(() => document.body.innerText); | |
| console.log(`country=${COUNTRY}${SESSION ? ` session=${SESSION}` : " (rotating)"}`); | |
| console.log(body.trim()); | |
| await browser.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment