Proxy Port logo
How-to Guides > How to set up a proxy for got

How to set up a proxy for got

The easiest way to set up a proxy for the got library is to use the httpAgent helper package.
It can be hpagent or https-proxy-agent. We'll look at both.

hpagent

First install the package:

npm i hpagent
        
Then you need to pass HttpsProxyAgent instance to got config:
import got from 'got';
import { HttpsProxyAgent } from 'hpagent';

const httpsProxyAgent = new HttpsProxyAgent({proxy: 'http://127.0.0.1:8080'});

const options = { agent: { https: httpsProxyAgent } };

(async () => {
    let response = await got('https://example.com', options);
    console.log(response.body);
})();

            
        

https-proxy-agent

First install the package:

npm i https-proxy-agent
        
Then you need to pass HttpsProxyAgent instance to got config:
import got from 'got';
import HttpsProxyAgent from 'https-proxy-agent';

const httpsProxyAgent = new HttpsProxyAgent('http://127.0.0.1:8080');

const options = { agent: { https: httpsProxyAgent } };

(async () => {
    let response = await got('https://example.com', options);
    console.log(response.body);
})();

            
        
Proxy for scraping
More