Black Friday Up to 20% off with code BLACKFRIDAY until Tue 1 Dec, 23:59 UK time
PP

Scraping & code · Setup guide

How to use an authenticated proxy with Puppeteer

Chrome takes the proxy address as a launch flag, and Puppeteer’s page.authenticate() answers the proxy’s username and password challenge. Here is the full setup, plus per-context proxies, sticky sessions and SOCKS5.

Broken on our side? Fixed within 48 hours of your report, or the unused part refunded. A website blocking the proxies isn’t covered. Refund Policy

Quick answer

To use a proxy with Puppeteer, follow four steps: (1) Launch with args: ["--proxy-server=http://HOST:PORT"]; (2) Call page.authenticate({ username, password }); (3) Open your target page; (4) Use one sticky line per browser context. Take the host, port, username and password from a line in your ZenProxy generator.

You need Puppeteer and a ZenProxy proxy line from the generator. In the examples, replace HOST, PORT, USERNAME and PASSWORD for the values in your line (where to find them).

Quick setup

  1. 1Launch with args: ["--proxy-server=http://HOST:PORT"].
  2. 2Call page.authenticate({ username, password }).
  3. 3Open your target page.
  4. 4Use one sticky line per browser context.

Launch with a proxy and log in

puppeteer-proxy.mjs
import puppeteer from 'puppeteer';

const browser = await puppeteer.launch({
  args: ['--proxy-server=http://HOST:PORT'],
});

const page = await browser.newPage();
await page.authenticate({ username: 'USERNAME', password: 'PASSWORD' });

await page.goto('https://ipinfo.io/json');
console.log(await page.evaluate(() => document.body.innerText));

await browser.close();

Keep the username and password out of --proxy-server. Chrome ignores credentials in that flag, which is why page.authenticate() exists.

Call page.authenticate() on every new page before its first goto(), including pages opened from a popup or a new tab.

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky; static IP plans skip this and show your list of IPs), the Protocol (HTTP, or SOCKS5 where your plan offers it) and a Format, then copy a line. The generator offers three formats (SOCKS5 lines always use the protocol:// format):

HOST:PORT:USERNAME:PASSWORD

Paste into apps and anti-detect browsers that split the fields or auto-fill them.

USERNAME:PASSWORD@HOST:PORT

Tools that want credentials before the address.

http://USERNAME:PASSWORD@HOST:PORT

Code and command-line tools. This is the proxy URL used in the examples below.

In the examples on this page, replace USERNAME, PASSWORD, HOST and PORT with the values from your line. SOCKS5_PORT is the port the generator shows when you pick SOCKS5.

Residential lines are either rotating (a new IP on new connections) or sticky (one IP for the time you choose). Static Datacenter, Shared ISP and Private ISP plans give you a fixed list of IPs, one per line as IP:PORT:USERNAME:PASSWORD: Private ISP IPs are yours alone, while Shared ISP IPs can also be used by other customers. Rotating Datacenter and IPv6 plans give you one endpoint, with rotating or sticky lines; IPv6 only reaches sites that have an IPv6 address.

To test a line, open https://ipinfo.io/json through it: it shows the exit IP and country. For HTTP lines you can also use the free proxy checker.

A different proxy for each browser context

Browser contexts are isolated, like separate incognito windows, and each can have its own proxy. This lets one browser run several sessions with different IPs.

JavaScript
import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

// Two sticky lines from the generator: same host and port, different usernames.
const lines = [
  { username: 'STICKY_USERNAME_1', password: 'PASSWORD' },
  { username: 'STICKY_USERNAME_2', password: 'PASSWORD' },
];

for (const credentials of lines) {
  const context = await browser.createBrowserContext({ proxyServer: 'http://HOST:PORT' });
  const page = await context.newPage();
  await page.authenticate(credentials);
  await page.goto('https://ipinfo.io/json');
  console.log(await page.evaluate(() => document.body.innerText));
  await context.close();
}

await browser.close();

In Puppeteer versions before 22, the method is called createIncognitoBrowserContext().

Sticky sessions

Use sticky lines in browsers. A browser opens many connections for one page, so with a rotating line a single page load can come from several IPs, which breaks logins and looks unnatural. To make sticky lines, open the generator, set Proxy Type to Sticky, choose a Sticky Duration if your plan offers one, set how many lines you want in Sticky Amount, then copy or download them. Each line keeps its IP for the chosen time. The session part of a sticky username differs by plan, so always generate sticky lines instead of editing usernames by hand.

Target a country

Build the line in the generator: it writes the country, and a state, a city or a sticky session if you want one, into the username in the format your plan uses, and shows only the options your plan supports. On most residential plans the country is -country- and a two-letter code at the end of the username (for example USERNAME-country-gb), but older Unlimited Residential plans use a different format, so do not edit that username by hand. Datacenter and ISP IPs have a fixed location that you choose when you buy them. In Puppeteer, the country tag goes in the username you pass to page.authenticate().

JavaScript
await page.authenticate({ username: 'USERNAME-country-us', password: 'PASSWORD' });

SOCKS5

Chrome doesn’t support usernames and passwords for SOCKS5 proxies, so use HTTP lines with Puppeteer. SOCKS5 in Chrome works only without a password, and our plans log in with a username and password (login by IP isn’t available yet), so it isn’t an option with them.

Use less data

Residential plans bill by the gigabyte, and images, fonts and video are most of a page’s weight. Skip them when you only need the HTML.

JavaScript
await page.setRequestInterception(true);
page.on('request', (req) => {
  if (['image', 'media', 'font'].includes(req.resourceType())) req.abort();
  else req.continue();
});

Troubleshooting

  • net::ERR_TUNNEL_CONNECTION_FAILED

    The proxy refused the CONNECT request. Run the line through curl -v to see the real status (407, 502 or 503).More on this error →

  • net::ERR_PROXY_CONNECTION_FAILED

    The browser couldn’t reach the proxy at all. Check the host and port, and that you used the HTTP port for an http:// proxy.More on this error →

  • A 407 page, or net::ERR_INVALID_AUTH_CREDENTIALS

    The page has no credentials or the wrong ones. Call page.authenticate() on that page before goto(), and check the line.More on this error →

  • Different IPs within one page load

    You’re using a rotating line in a browser. Switch to a sticky line.More on this error →

Seeing another error? Read proxy errors and how to fix them, or contact support.

Questions

Can I put the username and password in --proxy-server?

No. Chrome ignores credentials in that flag. Pass the address with --proxy-server and the credentials with page.authenticate().

Does Puppeteer support SOCKS5 proxies with a password?

No. Chrome can’t authenticate to SOCKS5 proxies. Use an HTTP line: our plans log in with a username and password (login by IP isn’t available on them yet).

Can each page use a different proxy?

Each browser context can. Create contexts with browser.createBrowserContext({ proxyServer }) and call page.authenticate() with that context’s credentials.

How do I change IP in Puppeteer?

Open a new browser context (or a new browser) with a different sticky line. Sticky lines keep their IP for the duration you chose in the generator.

Try it with Puppeteer for $0.49

Get 1 GB of Residential Lite, then copy your rotating or sticky lines from the generator and paste them into the steps above.

Broken on our side? Fixed within 48 hours of your report, or the unused part refunded. A website blocking the proxies isn’t covered. Refund Policy

Related guides

All integrations →