How to screenshot a website with Node.js
Capture any web page as PNG, WebP or PDF from Node.js with a single fetch call. No Puppeteer, no Chrome to install. Full page, mobile and dark mode examples.
The usual way to screenshot a page from Node.js is Puppeteer or Playwright. They work, but they download a full Chrome, use hundreds of megabytes of memory per page and are hard to run on serverless platforms. If you only need the picture, an API call is simpler.
Basic example (Node.js 18 and later)
Node.js 18 has fetch built in, so you need no dependency at all:
import { writeFile } from "node:fs/promises";
const res = await fetch("https://getscreenapi.com/api/v1/screenshot", {
method: "POST",
headers: {
"x-api-key": process.env.SCREENAPI_KEY,
"content-type": "application/json",
},
body: JSON.stringify({ url: "https://example.com", full_page: true, format: "png" }),
});
if (!res.ok) {
const { error } = await res.json();
throw new Error(`${error.code}: ${error.message}`);
}
await writeFile("example.png", Buffer.from(await res.arrayBuffer()));Mobile screenshot
Pass a device to get the right size, pixel density and user agent:
body: JSON.stringify({ url: "https://example.com", device: "iphone-15-pro", format: "webp" })The documentation lists the 17 devices, from iphone-se to desktop-1920.
Save a page as PDF
body: JSON.stringify({ url: "https://example.com/invoice/42", format: "pdf" })
// then: await writeFile("invoice.pdf", Buffer.from(await res.arrayBuffer()));Dark mode, hide popups, wait for content
body: JSON.stringify({
url: "https://example.com",
dark_mode: true, // ask the site for its dark theme
block_chats: true, // hide Intercom, Crisp, newsletter popups...
wait_for_selector: "#app", // wait until this element exists
delay: 500, // then wait 0.5 s more for animations
})Show a screenshot in an <img> without exposing your key
Build a signed URL on your server and put it straight in the page. Anyone can load it, but no one can change the parameters or reuse your key. See signed URLs.
Errors to handle
402 INSUFFICIENT_CREDITS: monthly quota used up.429 RATE_LIMIT_EXCEEDED: wait for theRetry-Afterseconds.504 TIMEOUT: the page took too long; raisetimeout(up to 60,000 ms).
Set your own HTTP timeout to at least 60 seconds. Failed captures are not counted.
Get a key and run the example: 100 free captures a month.
Get a free key