Automation

Automate a DOCX with Node.js

Query a DOCX, accept its tracked changes, and save the result from Node.js.

Use the SDK when application code knows the operation it needs. No model is involved: the script queries, mutates, and saves deterministically.

1. Install the Node.js SDK

pnpm add @superdoc-dev/sdk

The SDK embeds the SuperDoc runtime. You do not need a separate CLI installation to use SuperDocClient.

2. Prepare a document

Download the tracked-changes fixture. Save it as contract.docx in your working directory. It contains the word termination and one tracked change. Any document meeting those two conditions also works.

Download the tracked-changes fixtureSynthetic NDA with one tracked changeDOCX

A DOCX file enters a headless SuperDoc workflow, the SDK queries and accepts its tracked changes, and a separate DOCX file is saved.

3. Query, mutate, and save

Create accept-changes.mjs:

import { SuperDocClient } from '@superdoc-dev/sdk';

/** @param {unknown} value */
function isSuccessfulReceipt(value) {
  return typeof value === 'object' && value !== null && 'success' in value && value.success === true;
}

const client = new SuperDocClient();

try {
  await client.connect();
  const doc = await client.open({ doc: './contract.docx' });

  try {
    const match = await doc.query.match({
      select: { type: 'text', pattern: 'termination' },
      require: 'first',
    });

    console.log('Matched:', match.items[0]);

    const receipt = await doc.trackChanges.decide({
      decision: 'accept',
      target: { kind: 'all' },
    });

    console.log('Mutation receipt:', receipt);
    if (!isSuccessfulReceipt(receipt)) throw new Error('Accepting tracked changes failed.');

    await doc.save({
      out: './contract.accepted.docx',
      force: true,
    });
  } finally {
    await doc.close({ discard: true });
  }
} finally {
  await client.dispose();
}

Run it with Node.js:

node accept-changes.mjs

4. Verify the output

Open contract.accepted.docx in Word or the SuperDoc editor. The tracked changes should be accepted. The original contract.docx remains unchanged.

Saving to a separate path leaves the source session dirty by design. After the output succeeds, close({ discard: true }) closes that in-memory session without overwriting the source file.

For the contract behind each step, read the Document API mental model.

Prefer Python? Follow the Python SDK guide for the same deterministic workflow.

On this page