Build

TypeScript SDK guide

Install the TypeScript SDK, compress a PDF in three lines, then run tools and pipelines, handle errors and verify webhooks.

Install

TypeScript package wellfriend-document-sdk, for Node.js 20 or later. Import it with import { Wellfriend } from "wellfriend-document-sdk"; What the registry serves now: 1.0.0 on npm.

Install the TypeScript SDK
npm install wellfriend-document-sdk@latest

Quickstart

Create a key in the dashboard (/dashboard/developer-api/keys) and set it as WELLFRIEND_API_KEY on your server; the client reads it, and https://wellfriend.dev/api/v2 is the default address.

TypeScript: compress a PDF to a file
import { Wellfriend } from "wellfriend-document-sdk";
const wf = new Wellfriend(); // reads WELLFRIEND_API_KEY
await wf.tools.compressPdf("input.pdf").then((result) => result.save("output.pdf"));
The same job as a complete program
// wellfriend-document-sdk: npm install wellfriend-document-sdk@latest. An ES module (top-level await): save it as .mts.
// Your API key: set WELLFRIEND_API_KEY, or replace YOUR_API_KEY (keys: https://wellfriend.dev/dashboard/developer-api/keys).
import { Wellfriend } from "wellfriend-document-sdk";

const wf = new Wellfriend({ apiKey: process.env.WELLFRIEND_API_KEY ?? "YOUR_API_KEY" });
const result = await wf.tools.compressPdf("input.pdf");
await result.save("input-wellfriend.pdf");

Tools

Every tool is one method, wf.tools.<tool>(input, params, options): it uploads the input (a path, bytes, a stream or a stored document; a list for merge), runs the tool, waits and returns the result. The result saves its first output with save(path), or every output into a folder; it also gives the bytes, a stream, the outputs, the operation and the usage. tools.run(toolId, ...) runs any tool by id, wait: false returns the started operation to wait on later, and quote(...) prices a run before you send it.

Settings, any tool by id, and not waiting
const result = await wf.tools.compressPdf("input.pdf", { level: "strong" });
console.log(result.outputs, result.usage);
await wf.tools.run("merge-pdf", ["a.pdf", "b.pdf"]).then((merged) => merged.save("merged.pdf"));
const operation = await wf.tools.ocrPdf("scan.pdf", {}, { wait: false }); // resume later with operation.wait()

Pipelines

A pipeline runs up to ten tools on one file in one run; each step reads the previous step's output on the server. workflows.define(name, steps) keeps a named pipeline you run again like a tool. The SDK reuses one saved pipeline for the same steps instead of creating one per run.

Compress, then OCR
const result = await wf.pipeline([{ tool: "compress-pdf" }, { tool: "ocr-pdf" }]).run("input.pdf");
await result.save("output.pdf");
const tidy = await wf.workflows.define("tidy-scans", [{ tool: "compress-pdf" }, { tool: "ocr-pdf" }]);
await tidy.run("next.pdf").then((done) => done.save("next-out.pdf"));

Errors

Every SDK raises the same errors. A message ends with the reference to quote to Support. Network errors, 408, 429 and 5xx answers are retried twice with backoff (honouring Retry-After) for reads and for writes the SDK sends with an idempotency key; set maxRetries and timeout on the client or per call.

The error types
WellfriendError
├─ APIError (status, code, message, referenceId, details)
│  ├─ BadRequestError (400) / ValidationError (422)
│  ├─ AuthenticationError (401) / PermissionDeniedError (403)
│  ├─ NotFoundError (404) / ConflictError (409)
│  ├─ RateLimitError (429, retryAfter)
│  └─ ServerError (5xx)
├─ ConnectionError
├─ TimeoutError
├─ OperationFailedError (code, message, operation)
└─ WaitTimeoutError (operation id, to resume)
Handle the common cases
import { ValidationError, RateLimitError, OperationFailedError } from "wellfriend-document-sdk";
try {
  await wf.tools.compressPdf("input.pdf", { level: "extreme" });
} catch (error) {
  if (error instanceof ValidationError) console.error(error.message, error.details);
  else if (error instanceof RateLimitError) console.error(`Try again in ${error.retryAfter} s`);
  else if (error instanceof OperationFailedError) console.error(error.code, error.operation.id);
  else throw error;
}

Webhooks

webhooks.verify(payload, headers, secret), also available on its own as verifyWebhook, checks the signature over the raw body with a five-minute tolerance and returns the typed event; a bad signature raises WebhookVerificationError. Pass a list of secrets while you rotate one. See /docs/webhooks/signatures.

Verify a delivery
// Use the raw request body, exactly as received.
const secret = process.env.WELLFRIEND_WEBHOOK_SECRET;
if (!secret) throw new Error("Set WELLFRIEND_WEBHOOK_SECRET.");
const event = wf.webhooks.verify(rawBody, request.headers, secret);
console.log(event.type, event.data);

Every other endpoint

Every API operation is a method named after it: documentVersions.download, webhooks.endpoints.create, and so on. List methods return one page and can also go through every page. The API reference (/docs/developer-api/snippets) shows each one in this language.

Documents and webhooks
await wf.documents.list({ limit: 20 });
await wf.documentVersions.download("<documentId>", "<versionId>").then((file) => file.save("output.pdf"));
await wf.webhooks.endpoints.create({ url: "https://example.com/hooks", eventFilters: ["com.wellfriend.document.*"] });