Scraping and automation libraries

How to use a proxy with Playwright

Playwright drives Chromium, Firefox and WebKit from Python, Node.js, Java and .NET, and it is the common choice for scraping pages that need JavaScript. It has a proxy option with separate server, username and password fields, so authentication works without browser prompts or extensions.

You can set the proxy when launching the browser or per browser context. Per-context proxies are the most useful pattern: each context gets its own sticky session, cookies and IP, all inside one browser process.

Updated 23 November 2026

What you need
Hostgate.firstproxy.shop
HTTP / HTTPS port8000
SOCKS5 port9000
Usernameuser-country-us
PasswordYOUR_TOKEN

Use the HTTP port 8000. Playwright accepts socks5:// servers, but browsers do not reliably support a username and password on SOCKS5, so authenticated SOCKS5 is not recommended here. Your username and token appear on the Proxy setup page of your dashboard after your first order.

Setup

How do I connect Playwright to a proxy?

Follow the steps in order. Replace YOUR_TOKEN with the token from your dashboard and change the country, city or session in the username to suit the job.

  1. Install Playwright and a browser

    Install the library, then download the browser build Playwright is tested against.

    python -m pip install playwright
    python -m playwright install chromium
  2. Launch a browser through the proxy

    Pass server without credentials and give username and password separately. Playwright answers the proxy authentication challenge for every page in that browser.

    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.launch(proxy={
            "server": "http://gate.firstproxy.shop:8000",
            "username": "user-country-us",
            "password": "YOUR_TOKEN",
        })
        page = browser.new_page()
        page.goto("https://ipinfo.io/json")
        print(page.inner_text("body"))
        browser.close()
  3. Give each context its own sticky IP

    Set the proxy on new_context instead of launch. Use a different session name per context and a ttl long enough for the whole flow. Each context then keeps its own IP, cookies and storage, which is how you run several accounts or regions in parallel.

    with sync_playwright() as p:
        browser = p.chromium.launch()
        for n, country in enumerate(["us", "gb", "de"]):
            ctx = browser.new_context(proxy={
                "server": "http://gate.firstproxy.shop:8000",
                "username": f"user-country-{country}-session-ctx{n}-ttl-30m",
                "password": "YOUR_TOKEN",
            })
            page = ctx.new_page()
            page.goto("https://ipinfo.io/json")
            print(country, page.inner_text("body"))
            ctx.close()
        browser.close()
  4. Match locale and timezone to the exit

    A US IP with a browser that reports a European timezone is an easy signal for anti-bot checks. Set locale and timezone_id (Python) or timezoneId (Node.js) on the context to match the country and city in the username.

    ctx = browser.new_context(
        proxy={
            "server": "http://gate.firstproxy.shop:8000",
            "username": "user-country-us-city-new-york-session-nyc1-ttl-20m",
            "password": "YOUR_TOKEN",
        },
        locale="en-US",
        timezone_id="America/New_York",
    )
  5. Save traffic by blocking heavy files

    Residential proxies are billed per GB, and images, fonts and video are usually most of a page. Abort them with a route when you only need text and data.

    BLOCK = {"image", "media", "font"}
    
    def block_heavy(route):
        if route.request.resource_type in BLOCK:
            route.abort()
        else:
            route.continue_()
    
    page.route("**/*", block_heavy)
Test

How do I check that the proxy works?

Open ipinfo.io/json in the proxied page and print the body. For a per-context setup, run the check once per context: each should show its own country and IP.

A working proxy returns a response like the one shown, trimmed to the main fields. The IP belongs to the exit, the city and country follow your username, and the time zone is where that IP is located, here United States.

page.goto("https://ipinfo.io/json")
print(page.inner_text("body"))
ipinfo.io/json response
{
  "ip": "203.0.113.24",
  "city": "New York",
  "country": "US",
  "timezone": "America/New_York"
}

203.0.113.24 is a documentation address. Your result shows a real exit IP.

Targeting

How do I set country, city and session in the username?

All targeting is written into the username, so Playwright needs no extra settings to change location. Parts are joined with hyphens in this order after your proxy user. The password is always your token.

Proxy username parts
PartFormatExampleRule
Country-country-{cc}-country-usRequired. Two-letter country code in lowercase. 64 countries are available.
City-city-{city}-city-new-yorkOptional. City name in lowercase, spaces become hyphens. 172 cities are available.
Session-session-{id}-session-a1b2c3Optional. Any letters and digits you choose. The same id keeps the same IP, a new id gets a new IP.
Session length-ttl-{minutes}m-ttl-30mUsed with a session. Residential 1 to 120 minutes, mobile 1 to 60, long-acting ISP up to 720 (12 hours).
Full usernameuser-country-us-city-new-york-session-a1b2c3-ttl-30m

Find country codes and city values on the proxy locations page.

Troubleshooting

Playwright proxy errors and fixes

The problems people run into most often with Playwright, with the message you are likely to see and how to solve it.

net::ERR_TUNNEL_CONNECTION_FAILED

In Chromium this usually means the proxy answered 407. Check the username and token, and make sure credentials are in the username and password fields, not inside the server URL.

net::ERR_PROXY_CONNECTION_FAILED

The browser cannot reach the gateway. Check the host spelling and port 8000, and that a firewall or corporate network allows outbound connections to it.

Timeout 30000ms exceeded on page.goto

Proxied pages with many assets can take longer. Use wait_until="domcontentloaded", raise the timeout with page.set_default_navigation_timeout(60000), and block heavy resources.

The IP changes between pages

Without a session part, new connections get new IPs, and a browser opens many connections. Add -session-{name}-ttl-{minutes}m to the username for any flow that must stay on one IP.

Location checks still fail

WebRTC and the reported timezone can contradict the IP. Match timezone_id and locale, and for Chromium add --force-webrtc-ip-handling-policy=disable_non_proxied_udp to the launch args so WebRTC cannot use UDP outside the proxy. Then test the context on a WebRTC leak page.

FAQ

Playwright proxy questions

Short answers to what people ask most about using a proxy with Playwright.

How do I use an authenticated proxy in Playwright?

Pass proxy={"server": "http://gate.firstproxy.shop:8000", "username": "...", "password": "YOUR_TOKEN"} to launch or new_context. Playwright handles the authentication challenge, so no extension or prompt is needed.

Can each Playwright context use a different proxy?

Yes. Set the proxy option on new_context. Each context can have its own username, which means its own country, session and IP, while sharing one browser process.

Does Playwright support SOCKS5 proxies?

Playwright accepts socks5:// servers, but username and password on SOCKS5 are not reliably supported by the browsers it drives. With credentials, use the HTTP port, which tunnels HTTPS pages as well.

Why does my Playwright scraper use so much proxy traffic?

Full page loads include images, fonts, video and tracking scripts. Block those resource types with page.route and reuse a context for several pages of the same site to keep traffic low.

Is the proxy setup the same in Python and Node.js?

Yes. The option has the same three fields in every language. Only the method names differ, such as new_context in Python and newContext in Node.js.

Get your token and run the first Playwright request

Create an account, top up from $40 with crypto, and copy the gateway, username and token from the dashboard.