Node.js SDK
Use the official Pritset Node.js SDK to manage DOCX templates and generate PDFs from your Node.js application.
View the official Node.js SDK repository on GitHub
Requirementsโ
- Node.js 18 or newer
- A Pritset access token and secret from the Profile page
Node 22 and 24 are fully tested. Node 18 and 20 compatibility is best effort because they are outside current upstream support.
Compatibilityโ
This page documents SDK version 0.1.5, which implements Pritset SDK contract 1.0.0. Version 0.1.5 is a preview release, so public method names may be refined before 1.0.0.
Install the packageโ
npm install @pritset/sdk
pnpm add @pritset/sdk
yarn add @pritset/sdk
Create a clientโ
Keep credentials in server-side environment variables rather than source code. Pritset sends the access token directly in the Authorization header and the secret in X-Secret; do not add a Bearer prefix. Never expose either value to browser code.
import { PritsetClient } from "@pritset/sdk";
const client = new PritsetClient({
accessToken: process.env.PRITSET_ACCESS_TOKEN!,
secret: process.env.PRITSET_SECRET!,
});
Generate a PDFโ
Upload a DOCX template first and copy its ID. See Template management for upload requirements and the portal workflow.
const pdf = await client.documents.generate("YOUR_TEMPLATE_ID", {
invoice: { number: "INV-1042", customer: "Ada Lovelace" },
});
console.log(pdf.contentType); // application/pdf
await pdf.saveToFile("invoice.pdf");
Binary responses are stream-first. saveToFile() and toBuffer() both consume the stream, so call only one of them. The data object must match the placeholders and loops in your DOCX file; see Templating syntax.
Use an AbortSignal to cancel a request:
const controller = new AbortController();
const pdf = await client.documents.generate(
"YOUR_TEMPLATE_ID",
{ title: "Cancelable report" },
{ signal: controller.signal },
);
Manage templatesโ
Use client.templates to list, retrieve, create, update, download, validate, and delete templates.
const page = await client.templates.list({
search: "invoice",
page: 1,
pageSize: 25,
sort: { sortBy: "Name", sortDirection: 0 }, // 0 ascending, 1 descending
});
for (const template of page.data) {
console.log(template.id, template.name);
}
const created = await client.templates.create({
name: "Monthly invoice",
tags: "invoice,monthly",
template: { data: "./invoice.docx" },
});
Retrieve the complete template record, including file metadata, then update its name and tags. The template input is optional on update; provide one to replace the DOCX file.
const details = await client.templates.get(created.id);
console.log(details.fileInfo.objectName, details.fileInfo.size);
await client.templates.update(created.id, {
name: "Monthly invoice 2026",
tags: "invoice,monthly,2026",
template: { data: "./invoice-2026.docx" }, // optional replacement file
});
Paths can be used directly for uploads. Buffers and streams require an explicit filename:
const created = await client.templates.create({
name: "Monthly invoice",
template: {
data: docxBuffer,
filename: "invoice.docx",
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
});
const download = await client.templates.download(created.id);
await download.saveToFile("downloaded-invoice.docx");
const valid = await client.templates.validate({
file: { data: "./invoice.docx" },
data: { invoice: { number: "INV-1042" } },
});
console.log("Template valid:", valid);
await client.templates.delete(created.id);
For endpoint behavior and limits, see the Template management API reference.
Raw JSONโ
Pass a JSON string when exact serialization matters.
await client.documents.generate("YOUR_TEMPLATE_ID", '{"amount":"10.00"}');
Generate through a webhookโ
Use generateWebhook when Pritset should send the generation result to your endpoint. The webhook URL must be an absolute HTTP(S) URL without embedded credentials.
const job = await client.documents.generateWebhook(
"YOUR_TEMPLATE_ID",
{ invoice: { number: "INV-1042" } },
"https://example.com/webhooks/pritset",
);
console.log(job.id);
Pritset posts the generated PDF to the supplied URL and includes the job ID as an id query parameter. The SDK does not run or verify your webhook receiver. See Direct and webhook processing for the API workflow.
Handle errorsโ
The SDK throws PritsetApiError for API responses with an error status and PritsetTransportError when a request does not complete. It does not retry automatically.
import { PritsetApiError, PritsetTransportError } from "@pritset/sdk";
try {
await client.templates.get("YOUR_TEMPLATE_ID");
} catch (error) {
if (error instanceof PritsetApiError) {
console.error(error.status, error.fieldErrors, error.trace, error.retryAfter);
} else if (error instanceof PritsetTransportError) {
console.error("The request did not complete.");
} else {
throw error;
}
}
See API errors for status codes and retry guidance.
Local development and configurationโ
The SDK uses https://api.pritset.com by default and disables automatic redirects so credentials cannot be forwarded to another origin. Set a custom timeout in milliseconds when creating the client:
const client = new PritsetClient({
accessToken: process.env.PRITSET_ACCESS_TOKEN!,
secret: process.env.PRITSET_SECRET!,
timeoutMs: 120_000,
});
HTTPS is required except for an explicit local test server:
const local = new PritsetClient({
accessToken: "test-token",
secret: "test-secret",
baseUrl: "http://127.0.0.1:5000",
allowInsecureHttp: true,
});
CommonJSโ
const { PritsetClient } = require("@pritset/sdk");