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

Use context.cookies() to retrieve cookies, save the returned array yourself (for example, as JSON), then pass the parsed cookie objects to context.setCookie(...cookies) before navigating to the site. Puppeteer provides the browser APIs for reading and setting cookies; file persistence is application code, not a built-in save/load operation.

Save and restore cookies with a BrowserContext

This example uses Puppeteer’s default browser context. It writes the current context cookies to cookies.json, then shows how a later run can read that file and restore the cookies before visiting the target. The API calls are documented in the Puppeteer cookies guide and the BrowserContext API.

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 {
  // Save cookies after the site has set them.
  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 file and restore cookies before navigation.
  const savedCookies = JSON.parse(await readFile('cookies.json', 'utf8'));
  await context.setCookie(...savedCookies);
  await page.goto('https://example.com');
} finally {
  await browser.close();
}

The example illustrates application code; it is not a claim of a tested run. In an actual two-run workflow, the save and restore sections happen in separate processes or test steps. Save after the relevant site has established its cookies, and restore before the request that needs them.

Use the context that owns the page

Cookies belong to browser storage, and BrowserContexts isolate that storage. If a page was created in a non-default context, retrieve and restore cookies through that same context. Cookies from one context are not automatically shared with another. See the BrowserContext API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const context = await browser.createBrowserContext();
const page = await context.newPage();

// After visiting the site:
const cookies = await context.cookies();

// On a later run, in the matching context:
await context.setCookie(...cookies);
await page.goto('https://example.com');

For the default context only, await browser.cookies() and await browser.setCookie(...cookies) are documented shortcuts to the corresponding context methods. Use the context methods when the page belongs to a chosen context. The shortcuts are documented at Browser.cookies() and Browser.setCookie().

What to preserve in the cookie file

Serialize the cookie objects returned by Puppeteer rather than reducing them to name/value pairs. Cookie scope and behavior can depend on fields such as domain, path, expires, httpOnly, secure, sameSite, and partitionKey. The CookieData reference documents name, value, and domain, along with optional fields. A missing expiration means the cookie is a session cookie.

The same reference identifies priority and sourceScheme as Chrome-only, and describes partition-key semantics differently for Chrome and Firefox. Do not assume every cookie can be replayed unchanged across browser engines or configurations; if a site rejects a restored cookie, preserve its relevant attributes and check the site’s and browser’s cookie requirements.

Protect saved cookies

A cookie file can contain authentication material. Keep it out of public repositories and shared logs, restrict access to it, and remove it when it is no longer needed. Treat it as a secret rather than ordinary test data.

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

Troubleshooting

  • The site still appears logged out: confirm cookies were saved after login, loaded into the same context type, and restored before navigation. Also check whether the site requires state beyond cookies, such as local storage.
  • Cookie writes fail or have no effect: inspect the saved objects for the documented required fields and relevant scope attributes. A cookie’s domain and path affect where it applies; consult the CookieData reference.
  • The file cannot be parsed: verify that it contains valid JSON representing an array of cookie objects, and that the read path points to the file written by the save step.
  • Cookies appear missing: make sure you queried the context containing the page. A newly created isolated context will not inherit another context’s cookies.
  • Restoration behaves differently in another browser engine: check engine-specific fields such as priority, sourceScheme, and partition-key behavior instead of assuming every optional property is portable.

Or skip the browser setup

If you only need a website screenshot rather than an interactive Puppeteer session with reusable browser state, ScreenshotNeo can return an image or PDF from one GET request. Its capture options can accept cookies and custom headers, but this is not a substitute for saving and restoring a Puppeteer context.

For example, capture a public page with cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for the API options. Cookie banners, popups, and chat widgets are removed before the shot; bot checks, blank pages, and failed loads are never billed; an MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for free.

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

Frequently Asked Questions

Can Puppeteer save cookies directly to a file?

No file operation is provided by the cited cookie APIs. Retrieve cookies with a context method, then persist the returned data in your application.

Should I use browser.cookies() or context.cookies()?

Use context.cookies() for the BrowserContext containing your page. browser.cookies() is a shortcut for the default context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.