Skip to main content

Using proxies

Understand how to use proxies in your Puppeteer and Playwright requests, as well as a couple of the most common use cases for proxies.


Proxies are a great way of appearing as if you are making requests from a different location. A common use case for proxies is to avoid geolocation restrictions. For example your favorite TV show might not be available on Netflix in your country, but it might be available for Vietnamese Netflix watchers.

In this lesson, we'll be learning how to use proxies with Playwright and Puppeteer. This will be demonstrated with a Vietnamese proxy that we got by running this proxy-scraping actor on the Apify platform.

Adding a proxy

First, let's add our familiar boilerplate code for visiting Google and also create a variable called proxy which will point to our proxy server:

Note that this proxy may no longer be working at the time of reading. If you don't have a proxy to use during this lesson, we recommend using Proxy Scraper for a list of free ones, or checking out Apify proxy

import { chromium } from 'playwright';

// our proxy server
const proxy = '103.214.9.13:3128';

const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://google.com');

await page.waitForTimeout(10000);
await browser.close();

For both Puppeteer and Playwright, the proxy server's URL should be passed into the options of the launch() function; however, it's done a bit differently depending on which library you're using.

In Puppeteer, the server must be passed within the --proxy-server Chromium command line argument, while in Playwright, it can be passed into the proxy option.

import { chromium } from 'playwright';

const proxy = '103.214.9.13:3128';

const browser = await chromium.launch({
headless: false,
// Using the "proxy" option
proxy: {
// Pass in the server URL
server: proxy,

},
});
const page = await browser.newPage();
await page.goto('https://google.com');

await page.waitForTimeout(10000);
await browser.close();

And that's it! Now, when we visit Google, it's in Vietnamese. Depending on the country of your proxy, the language will vary.

Vietnamese Google

Note that in order to rotate through multiple proxies, you must retire a browser instance then create a new one to continue automating with a new proxy.

Authenticating a proxy

The proxy in the last activity didn't require a username and password, but let's say that this one does:

my.proxy.com:3001

One might automatically assume that this would be the solution:

// This code is wrong!
import { chromium } from 'playwright';

const proxy = 'my.proxy.com:3001';
const username = 'someUsername';
const password = 'password123';

const browser = await chromium.launch({
headless: false,
proxy: {
server: `http://${username}:${password}@${proxy}`,

},
});

However, authentication parameters need to be passed in separately in order to work. In Puppeteer, the username and password need to be passed into the page.authenticate() prior to any navigations being made, while in Playwright they just need to be passed into the proxy option object.

import { chromium } from 'playwright';

const proxy = 'my.proxy.com:3001';
const username = 'someUsername';
const password = 'password123';

const browser = await chromium.launch({
headless: false,
proxy: {
server: proxy,
username,
password,
},
});
// Proxy will now be authenticated

Next up

You already know how to launch a browser with various configurations, which means you're ready to learn about browser contexts. Browser contexts can be used to automate multiple sessions at once with completely different configurations. You'll also learn how to emulate different devices, such as iPhones, iPads, and Androids.