
How to take a website screenshot in Node.js
Install one package, set one environment variable, and a web page becomes a PNG, WebP or PDF in a few lines of JavaScript or TypeScript. No headless browser to install, update or keep alive on your server.
· 8 min read
Two ways to do it
There are two ways to screenshot a web page from Node.js. You can drive a browser yourself, with Puppeteer or Playwright, which means a browser download in every deploy, memory to run it, and your own answers to cookie banners, lazy-loaded images and pages that never finish loading. Or you can ask an API that already runs the browsers, and get the file back.
This post is the second way, with the official @websitescreenshotworld/sdk package for the Instant Snap API. It is open source under the MIT licence, written in TypeScript with types for every option and answer, works from ES modules and CommonJS, and has no runtime dependencies. It needs Node.js 22 or later.
The first capture
Create an Instant Snap API key in the app under Developer Tools › API Keys, then install the package and put the key in the environment:
npm install @websitescreenshotworld/sdk
export WSW_INSTANT_SNAP_API_KEY="wsw_api_..."The client reads the key from that variable, so the program does not contain it. Create the client once and reuse it:
import { WebsiteScreenshotWorld } from "@websitescreenshotworld/sdk";
// Reads WSW_INSTANT_SNAP_API_KEY from the environment.
const client = new WebsiteScreenshotWorld();
const capture = await client.instantSnap.capture({
url: "https://example.com",
format: "webp",
viewport: { width: 1440, height: 900 },
});
console.log(capture.id, capture.width, capture.height);
console.log(capture.downloadUrl); // valid for 60 minutesThe answer is the capture: its id, its size in pixels and a signed download link. The link is valid for 60 minutes and the file is kept for 24 hours. An option you leave out takes the API's default, and client.instantSnap.defaults() lists them: a 1920 by 1080 window to begin with.
To check a key without spending a capture, call client.instantSnap.account(). It answers with the name of the key and the email of the account it belongs to.
Getting the file
When you want the image on disk and nothing else, captureFile returns the file in the answer itself, as a stream, along with its details:
const file = await client.instantSnap.captureFile({
url: "https://example.com",
format: "png",
});
console.log(file.filename, file.contentType, file.sha256);
await file.saveTo("./example.png");file.body is a web ReadableStream you can pipe anywhere instead, for example into an HTTP response, and file.arrayBuffer() reads the whole file into memory. The stream can be read once. A file returned this way is not kept by the API, so save or read it before you drop the result.
To fetch a stored capture later, read it back by its id and download it:
const capture = await client.instantSnap.get(
"3f6c2a1e-9b8d-4c7e-a5f4-1d2e3c4b5a69",
);
await client.instantSnap.download(capture, "./example.png");download writes to a path, a Node.js stream or a web WritableStream. There is also captureToBucket, which uploads the capture to your own S3-compatible bucket.
Full page, phone, PDF
Every option of the API is available in camelCase: mediaCategory, scrollMode, blockAds, timestampWatermark and the rest. Nested blocks such as viewport and selectors are plain objects, typed so your editor knows their fields.
// The whole page, top to bottom
await client.instantSnap.capture({
url: "https://example.com",
scrollMode: "scroll",
});
// As a phone
await client.instantSnap.capture({
url: "https://example.com",
deviceCategory: "mobile",
emulatedDevice: "iPhone 13",
});
// A PDF of the whole page
await client.instantSnap.capture({
url: "https://example.com",
mediaCategory: "pdf",
format: "image_pdf",
scrollMode: "scroll",
});
// Without the sticky header and the chat bubble
await client.instantSnap.capture({
url: "https://example.com",
selectors: { hide: "header.sticky, #chat-widget" },
});- Full page.
scrollModeisviewportby default, which captures what fits on screen.scrollwalks the whole page. Our full-page guide covers the pages that fight back. - Phone. A named device decides the viewport, the pixel density and the touch behaviour, so a
viewportsent with it is ignored.client.instantSnap.catalog()lists the devices you can name. - PDF. For a scrolling capture use
image_pdf, whose pages are images merged into one document.document_pdfwithscrollModeset toscrollis refused. - Hiding elements.
selectors.hidetakes up to 25 comma-separated CSS selectors. One the browser rejects is skipped; it does not fail the capture.
The request parameters page lists every option with its default and its limits.
Errors and retries
Every error is a WebsiteScreenshotWorldError with an errorCode, a message and the httpStatus. Each kind of refusal has its own subclass. Decide what to do from the class or the code, never from the message:
import {
CaptureError,
ErrorCode,
PlanLimitError,
WebsiteScreenshotWorldError,
} from "@websitescreenshotworld/sdk";
try {
await client.instantSnap.capture({ url: "https://example.com" });
} catch (error) {
if (
error instanceof CaptureError &&
error.errorCode === ErrorCode.CaptureTimeout
) {
// The page took too long to load: a longer pageTimeoutMs may help.
} else if (error instanceof PlanLimitError) {
// Out of captures, or the plan does not include an option that was asked for.
} else if (error instanceof WebsiteScreenshotWorldError) {
console.error(error.errorCode, error.message);
} else {
throw error;
}
}- What is retried for you. A rate limit, a busy service and a request that got no answer are retried twice. The wait is the one the API asked for, up to a minute; otherwise 0.5 seconds and then 1 second. Set
maxRetriesto change the count. - What is not. A failed or timed-out capture is an answer about the website, and it is not retried.
- Your own idempotency key. Every capture already carries one, reused on its retries. Pass
idempotencyKeyas the second argument when a job queue may run the same task twice: the capture is then safe to repeat across calls and processes for 24 hours.
The post on rate limits and retries explains which refusals clear by themselves and which never will.
Timeouts and cancelling
A capture call times out after 180 seconds and any other call after 30, retries included. Change them for the client or for one call, and pass an AbortSignal to cancel, for example when the visitor who asked for the capture has gone away:
const quick = new WebsiteScreenshotWorld({ captureTimeout: 60_000 });
const controller = new AbortController();
const pending = quick.instantSnap.capture(
{ url: "https://example.com" },
{ timeout: 90_000, signal: controller.signal },
);
controller.abort(); // pending rejects with the signal's reasonA timeout raises ConnectionError. Cancelling stops the call and any retry it would have made at once.
Many pages: the queue
A workspace has at most two captures rendering at once, so a Promise.all over fifty addresses does not make them finish sooner. For a batch, queue the captures instead: submit answers at once with the capture in status queued, and wait reads it every few seconds until it has finished.
import { CaptureStatus } from "@websitescreenshotworld/sdk";
const queued = await client.instantSnap.submit({
url: "https://example.com",
format: "png",
});
const capture = await client.instantSnap.wait(queued.id);
if (capture.status === CaptureStatus.Succeeded) {
await client.instantSnap.download(capture, "./example.png");
}wait gives up after 360 seconds by default and returns the capture as last read, still unfinished; it does not throw, so you can wait again. A capture still queued 15 minutes after it was accepted ends as failed with capture_expired, and is not charged. Instead of waiting, you can set callbackUrl and receive a webhook when the capture succeeds or fails; the package's Webhook.verify checks its signature, and the post on verifying a webhook signature explains what that check does.
Without the library
You do not need the package. The API is plain HTTPS and JSON, with the key in an X-Api-Key header, and Node.js has fetch built in:
const response = await fetch(
"https://api.websitescreenshotworld.com/instant-snap/v1/captures",
{
method: "POST",
headers: {
"X-Api-Key": process.env.WSW_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({ url: "https://example.com" }),
},
);
const capture = await response.json();
console.log(capture.download_url);What you give up is the retry, timeout and idempotency code, which you would then write yourself. If Python is the other half of your stack, the Python version of this post uses the same calls under their Python names.
FAQ
Do I need Puppeteer, Playwright or Chrome installed?
No. The page is loaded in a real browser on our side and your code makes one HTTPS request. The library has no runtime dependencies.
Which Node.js versions does the library support?
Node.js 22 and later, from ES modules or CommonJS. It is written in TypeScript, and its type definitions need TypeScript 5.0 or later.
How long is a capture kept?
A capture made with capture is kept for 24 hours. Its download link is valid for 60 minutes, and reading the capture again by its id gives a fresh link. A file returned by captureFile is not kept at all.
Can a retry charge me twice?
No. Every capture the library sends carries an idempotency key, reused on its retries, so a retried capture is never run twice.
Can I try it without paying?
Yes. The free trial lasts one month, needs no card and includes 100 captures. Its API rate limit is 10 requests a minute. Request headers, cookies and choosing the country a capture comes from are not part of the trial.