Guides · Node.js · built-in fetch

Wait for a JavaScript-rendered form, then make it fillable in Node.js with built-in fetch

React, Vue and friends render forms after the initial HTML arrives — so a too-eager render captures a spinner where the form should be, and a pdf_forms pass finds zero controls to convert. The fix is two request parameters, no code changes on your site: "wait_until": "networkidle" holds the render until the page stops fetching, and delay adds a fixed settling pause after that.

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/apply",
    format: "pdf",
    pdf_forms: true,
    wait_until: "networkidle",
    delay: 500,
  }),
});

if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("spa-form.pdf", Buffer.from(await res.arrayBuffer()));
console.log(res.headers.get("x-form-fields"), "// 0 here usually means you rendered too early");

wait_until accepts four stages, in order of patience: commit (bytes started arriving), domcontentloaded, load (the default), and networkidle (no network requests for a quiet period — the right choice for SPAs that hydrate after load). delay is an extra wait in milliseconds after that stage is reached: use a few hundred ms for forms that animate in, or that fetch their field list from an API after mount.

The feedback loop is X-Form-Fields: if it comes back 0 on a page you know has a form, the form was not in the DOM yet (or it is inside an iframe, which is never converted). Bump delay, or check the form is rendered server-side. Raise timeout (navigation cap, ms) for genuinely slow apps rather than looping retries — a failed render is never charged, but it is slower than one patient render.

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-Form-Fields0 on a form page = rendered before the form existed.
X-Demo-Duration-MsHow long the render took (demo route).
X-CacheCached responses replay instantly.

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