🚀 We are hiring! See open positions

How to save and load cookies in Puppeteer?

When web scraping, we often need to save the connection state like browser cookies and resume it later. Using Puppeteer, to save and load cookies we can use page.cookies() and page.setCookie() methods:

javascript
const puppeteer = require('puppeteer');
const fs = require('fs').promises;

async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    // get some cookies:
    await page.goto("https://httpbin.dev/cookies/set/mycookie/myvalue");
    // then we can save them as JSON file:
    const cookies = await page.cookies();
    await fs.writeFile('cookies.json', JSON.stringify(cookies));

    // then later, we load the cookies from file:
    const cookies = JSON.parse(await fs.readFile('./cookies.json'));
    await page.setCookie(...cookies);
    await page.goto("https://httpbin.dev/cookies");

    console.log(await page.content())
    browser.close();
}

run();
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.