Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Save and Load Cookies in Puppeteer

Persist Puppeteer cookies between runs by saving context.cookies() output as JSON and restoring it with context.setCookie(...cookies) before navigation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Puppeteer’s BrowserContext.cookies() to read cookies and BrowserContext.setCookie(...cookies) to restore them. To carry cookies across separate runs, your application must save the returned cookie objects somewhere—such as a JSON file—and load them before navigating to the site. Puppeteer provides the browser APIs, not a built-in cookie-file save/load operation.

Save and restore cookies with a BrowserContext

A browser context owns its own storage, including cookies. Use the same context for the page and the cookie operations; cookies from one context are not shared with another. The example below uses Puppeteer’s default browser context and Node.js file APIs to persist cookies in JSON.

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';
import { readFile, writeFile } from 'node:fs/promises';

const browser = await puppeteer.launch();
const context = browser.defaultBrowserContext();
const page = await context.newPage();

try {
  // Visit the site and let it establish the cookies you need.
  await page.goto('https://example.com');
  const cookies = await context.cookies();
  await writeFile('cookies.json', JSON.stringify(cookies, null, 2), 'utf8');

  // In a later run, load the saved cookies before the relevant navigation.
  const savedCookies = JSON.parse(await readFile('cookies.json', 'utf8'));
  await context.setCookie(...savedCookies);
  await page.goto('https://example.com');
} finally {
  await browser.close();
}

This is application code for serialization and file handling, not a Puppeteer-provided persistence feature. The Puppeteer API calls shown are documented in the Cookies guide and BrowserContext API.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the cookie API for the context you use

For a specific BrowserContext

Call await context.cookies() to retrieve cookies in that context, then call await context.setCookie(...cookies) to set cookie objects there. This is the appropriate choice when the page was created from a non-default context.

For the default context

await browser.cookies() and await browser.setCookie(...cookies) are shortcuts for the corresponding default-context operations. Use these only when the page belongs to the default context. The API references describe these shortcuts at Browser.cookies() and Browser.setCookie().

Preserve scope and cookie attributes

A cookie object includes required name, value, and domain fields. Other documented fields include expires, httpOnly, partitionKey, path, priority, sameSite, secure, and sourceScheme. Keep the returned attributes when serializing rather than reducing cookies to name/value pairs: scope and behavior matter to whether a cookie applies. An absent expiration represents a session cookie. See the CookieData reference.

Optional fields are not guaranteed to behave identically across browser engines. The API reference identifies priority and sourceScheme as Chrome-only and documents different partition-key semantics for Chrome and Firefox. The documentation demonstrates setting cookie objects, but does not establish that every cookie from every site can be replayed unchanged in every configuration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Common problems and fixes

  • The target request is unauthenticated: restore cookies before the navigation that needs them. Confirm the cookies were saved after the site established them, and that you are restoring them into the page’s context.
  • Cookies appear missing: check which context created the page. A cookie read from the default context will not represent cookies held by a separate context.
  • Setting cookies fails or the site ignores them: inspect the cookie’s domain, path, expiration, secure and SameSite attributes, and any partition key. Preserve relevant returned fields and investigate browser- or site-specific rejection rather than assuming all cookies are portable.
  • The JSON file cannot be read: ensure the path is correct relative to the Node.js process working directory, and that the file contains a JSON array of cookie objects. Handle a missing or malformed file in your application if first-run behavior should continue without saved cookies.

Protect saved cookie files

Cookie files can contain authentication material. Treat them as secrets: do not commit them to a public repository, restrict access, and remove them when no longer needed. The Puppeteer cookie APIs expose browser storage data; this handling advice is a practical security precaution.

Or skip the browser setup

If your goal is a screenshot rather than carrying browser state into a Puppeteer workflow, ScreenshotNeo offers a screenshot API and MCP server. A single GET request can return an image or PDF; consult the API documentation for options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
  • Cookie and consent banners, newsletter popups, and chat widgets are handled before capture.
  • Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed.
  • An MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients.
  • The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.

Sign up for 1,000 free screenshots a month—no card required.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Which Puppeteer documentation version does this API guidance reflect?

The cited API pages displayed Puppeteer 25.11.0 or 25.12.0, while the Cookies guide is the current next documentation. Check the API reference for changes if using a substantially newer release.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can I use this approach to restore cookies into a different browser context?

You can set cookie objects in a context with its `setCookie()` method, but contexts isolate storage, and the documentation does not guarantee universal replay across browser configurations.

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.