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

Scraping & code · Setup guide

How to use an authenticated proxy with Playwright

Playwright has proxy support built in. Pass server, username and password when you launch the browser or create a context. Examples for Node.js and Python.

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 Playwright, follow four steps: (1) Pass proxy: { server, username, password } to launch or newContext; (2) Use a new context per sticky line for clean cookies and its own IP; (3) Open your target page; (4) Close contexts when done to free connections. Take the host, port, username and password from a line in your ZenProxy generator.

You need Playwright 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. 1Pass proxy: { server, username, password } to launch or newContext.
  2. 2Use a new context per sticky line for clean cookies and its own IP.
  3. 3Open your target page.
  4. 4Close contexts when done to free connections.

Node.js

playwright-proxy.mjs
import { chromium } from 'playwright';

const browser = await chromium.launch({
  proxy: {
    server: 'http://HOST:PORT',
    username: 'USERNAME',
    password: 'PASSWORD',
  },
});

const page = await browser.newPage();
await page.goto('https://ipinfo.io/json');
console.log(await page.textContent('body'));

await browser.close();

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.

Python

Python
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(proxy={
        "server": "http://HOST:PORT",
        "username": "USERNAME",
        "password": "PASSWORD",
    })
    page = browser.new_page()
    page.goto("https://ipinfo.io/json")
    print(page.text_content("body"))
    browser.close()

One IP per browser context

Contexts have their own cookies and storage, and each can have its own proxy. Give each context a different sticky line to run several sessions from one browser.

JavaScript
import { chromium } from 'playwright';

const browser = await chromium.launch();

// Sticky lines from the generator: same server, different usernames.
const proxies = [
  { server: 'http://HOST:PORT', username: 'STICKY_USERNAME_1', password: 'PASSWORD' },
  { server: 'http://HOST:PORT', username: 'STICKY_USERNAME_2', password: 'PASSWORD' },
];

for (const proxy of proxies) {
  const context = await browser.newContext({ proxy });
  const page = await context.newPage();
  await page.goto('https://ipinfo.io/json');
  console.log(await page.textContent('body'));
  await context.close();
}

await browser.close();

Playwright Test

Set the proxy for every test in playwright.config.ts.

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    proxy: {
      server: 'http://HOST:PORT',
      username: 'USERNAME',
      password: 'PASSWORD',
    },
  },
});

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.

JavaScript
const context = await browser.newContext({
  proxy: { server: 'http://HOST:PORT', username: 'USERNAME-country-de', password: 'PASSWORD' },
});

SOCKS5

Playwright accepts socks5:// servers, but its username and password options are documented for HTTP proxies. Use HTTP lines when you log in with a password.

Use less data

JavaScript
await page.route('**/*', (route) => {
  const type = route.request().resourceType();
  return ['image', 'media', 'font'].includes(type) ? route.abort() : route.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 →

  • Error asking you to launch the browser with a global proxy

    Older Playwright versions need a launch proxy before contexts can set their own. Launch with proxy: { server: "http://per-context" }, or upgrade Playwright.

  • 407 or a proxy login page

    Check username and password. The country tag belongs in the username.More on this error →

  • Different IPs within one session

    Use a sticky line instead of a rotating one.More on this error →

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

Questions

Does Playwright support proxy authentication?

Yes. Pass proxy: { server, username, password } to launch() or newContext().

Can each Playwright context use a different proxy?

Yes. Pass a proxy object to browser.newContext(). Use a different sticky line per context to get a different IP for each.

Does Playwright support SOCKS5 with a username and password?

Playwright documents credentials for HTTP proxies. Use an HTTP line when you need a password.

How do I set a proxy for all Playwright tests?

Add use: { proxy: { server, username, password } } to playwright.config.ts.

Try it with Playwright 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 →