Guides · Node.js · built-in fetch

Verify a fillable PDF render without opening the file in Node.js with built-in fetch

When form-PDF generation is part of a pipeline — nightly exports, CI checks, a queue worker — "did it actually come out fillable?" needs a machine answer, not a human opening Acrobat. snapdok puts the verification data in the response headers, so your code can assert on the render it just received.

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/intake-form",
    format: "pdf",
    pdf_forms: true,
    pdf_form_only: true,
  }),
});

if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
await writeFile("checked.pdf", Buffer.from(await res.arrayBuffer()));
console.log(res.headers.get("x-form-fields"), "// assert on this in CI");

Three headers carry the contract. X-Form-Fields is the number of AcroForm fields placed — assert it equals the number of controls your form has, and you have a regression test that catches a broken deploy of your own form page, too. X-Form-Skipped counts controls that could not convert (file pickers, hidden inputs, iframe forms); a sudden rise means someone changed the form. X-Form-Extracted tells you whether pdf_form_only actually pruned the page or fell back to the full page — fallback is legitimate output, but your layout expectations may differ.

Two operational notes for pipelines: failed renders are never metered, and a repeat of an identical request inside 24 hours returns X-Cache: HIT and is also not charged — so a retrying job that occasionally double-fires does not eat quota. Rate limits surface as HTTP 429 with a Retry-After header; honour it instead of hammering.

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-FieldsFields placed — the number to assert on.
X-Form-SkippedUnconvertible controls; watch for regressions.
X-Form-ExtractedPrune result when pdf_form_only was requested.
Retry-AfterPresent on 429 — seconds to back off.

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