How to Use Mobile Proxieswith Puppeteer
Puppeteer supports proxies via Chrome launch arguments, but authentication requires a separate page.authenticate() call. This guide covers the full setup including IP rotation mid-session.
Basic Proxy Setup with Authentication
Install Puppeteer:
npm install puppeteer
Puppeteer uses Chrome's --proxy-server flag to set the proxy address. However, this flag does not accept credentials. You must call page.authenticate() before navigating to pass the username and password:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
args: ['--proxy-server=http://HOSTNAME:HTTP_PORT'],
});
const page = await browser.newPage();
await page.authenticate({
username: 'USERNAME',
password: 'PASSWORD',
});
await page.goto('https://httpbin.org/ip');
console.log(await page.content());
await browser.close();
})();Order matters: Call page.authenticate() before page.goto(). If you navigate first, the proxy will reject the request with a 407 status before credentials are sent.
IP Rotation Mid-Session
Trigger an IP change while Puppeteer is running by calling the mobileproxies.org rotation API. The modem disconnects and reconnects with a new carrier IP. You can use axios or the built-in fetch (Node.js 18+):
Using axios
const axios = require('axios');
const API_KEY = 'YOUR_API_KEY';
const SLOT_ID = 'YOUR_SLOT_ID';
await axios.post(
`https://buy.mobileproxies.org/api/v1/proxies/${SLOT_ID}/switch`,
null,
{ headers: { Authorization: `Bearer ${API_KEY}` } }
);
// Wait for modem reconnection (typically 10-30 seconds)
await new Promise(r => setTimeout(r, 10000));Using fetch (Node.js 18+)
const API_KEY = 'YOUR_API_KEY';
const SLOT_ID = 'YOUR_SLOT_ID';
await fetch(
`https://buy.mobileproxies.org/api/v1/proxies/${SLOT_ID}/switch`,
{
method: 'POST',
headers: { Authorization: `Bearer ${API_KEY}` },
}
);
// Wait for modem reconnection
await new Promise(r => setTimeout(r, 10000));No browser restart needed: After the modem reconnects, the same proxy hostname and port serve the new IP. Continue using the existing Puppeteer browser instance and pages.
Full Working Example
This example fetches proxy credentials from the API, browses with the proxy, rotates the IP, and verifies the new address:
const puppeteer = require('puppeteer');
const API_KEY = 'YOUR_API_KEY';
const API_BASE = 'https://buy.mobileproxies.org';
async function getProxyCredentials() {
const resp = await fetch(`${API_BASE}/api/v1/proxies`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
const proxies = await resp.json();
// Returns: [{ hostname, http_port, socks5_port, username, password, slot_id, ... }]
return proxies[0];
}
async function rotateIP(slotId) {
const resp = await fetch(
`${API_BASE}/api/v1/proxies/${slotId}/switch`,
{
method: 'POST',
headers: { Authorization: `Bearer ${API_KEY}` },
}
);
console.log('Rotation status:', resp.status);
// Wait for modem reconnection
await new Promise(r => setTimeout(r, 15000));
}
(async () => {
// Step 1: Get proxy credentials from the API
const proxy = await getProxyCredentials();
console.log(`Using proxy: ${proxy.hostname}:${proxy.http_port}`);
// Step 2: Launch Puppeteer with the proxy
const browser = await puppeteer.launch({
args: [`--proxy-server=http://${proxy.hostname}:${proxy.http_port}`],
});
const page = await browser.newPage();
await page.authenticate({
username: proxy.username,
password: proxy.password,
});
// Step 3: Browse with the current IP
await page.goto('https://httpbin.org/ip');
const ipBefore = await page.evaluate(() => document.body.innerText);
console.log('IP before rotation:', ipBefore);
// Step 4: Rotate the IP
await rotateIP(proxy.slot_id);
// Step 5: Verify the new IP
await page.goto('https://httpbin.org/ip');
const ipAfter = await page.evaluate(() => document.body.innerText);
console.log('IP after rotation:', ipAfter);
await browser.close();
})();Install dependencies: npm install puppeteer. Node.js 18+ is required for the built-in fetch. For older versions, install node-fetch or axios.
Important Notes
page.authenticate() is for the proxy, not the website
The page.authenticate() method responds to HTTP 407 (Proxy Authentication Required) challenges. It does not set credentials for the target website. If a site requires login, use page.type() to fill in forms or set cookies directly.
One proxy per browser instance
The --proxy-server flag is set at launch and applies to the entire browser. Unlike Playwright, Puppeteer does not support per-page or per-context proxies natively. To use different proxies for different accounts, launch separate browser instances.
Headless mode
Puppeteer runs headless by default since v22. The proxy configuration works identically in both headless and headful modes. To run with a visible browser for debugging, pass headless: false to puppeteer.launch().
SOCKS5 support
Puppeteer supports SOCKS5 proxies via the same --proxy-server flag. Change the URL to socks5://HOSTNAME:SOCKS5_PORT. Note that page.authenticate() does not work with SOCKS5 proxies — use IP-whitelisted access for SOCKS5 authentication.
Ready to automate with mobile proxies?
Get proxy credentials with HTTP and SOCKS5 endpoints, IP rotation API access, and carrier-grade IPs that pass anti-bot detection.