← Docs / Proxies
Datacenter proxies with Puppeteer
Chrome takes the proxy address as a launch flag, and Puppeteer supplies the credentials. This page uses the datacenter tier: a US datacenter IP that changes for every new connection.
What you need
- Your proxy API key, from the Proxies page in the portal.
npm install puppeteer(orpuppeteer-corewith your own Chrome).
export PB_PROXY_KEY="YOUR_KEY"
Launch a browser through the proxy
import puppeteer from "puppeteer";
const browser = await puppeteer.launch({
args: ["--proxy-server=http://proxy.privatebyte.com:3128"],
});
const page = await browser.newPage();
await page.authenticate({
username: process.env.PB_PROXY_KEY,
password: "datacenter",
});
await page.goto("https://api.ipify.org");
console.log(await page.evaluate(() => document.body.innerText)); // a US datacenter address
await browser.close();
Call page.authenticate() before the first goto(). The flag sets where the proxy is, and authenticate() answers its login prompt.
Get a new IP
One browser keeps its connections open, so every page in it leaves from the same address. Create a new browser context to get a new one:
for (let i = 0; i < 3; i++) {
const context = await browser.createBrowserContext();
const page = await context.newPage();
await page.authenticate({
username: process.env.PB_PROXY_KEY,
password: "datacenter",
});
await page.goto("https://api.ipify.org");
console.log(await page.evaluate(() => document.body.innerText));
await context.close();
}
Authenticate each new page, because a new context starts clean. In older Puppeteer versions the method is createIncognitoBrowserContext().
Know the limits
- The datacenter tier is US only and has no sessions. A site that checks the reputation of an address may block it. If you see
403or a challenge page, use a residential tier such asstandard. - Chrome remembers proxy logins for an address until it restarts. Keep the password the same within one browser run.
Errors
| What you see | What it means |
|---|---|
ERR_TUNNEL_CONNECTION_FAILED with a 407 |
The API key is wrong or missing. |
ERR_TUNNEL_CONNECTION_FAILED with a 400 |
The password asked for something this tier does not do: a country other than US, a session or a TTL. |
A page returns 403 |
The site is refusing datacenter addresses. |
A 400 that names an unknown tier |
The tier word is a close misspelling of a real tier, such as datacentre. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier. |
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.