> [!WARNING]
> [Zyte API](../../zyte-api/get-started.md#zyte-api) is replacing Smart Proxy Manager. It is
> no longer possible to sign up to Smart Proxy Manager. If you are an
> existing Smart Proxy Manager user, see spm-migrate.

# Using Smart Proxy Manager with Puppeteer

> [!WARNING]
> For the code below to work you must first [install the Zyte
> CA certificate](../../misc/ca.md#ca).

> [!NOTE]
> Puppeteer requires [Node.js](https://nodejs.org/en/download/), [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed and an active Smart Proxy Manager account. [Log in here](https://app.zyte.com/account/login).

> [!NOTE]
> All the code in this documentation has been tested with Node.js v14.15.1 and puppeteer 10.4.0

## Option 1 - Plugin for Puppeteer-Extra

Check out details [here](https://github.com/zytedata/zyte-smartproxy-plugin#quickstart-for-puppeteer-extra).

## Option 2 - Zyte SmartProxy Puppeteer

### Installation

Download and install [Zyte SmartProxy Puppeteer](https://www.npmjs.com/package/zyte-smartproxy-puppeteer):

```
$ npm install zyte-smartproxy-puppeteer
```

### Sample script

Zyte SmartProxy Puppeteer is a client library that provides Zyte Smart Proxy Manager related functionalities over
Puppeteer.

1. In order to run the sample code present below save it in a file named `sample.js`:

```javascript
const puppeteer = require('zyte-smartproxy-puppeteer');
(async () => {
    const browser = await puppeteer.launch({
        spm_apikey: '<Smart Proxy Manager API KEY>',
        ignoreHTTPSErrors: true,
        headless: false,
    });
    console.log('Before new page');
    const page = await browser.newPage();
    console.log('Opening page ...');
    try {
        await page.goto('https://toscrape.com/', {timeout: 180000});
    } catch(err) {
        console.log(err);
    }
    console.log('Taking a screenshot ...');
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();
```

1. Now run the sample code using Node:

```bash
node sample.js
```

To know more about what you can do with Zyte SmartProxy Puppeteer, check out the
[API Documentation](https://github.com/zytedata/zyte-smartproxy-puppeteer#api).

## Option 3 - Zyte SmartProxy Headless Proxy

### Installation

1. Setup the Zyte SmartProxy (formerly Crawlera) Headless Proxy as described in spm-headless.
2. Download and install [Puppeteer](https://developers.google.com/web/tools/puppeteer/get-started):
   ```
   $ npm install puppeteer
   ```

### Sample script

Here is a sample script you can use to test the integration of Puppeteer with
Smart Proxy Manager, once you have started the Headless Proxy.

```javascript
const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
        ignoreHTTPSErrors: true,
        headless: false,
        args: [
            '--proxy-server=localhost:3128'
        ]
    });
    const page = await browser.newPage({ignoreHTTPSErrors: true});

    console.log('Opening page ...');
    try {
        await page.goto('https://toscrape.com/', {timeout: 180000});
    } catch(err) {
        console.log(err);
    }

    console.log('Taking a screenshot ...');
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();
```
