Generate PDFs from JSON in Node.js
This quickstart turns a JSON array into PDFs with the RowScribe API: one PDF for each object in the array, made from a template you design in RowScribe. The whole program is about 65 lines of Node.js with no packages to install. It sends the rows, waits for the job and saves the result.
Last updated November 27, 2026
What you need
- Node.js 18 or later, which has fetch built in. Nothing to install.
- A template. In RowScribe, create a PDF template from the Award Certificate starter. The example rows below have its columns. To use your own template, give each row that template's columns instead.
- An API key with the
templates:readandjobs:writescopes, from Developers → API. API access is on Starter and above. The key is shown once, so copy it then.
Steps
Set your key and template
The program reads both from environment variables. The template's id is in its address in the editor (
/templates/{id}/edit/pdf), or inGET /templates.macOS / Linuxexport ROWSCRIBE_API_KEY="rsc_…" export ROWSCRIBE_TEMPLATE_ID="your template's id"Windows (PowerShell)$env:ROWSCRIBE_API_KEY = "rsc_…" $env:ROWSCRIBE_TEMPLATE_ID = "your template's id"Save the rows
Each object becomes one PDF, and each key is a column the template prints. Save this as
rows.json.rows.json[ { "OrganizationName": "Acme Inc.", "RecipientName": "John Smith", "AwardTitle": "Outstanding Customer Service", "AwardDescription": "For outstanding dedication to our customers in 2026.", "Date": "December 12, 2026", "SignerName": "Sarah Johnson", "SignerTitle": "Chief Executive Officer", "SecondSignerName": "Michael Brown", "SecondSignerTitle": "Head of People" }, { "OrganizationName": "Acme Inc.", "RecipientName": "Jane Doe", "AwardTitle": "Excellence in Teamwork", "AwardDescription": "For bringing the whole team together on every project in 2026.", "Date": "December 12, 2026", "SignerName": "Sarah Johnson", "SignerTitle": "Chief Executive Officer", "SecondSignerName": "Michael Brown", "SecondSignerTitle": "Head of People" }, { "OrganizationName": "Acme Inc.", "RecipientName": "Emily Davis", "AwardTitle": "Five Years of Service", "AwardDescription": "For five years of hard work and loyalty to Acme Inc.", "Date": "December 12, 2026", "SignerName": "Sarah Johnson", "SignerTitle": "Chief Executive Officer", "SecondSignerName": "Michael Brown", "SecondSignerTitle": "Head of People" } ]Save the program
Save it as
generate.mjs, next torows.json.generate.mjs// Generate one PDF per row of rows.json with the RowScribe API. // Needs Node.js 18 or later. Run: node generate.mjs import { readFile, writeFile } from "node:fs/promises"; import { randomUUID } from "node:crypto"; const api = "https://rowscribe.com/api/v1"; const headers = { "X-API-Key": process.env.ROWSCRIBE_API_KEY }; const templateId = process.env.ROWSCRIBE_TEMPLATE_ID; // Every error has a code and a message; stop on the first one. async function call(path, options = {}) { const response = await fetch(`${api}${path}`, { ...options, headers: { ...headers, ...options.headers } }); if (!response.ok) { const error = await response.json().catch(() => ({})); console.error(`${response.status} ${error.code ?? ""}: ${error.message ?? response.statusText}`); process.exit(1); } return response; } // 1. The template, and the columns each row needs. const template = await (await call(`/templates/${templateId}`)).json(); const rows = JSON.parse(await readFile("rows.json", "utf8")); // A row's columns are its keys, and the keys of the items in any list (an invoice's line items). const keys = (row) => Object.entries(row ?? {}).flatMap(([key, value]) => Array.isArray(value) ? [key, ...value.filter((item) => typeof item === "object").flatMap(keys)] : [key]); const columns = new Set(rows.flatMap(keys)); const missing = template.dataColumns.filter((column) => !columns.has(column)); if (missing.length > 0) { console.error(`rows.json has no ${missing.join(", ")}, which ${template.name} uses.`); process.exit(1); } // 2. One job: each row becomes one PDF. The Idempotency-Key makes a retried request safe. let response = await call("/jobs", { method: "POST", headers: { "Content-Type": "application/json", "Idempotency-Key": randomUUID() }, body: JSON.stringify({ templateId, rows }), }); let job = await response.json(); console.log(`Job ${job.id}: ${job.totalRows} rows`); // 3. Wait for it, checking as often as Retry-After says. const deadline = Date.now() + 10 * 60 * 1000; while (["pending", "scheduled", "processing"].includes(job.status)) { if (Date.now() > deadline) { console.error(`Still ${job.status} after 10 minutes; job ${job.id} carries on in RowScribe.`); process.exit(1); } const seconds = Number(response.headers.get("retry-after") ?? 5); await new Promise((resolve) => setTimeout(resolve, seconds * 1000)); response = await call(`/jobs/${job.id}`); job = await response.json(); } if (job.status !== "completed") { console.error(`Job ${job.status}: ${job.error ?? "see the job in RowScribe"}`); process.exit(1); } // 4. Download: one PDF, or a ZIP with a PDF per row. const file = await call(`/jobs/${job.id}/download`); const name = (file.headers.get("content-type") ?? "").includes("pdf") ? "output.pdf" : "output.zip"; await writeFile(name, Buffer.from(await file.arrayBuffer())); console.log(`Saved ${name}`);Run it
Terminalnode generate.mjsIt prints the job and the file it saved:
output.zip, with a PDF for each row, oroutput.pdfwhen the job's documents are merged into one. Each row counts as one document toward your plan's monthly allowance.OutputJob 8d0f6c2a-4b1e-4c57-9a3e-2f6d1b7c9e40: 3 rows Saved output.zip
How it works
GET /templates/{id}reads the columns the template needs. The program stops if none of the rows has one of them: the API refuses a job with a missing column, and this says which one before anything is sent. A column can also be a key of the items in a row's list, such as an invoice's line items in"Items": [{ "Description": …, "Qty": … }].POST /jobscreates one job for all the rows, with anIdempotency-Keyso a retried request can't create it twice.GET /jobs/{id}checks on the job as often as itsRetry-Afterheader says, until it has finished. It gives up after ten minutes; the job carries on either way.GET /jobs/{id}/downloadsaves the output.
Every error the API returns has a code and a message, and the program stops with both. The errors table lists every code.
Next steps
- Be told when it's done. In production a webhook beats checking: RowScribe posts a signed
job.completedevent to your server when the documents are ready. See Webhooks. - Email each person their PDF. Add a
deliveryto the job to send each row's PDF from your connected Gmail or Outlook, or to upload the output to your own storage. See Delivering documents. - Every endpoint is in the API reference, with a Postman collection.
- The same program in Python and C#, and all three on GitHub.