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.
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.
#1 Best Overall
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.
Rank #2
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.
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.
Rank #4
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.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCan 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.
Quick Recap
Best Value
- 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.




