In Crawlee, a proxy is defined with a ProxyConfiguration object and passed to the crawler through the proxyConfiguration option. The object picks a proxy URL for every request. With the session pool on, it binds each session to a single URL. CheerioCrawler and PlaywrightCrawler accept the same object. The trap is that some errors stay silent: on a plain HTTP target, the proxy's 407 response is processed like a page. This post covers the URL list, session mapping, the custom function and debugging.
Adding a proxy to an existing crawler
Adding a proxy to an existing crawler takes two lines. First you build the ProxyConfiguration object, then you add it to the crawler options. The request handler, queue and router stay as they are.
import { CheerioCrawler, ProxyConfiguration } from 'crawlee';
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: ['http://user:pass@pr.proxynet.io:8000'],
});
const crawler = new CheerioCrawler({
proxyConfiguration, // the added line
async requestHandler({ request, $, proxyInfo }) {
console.log(request.url, $('title').text(), proxyInfo?.port);
},
});
await crawler.run(['https://example.com/']);proxyUrls is an array, and Crawlee hands out its URLs in turn. A null entry means the request in that slot goes out without a proxy. If you use a single gateway URL, the array has one entry. IP rotation is then done by the gateway, not by Crawlee.
Even when the target is HTTPS, the URL starts with http://. Crawlee asks the proxy for a CONNECT tunnel and sets up TLS inside it. CheerioCrawler rejects a socks5:// URL. In our test (Crawlee 3.18.2), the request failed with Proxy URL protocol "socks5:" is not supported. Proxynet offers HTTP and SOCKS5 together, so use the HTTP URL in Crawlee.
Session and proxy mapping
With the session pool on, Crawlee binds each session to the first proxy URL it gets. The pool is on by default (useSessionPool: true). Later requests with the same session use the same URL. persistCookiesPerSession is also on by default, so cookies are stored on the session.
We measured the mapping with two local test proxies. The results:
| Setting | 4 HTTP requests | Result |
|---|---|---|
| Default pool | 4 different sessions | 8001, 8002, 8001, 8002 |
maxPoolSize: 1 | 1 session | All 8001 |
useSessionPool: false | No sessions | 8001, 8002, 8001, 8002 |
In the default pool, the first requests open new sessions, because the pool grows up to 1,000 sessions. A session's lifetime is limited too: the default maxAgeSecs is 3,000 seconds and maxUsageCount is 50. If you use sticky sessions on the gateway, align this lifetime with the sticky duration.
const crawler = new CheerioCrawler({
proxyConfiguration,
useSessionPool: true, // default
persistCookiesPerSession: true, // default
sessionPoolOptions: {
maxPoolSize: 20,
sessionOptions: { maxAgeSecs: 600 }, // same as a 10-minute sticky duration
},
async requestHandler({ request, session, proxyInfo }) {
console.log(request.url, session?.id, proxyInfo?.sessionId);
},
});At Proxynet, the Sticky Proxy duration is set between 1 and 60 minutes. If the Crawlee session outlives the IP, its cookies stay tied to the old IP. If it is shorter, cookies are thrown away for no reason. We cover how cookies relate to the IP in What Is a Sticky Session?.
Choosing with newUrlFunction
To pick the URL by condition, use newUrlFunction. The function receives the session ID as its first argument and the request in the second. It returns the proxy URL. To go without a proxy, it returns null.
const proxyConfiguration = new ProxyConfiguration({
newUrlFunction: (sessionId, { request } = {}) => {
// Reach the internal service without a proxy
if (request?.url.includes('/internal/')) return null;
return 'http://user:pass@pr.proxynet.io:8000';
},
});The second argument does not come with every call, so the = {} default is needed. In our test, the /internal/ URL went out directly and proxyInfo came back empty. The other request went through the proxy. If your provider wants the session ID in the username, this function is also the place to build that name in the documented format.
proxyUrls and newUrlFunction cannot be used together. When both are given to the same object, the constructor throws Cannot combine custom proxies. Don't try to get past a site's refusal by switching proxies: 403 and 429 mean the site is stopping you or asking you to slow down. Set maxRequestsPerMinute for pace and respectRobotsTxtFile for robots.txt rules, and follow the site's terms.
Special characters in the password
A proxy URL is a URL, and characters such as #, / and ? in the password break it apart. The fix is to encode the credentials with encodeURIComponent. Crawlee decodes the password before sending it, and the proxy receives the original.
const user = encodeURIComponent(process.env.PROXY_USER); // user
const pass = encodeURIComponent(process.env.PROXY_PASS); // pass@123 -> pass%40123
const proxyConfiguration = new ProxyConfiguration({
proxyUrls: [`http://${user}:${pass}@pr.proxynet.io:8000`],
});In our test, an unencoded # made the constructor fail on the first line with the Any predicate failed validation error. An unencoded @ worked, because Node's URL parser treats the last @ as the separator. Encode it anyway: the same URL breaks in another tool. We explain each part of the URL in How to Read a Proxy URL.
Read the credentials from environment variables instead of writing them into the code. One more warning: proxyInfo.url carries the password in plain text. Log proxyInfo.hostname, proxyInfo.port and proxyInfo.sessionId instead.
proxyInfo in PlaywrightCrawler
PlaywrightCrawler accepts the same ProxyConfiguration object. The difference is that the proxy is picked when the browser launches. Pages in the same browser share that proxy.
In our test, this difference made proxyInfo misleading. With default settings, the second page's proxyInfo.url showed 8002, but the traffic went through 8001. With useIncognitoPages: true, each page got its own context and its own proxy. Across three pages, proxyInfo matched the real port.
import { PlaywrightCrawler } from 'crawlee';
const crawler = new PlaywrightCrawler({
proxyConfiguration,
launchContext: { useIncognitoPages: true },
async requestHandler({ page, proxyInfo }) {
console.log(await page.title(), proxyInfo?.port);
},
});For an HTTP proxy with credentials, Crawlee puts a local relay without a password in front of the browser. That is why Chrome shows no login prompt. In our test, Chrome's own background requests also went through the proxy. Count them in your traffic budget too.
Debugging
Crawlee often shows proxy errors as session errors. The message starts with Detected a session error, rotating session..., and the real cause is on the next line. These errors are not subject to the maxRequestRetries limit but to maxSessionRotations, which defaults to 10. That is why the request with the wrong password was tried 11 times in our test.
| Symptom | Cause | Fix |
|---|---|---|
Proxy responded with 407 (HTTPS target) | Wrong username or password | Check the credentials and encoding |
| Handler processes a page with status 407 (HTTP target) | 407 is not treated as an error | additionalHttpErrorStatusCodes: [407] |
connect ECONNREFUSED …:80 | No port in the URL | Add the port, such as :8000 |
Proxy URL protocol "socks5:" is not supported | CheerioCrawler does not take SOCKS | Use an http:// URL |
Any predicate failed in the constructor | Unencoded # or a broken URL | Use encodeURIComponent |
Cannot combine custom proxies | Both methods on one object | Keep only one |
The second row is the quietest. Of the 4xx codes, Crawlee treats only 401, 403 and 429 as errors. The rest, 407 included, reach the handler, and the proxy's error page is processed like data. With additionalHttpErrorStatusCodes: [407], the request is marked as failed. For a general diagnosis of 407, see the HTTP 407 error post.
How we tested
We ran the tests on December 2, 2026, on Windows 11, with Node 24.22.2, Crawlee 3.18.2 and Playwright 1.65.2. Playwright used the Chrome installed on the machine via channel: 'chrome'. On 127.0.0.1 we ran two HTTP proxies that required Basic authentication (8001 and 8002). Both logged every incoming request, its target and the decoded credentials. As targets we used a local HTTP page and a self-signed HTTPS page. The HTTP page wrote into its body which proxy had forwarded the request. In the examples, we replaced the local address with pr.proxynet.io:8000.
The bottom line
Build the object once, pass it to the crawler and align the session lifetime with the sticky duration. Encode the password with encodeURIComponent and keep proxyInfo.url out of your logs. Above all, add additionalHttpErrorStatusCodes: [407] for plain HTTP targets, or a wrong password will look like data. Once the URL is ready, you can start your first crawl with Residential Proxy.
Sources
- Crawlee docs, Proxy Management guide: crawlee.dev
- Crawlee API, ProxyConfiguration class: crawlee.dev
- RFC 9110, §15.5.8 407 Proxy Authentication Required: rfc-editor.org
- MDN, encodeURIComponent(): developer.mozilla.org
Frequently asked questions
Can I use a SOCKS5 proxy in Crawlee?
Not in CheerioCrawler. In our test, Crawlee 3.18.2 rejected a socks5:// URL with Proxy URL protocol "socks5:" is not supported. HTTP-based crawlers accept only http:// and https:// proxy URLs. If your proxy offers both protocols, use the HTTP URL in Crawlee. HTTPS targets still travel encrypted, because Crawlee opens a CONNECT tunnel and sets up TLS inside it.
Can proxyUrls and newUrlFunction be used together?
No. Only one of the proxyUrls and newUrlFunction options can be given. If you put both on the same object, the constructor throws Cannot combine custom proxies. If you want to pick from a list and also apply a condition, keep the list inside newUrlFunction. The function can look at the session ID or the request URL and return an entry from the list.
What happens if I turn off the session pool?
With useSessionPool: false, requests carry no session ID. Crawlee hands out the proxyUrls list in turn on every request and binds no request to a URL. Cookies are not stored on a session either. If persistCookiesPerSession: true is set alongside this setting, the crawler throws an error. Keep the pool on for any flow that needs a login or moves forward with cookies.
Why is proxyInfo undefined?
Because the selected URL is null. When newUrlFunction returns null, or the turn comes to a null entry in the proxyUrls list, the request goes out without a proxy and proxyInfo stays empty. The same happens if the crawler was never given proxyConfiguration. Use optional access such as proxyInfo?.port in your code, or the handler will throw on requests without a proxy.
How do I write a proxy password that contains @?
As %40. Encoding the password with encodeURIComponent handles @, #, / and : all at once. Crawlee decodes the encoded password before sending it. In our test, the proxy received the password pass@123 correctly. An unencoded #, on the other hand, caused a validation error in the ProxyConfiguration constructor. Always encode.
How long does a Crawlee session live?
By default 3,000 seconds or 50 uses, whichever comes first. You change these values with maxAgeSecs and maxUsageCount inside sessionPoolOptions.sessionOptions. If you use sticky sessions on the gateway, set maxAgeSecs equal to the sticky duration. That way the Crawlee session and the exit IP are renewed at the same time, and cookies do not stay tied to an old IP.




