How to Set Up a Proxy in Playwright

Playwright makes proxy setup straightforward, but only after you know where the proxy belongs. If you are testing from 1 country or checking a geo-locked page, the browser’s traffic can be routed through a proxy instead of your local network. That changes the IP address the site sees. It also changes the risk profile.

A proxy in Playwright sits between the browser and the site. The browser sends requests to the proxy, and the proxy forwards them onward. The site sees the proxy’s IP, not yours. That matters for region checks, rate limits, and scripts that must behave differently across locations. If you want broader context on the surrounding tools, the VPN, proxy & privacy guides page covers the basics without overcomplicating the setup.

1. What a Proxy Does in Playwright

Playwright can send browser traffic through a proxy at launch time or at browser-context level. That means page visits, XHR calls, and most browser traffic can leave from the proxy’s address instead of the machine running the test. For a basic geo check, that is enough. For a more demanding automation job, it may be the difference between a clean run and a blocked one.

Think of a proxy as a gate with an address. The browser still behaves like a browser, and Playwright still drives it, but the site is looking at the gate. This is why teams use a proxy for testing region-specific pricing, content language, login flows, or country-based compliance pages. A site may show different shipping options to London and Lisbon. A proxy lets you confirm that without flying anywhere.

There is a second use case that gets less attention: IP rotation. A script that keeps hitting the same target from one address can trigger limits quickly. A proxy does not guarantee success, but it gives you a way to vary the outbound path. If you need a related primer on request behavior, see the how to hide your IP address guide.

2. Proxy Options You Can Use

Playwright supports a proxy object with a server URL and optional credentials. The common fields are simple: the proxy server address, a username, and a password. Some setups also use browser-context-level proxy configuration, which is useful when you want a proxy only for one isolated context rather than the entire browser session. That detail matters when one test needs a proxy and another should not touch one.

Proxy server formats usually include the protocol, host, and port. A typical value might look like http://proxy.example.com:8000. If the proxy requires login, Playwright can pass the username and password as part of the same object. The protocol must match the proxy type. A SOCKS5 endpoint is not the same as an HTTP proxy, and the wrong protocol causes avoidable failures. If you need to compare those formats, the SOCKS5 proxy vs HTTP proxy article helps keep the types straight.

Playwright also lets you scope the proxy more narrowly. That is useful for test suites where one browser context checks a regional storefront and another context checks a public homepage. One browser, two contexts, one proxy only where needed. Cleaner. Less surprise.

Before writing code, it helps to understand the vocabulary around ports, authentication, and protocols. The VPN and proxy glossary is handy when a provider uses terms like endpoint, tunnel, or upstream server in a way that sounds obvious until it is not.

3. Step 1: Install and Initialize Playwright

Start with a project that already runs a basic Playwright script. In Node.js, that usually means creating a folder, initializing package.json, and installing Playwright. One common path is the npm install step followed by a small test file that launches a browser and opens a page. Keep that first script plain. Do not add proxy settings yet.

A minimal starting point looks like this in concept: install Playwright, launch Chromium, open one page, then close the browser. That gives you a known-good baseline. If the browser opens a page without a proxy, you know the environment works before you change anything. This saves time later, because a broken proxy setup and a broken Playwright install can look annoyingly similar.

Test the script once. Then test it again. A proxy added too early can hide simple project problems, especially if the page never loads and the browser never starts cleanly. I have seen teams chase a proxy bug for an hour when the real issue was a missing dependency from the initial install.

4. Step 2: Add Proxy Configuration to the Browser Launch

This is the point where how to set up a proxy in Playwright becomes practical. The proxy object belongs in the browser launch options for the session you want to affect. In a typical JavaScript setup, that means passing a proxy field to launch(), alongside other browser options. The server address goes there first. Credentials, if needed, go there too.

A simple example structure is enough to show the shape:

launch options can include a proxy object with server, username, and password fields. The server points to the proxy host and port. The username and password are only included when the proxy requires them. If the proxy is plain and open, those fields are omitted.

Place the proxy object at the same level as other launch settings, not inside page.goto or some later navigation call. That matters because Playwright decides the browser’s outbound route when it starts the browser or context. Put the proxy in the wrong place, and nothing changes. Quiet failure is still failure.

Here is the pattern in plain English: create the browser with launch options, attach the proxy there, then open a context and page as usual. For one-off automation, this is often enough. For larger test suites, decide whether the proxy should apply to the whole browser run or only to one context. One setting can affect dozens of tests, so be deliberate.

5. Step 3: Handle Authenticated Proxies

Authenticated proxies ask for a username and password. Playwright supports that directly in the proxy object, which is convenient, but convenience should not become a habit of hardcoding secrets into test files. Put credentials in environment variables or a secrets manager. Plain text in a committed script is the kind of mistake that lingers in git history for years.

If the proxy provider gives you a username that includes a zone, customer ID, or session token, copy it exactly. A single missing character can cause 407 proxy authentication errors, and those errors are easy to misread if you are also dealing with site-level login issues. One proxy provider may want user:pass. Another may want a longer identifier with symbols. Match the provider’s format, not your guess.

Some teams rotate credentials per run. That works, but it needs discipline. The browser launch code should read the current secret from the environment at runtime, then throw it away when the process exits. Never leave the proxy password in a test fixture unless you enjoy cleanup work. It is dull work, too.

If your proxy provider supports authenticated SOCKS5 access, the setup details can differ from HTTP auth, so review the authenticated SOCKS5 proxy guide before assuming the same fields behave the same way. They often do not.

6. Step 4: Verify the Proxy Is Working

Do not trust the config alone. Verify the proxy in a live browser session. The simplest check is to visit an IP-check page and compare the reported address with your machine’s normal IP. If the site shows the proxy’s address, the browser traffic is taking the route you expected. If it shows your local address, the proxy is not active.

A second check is request behavior. Watch whether a site serves content from the expected country, language, or CDN edge. A US-only pricing page should not suddenly become a UK page unless the proxy location changed. That kind of mismatch tells you more than a generic green checkmark ever will.

You can also inspect browser logs or network behavior in a controlled test. One request to a known endpoint is enough. For example, if the proxy blocks a port or the protocol is wrong, the request may fail before page content loads. That failure is useful. It tells you the browser is trying to use the proxy at all.

For a tighter verification routine, the how to verify your IP is guide shows practical ways to confirm the address change instead of guessing from browser behavior alone.

7. Common Problems and Fixes

Connection failures are the first thing people see. The proxy host may be down, the port may be closed, or the protocol may be wrong. If an HTTP proxy is entered as SOCKS5, the browser may refuse to connect or may hang during startup. Check the scheme first, because that mistake is common and easy to fix.

Authentication errors come next. A 407 response usually means the proxy wants credentials and did not get them, or the credentials were wrong. Recheck the username, password, and any special formatting required by the provider. Some proxies want a long session string; some want a simple login. One missing separator can stop the whole run.

Unsupported proxy formats also cause confusion. Playwright expects the proxy data in a specific structure. If a provider gives you a URL with extra parameters, strip it down to the host, port, and required auth fields before passing it in. Weirdly formatted endpoints are common in provider dashboards, and not every string should be pasted directly into code.

Protocol mismatches are another slow-burning problem. A site might load through an HTTP proxy while a WebSocket call fails, or the other way around, depending on the target and the provider. If a workflow needs stronger control over proxy behavior, revisit the proxy authentication best practices guide and confirm the provider’s recommended format before changing the script again.

One more issue appears when the proxy works but the site still blocks. That usually means the problem is no longer transport, but fingerprinting, rate limits, or test behavior. The proxy did its job. The request pattern did not.

8. Best Practices for Stable Proxy Use

Keep proxy settings isolated per test run. If one suite needs a proxy and another does not, do not share the same browser state by accident. A clean browser context per run makes failures easier to trace, especially when you are comparing region-specific results or rotating endpoints between jobs. One context, one purpose.

Avoid hardcoded secrets. That is a rule worth repeating once, not ten times. Load proxy usernames and passwords from environment variables, CI secrets, or a vault system. If a teammate opens the code later, they should see the shape of the proxy setup, not the actual password. That keeps reviews simpler and accidental leaks less likely.

Match the proxy provider’s protocol and session rules before you scale up. A proxy that behaves well for one request can fail on the fifth if the provider caps concurrent connections or expects session reuse. If your workflow depends on rotating endpoints, the proxy rotation for web scraping guide explains the tradeoffs that matter most in production-like automation.

Test with one browser, one page, and one known IP-check page before running a full suite. That sounds basic because it is basic. Basic checks catch expensive mistakes. If a proxy setup survives one clean run, you can extend it to more contexts, more pages, and more test data without guessing whether the proxy is the thing breaking your run.

Keep the browser launch code close to the proxy decision. If a developer has to search through five files to find where the proxy is attached, the setup will drift. A direct launch block with a clear proxy object is easier to review, easier to audit, and easier to change when the provider updates the endpoint format.