New Residential Premium, our top-tier pool for the toughest sites
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 in 48h or refunded.

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.

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky), the Protocol (HTTP or SOCKS5) and a Format, then copy a line. The generator offers three formats:

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). Datacenter and ISP plans give you a list of dedicated IPs, one per line as IP:PORT:USERNAME:PASSWORD, and each line is one fixed IP.

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.

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();

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

On residential plans, add -country- and a two-letter country code to the end of the username, for example USERNAME-country-us or USERNAME-country-gb. To combine a country with a state, a city or a sticky session, build the line in the generator, which shows only the options your plan supports. 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 in 48h or refunded.

Related guides

All integrations →