Guides · Node.js · built-in fetch
Take a JPEG screenshot at a custom viewport size in Node.js with built-in fetch
Two independent knobs, one request: format picks the codec ("jpeg" here — much smaller files for photographic content), and width/height set the viewport the page believes it is being viewed in. 390×844 is an iPhone-class viewport, so responsive pages serve their mobile layout — media queries fire on viewport width, no user-agent tricks needed.
Below is a complete, runnable Node.js program using built-in fetch, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.
import { writeFile } from "node:fs/promises";
const res = await fetch("https://snapdok.io/v1/render", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SNAPDOK_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
url: "https://your-app.com/report/42",
format: "jpeg",
width: 390,
height: 844,
}),
});
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("mobile.jpg", Buffer.from(await res.arrayBuffer()));
When to pick which format: JPEG compresses gradients and photos far better and has no alpha channel; PNG is lossless and wins on flat UI, text and sharp edges. For mobile-layout QA screenshots at a fixed viewport, JPEG cuts transfer size dramatically with no visible cost. If you need the image to sit on a transparent background, that is PNG territory.
height here is the viewport height — what "above the fold" means for this render. Without full_page you get exactly that viewport; combine full_page: true with a width and the height is measured from the document instead, at which point the viewport height only affects lazy-load behaviour. All three parameters change the cache fingerprint, so a 390-wide render and a 1280-wide render are separate cache entries — as they should be.
Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.
Notes for built-in fetch
No dependency needed: fetch is global from Node 18 onward (snapdok's own test suite uses it). The binary-safety detail is res.arrayBuffer() → Buffer.from(...) — reaching for res.text() is the classic way to corrupt a PDF in Node. fetch does not reject on HTTP error statuses, so the res.ok check is load-bearing: without it, an expired key writes a 40-byte JSON error to disk with a .pdf extension.
Response headers worth reading
| Header | Meaning |
|---|---|
Content-Type | image/jpeg for this request. |
X-Cache | Each distinct width/height/format is its own cache entry. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Take a JPEG screenshot at a custom viewport size in Python with requests
- Take a JPEG screenshot at a custom viewport size in Python with httpx
- Take a JPEG screenshot at a custom viewport size in Node.js with axios
- Take a JPEG screenshot at a custom viewport size in PHP with cURL extension
- Take a JPEG screenshot at a custom viewport size in Ruby with Net::HTTP
More with Node.js + built-in fetch
- Convert an HTML form to a fillable PDF
- Get a fillable PDF of just the form — no nav, no footer
- Generate a fillable invoice PDF from a web page
- Turn an online registration form into a fillable PDF
- Wait for a JavaScript-rendered form, then make it fillable
- Verify a fillable PDF render without opening the file
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a retina-quality (2x) screenshot
- Use the 24-hour render cache to cut costs
- Take a screenshot of a URL
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing
- Add page numbers and headers to a PDF