How to use proxy per page with Puppeteer

Updated on Dmytro Krasun 3 min read Puppeteer
It is easy to use proxy globally for the puppeteer instance, but there is a trick to use proxy on a per-page basis.

Puppeteer allows you to automate everything you can do in the browser manually and even more. There are cases when you need to use a proxy. It is easy to use proxy globally for the puppeteer instance, but there is a trick to use proxy on a per-page basis.

Puppeteer plugin for proxying requests per page

Installation and usage

Make sure you installed the puppeteer library first:

Terminal window
npm i puppeteer

We are going to use an excellent plugin puppeteer-page-proxy to solve the problem with proxy per page.

The plugin supports:

  • proxy per page and proxy per request;
  • http, https, socks4 and socks5 proxies;
  • authentication;
  • and cookies.

Install:

Terminal window
npm i puppeteer-page-proxy

Then you can import the library and use proxy on per-page basis:

const puppeteer = require("puppeteer");
const useProxy = require("puppeteer-page-proxy");
(async () => {
const browser = await puppeteer.launch({});
try {
const page = await browser.newPage();
useProxy(page, "socks5://127.0.0.1:9876");
await page.goto("https://example.com/", {
waitUntil: "networkidle0",
});
await page.screenshot({ path: "example.com.png" });
} catch (e) {
console.log(e);
} finally {
await browser.close();
}
})();

In this example I take a screenshot through proxy.

Authentication

To use authentication with proxying, you need to specify user and password in your proxy connection URL:

const puppeteer = require("puppeteer");
const useProxy = require("puppeteer-page-proxy");
(async () => {
const browser = await puppeteer.launch({});
try {
const page = await browser.newPage();
useProxy(page, "https://user:password@host:port");
await page.goto("https://example.com/", {
waitUntil: "networkidle0",
});
await page.screenshot({ path: "example.com.png" });
} catch (e) {
console.log(e);
} finally {
await browser.close();
}
})();

Proxy per request

You can go further and proxy on per request basis:

const puppeteer = require("puppeteer");
const useProxy = require("puppeteer-page-proxy");
(async () => {
const browser = await puppeteer.launch({});
try {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", async (request) => {
await useProxy(request, "https://127.0.0.1:443");
});
await page.goto("https://example.com/", {
waitUntil: "networkidle0",
});
await page.screenshot({ path: "example.com.png" });
} catch (e) {
console.log(e);
} finally {
await browser.close();
}
})();

This example is not different from the regular usage because we proxy every page request. But! Imagine you don’t want to load your proxy bandwidth and skip proxying images. This way, it becomes more valuable:

const puppeteer = require("puppeteer");
const useProxy = require("puppeteer-page-proxy");
(async () => {
const browser = await puppeteer.launch({});
try {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", async (request) => {
if (request.resourceType() === "image") {
request.abort();
} else {
await useProxy(request, "socks4://127.0.0.1:1080");
}
});
await page.goto("https://example.com/", {
waitUntil: "networkidle0",
});
await page.screenshot({ path: "example.com.png" });
} catch (e) {
console.log(e);
} finally {
await browser.close();
}
})();

The plugin covers as many cases as you can image.

Proxy taking screenshots with API

If, in your case, you want to take screenshots or render HTML and you are OK with saving time and money, you can use our screenshot API with proxy to take screenshots.

That’s it. If you want to check out the complete guide on how to take screenshots with Puppeteer, you are welcome, and have a nice day!