Guides · Node.js · built-in fetch

Use the 24-hour render cache to cut costs in Node.js with built-in fetch

Every response carries an X-Cache header, and it is worth wiring into your logs on day one: HIT means the bytes came from the 24-hour cache — served in milliseconds and not counted against your quota. MISS means a real browser rendered the page and one render was metered.

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,
  }),
});

if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("shot.png", Buffer.from(await res.arrayBuffer()));
console.log(res.headers.get("x-cache"), "// HIT = free, MISS = rendered and metered");

The cache key is the normalised request body: URL plus every rendering parameter. Same URL at a different width, format or scale is a different entry; alias spellings (device_scale_factor vs device_scale) are folded together first, so they share an entry. Failed renders are never cached and never metered.

Two design consequences. First, idempotent retries are safe: a queue worker that re-fires the same job inside a day costs nothing extra. Second, if you need a fresh render of a page that just changed under the same URL, vary the request — the pragmatic trick is a throwaway query parameter on the target URL (?v=deploy-id), which changes the fingerprint and forces a MISS. Rate limiting still applies to HITs (it protects the endpoint, not the renderer), so keep an eye on X-RateLimit-Remaining in tight loops.

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 or MISS.
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

In the wild