Guides · Node.js · built-in fetch

Take a screenshot of a URL in Node.js with built-in fetch

The simplest request the API takes: a URL in, PNG bytes out. No full_page, no extra options — you get exactly what a visitor with a 1280×800 browser window would see above the fold. That is the right default more often than people expect: link previews, monitoring thumbnails and visual smoke tests all want the fold, not a 40,000-pixel scroll of the footer.

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: "png",
    width: 1280,
    height: 800,
  }),
});

if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("screenshot.png", Buffer.from(await res.arrayBuffer()));

The two dimensions are the viewport, not the output size: the page lays itself out as if the browser window were 1280×800, and the image comes back at exactly those pixels (multiply by device_scale if you set it). Responsive sites react to the width — ask for 390 and you get the mobile layout, no user-agent spoofing involved.

Two habits worth starting on day one. Check the HTTP status before writing bytes to disk — errors arrive as JSON, and saving {"error":"UNAUTHORIZED"} as screenshot.png is the classic first-week bug. And log the X-Cache header: identical requests within 24 hours return HIT, cost nothing, and come back in milliseconds — failed renders are never metered either way.

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

HeaderMeaning
X-CacheHIT = served from the 24h cache, free.
X-Quota-RemainingMetered renders left this month.
X-RateLimit-RemainingRequests left in the current second.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Node.js + built-in fetch