# 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 [#1-install-the-nodejs-sdk]

```bash
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 [#2-prepare-a-document]

Download the [tracked-changes fixture](/fixtures/tracked-changes.docx). 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 fixture](/fixtures/tracked-changes.docx): Synthetic NDA with one tracked change · DOCX


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


> **Keep the source file (warning)**
>
> Write the first result to a separate path so you can compare the output with the original document.


## 3. Query, mutate, and save [#3-query-mutate-and-save]

Create `accept-changes.mjs`:

```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:

```bash
node accept-changes.mjs
```

## 4. Verify the output [#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.

> **Verification target (success)**
>
> The output should contain the same document content and formatting, with every tracked change accepted. The source
> file should remain unchanged.


For the contract behind each step, read the [Document API mental model](/document-api/mental-model).

Prefer Python? Follow the [Python SDK guide](/agents/automation/python-sdk) for the same deterministic workflow.
