Many developers need to combine multiple Word documents in Node.JS to generate consolidated reports or packages. GroupDocs.Merger Cloud SDK for Node.js provides a powerful API that handles Word merging on the server side without complex file‑handling code. In this guide you will see a complete implementation, equivalent cURL calls, installation steps, and best‑practice tips to get your documents merged quickly.

Combine Multiple Word Documents in Node.JS - Complete Code Example

This example demonstrates how to merge three DOCX files into a single document using the GroupDocs.Merger Cloud SDK for Node.js.

const GroupDocsMergerCloud = require("@groupdocs/merger-cloud");

// ==== Configuration ====
// Replace with your actual credentials
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";

const config = new GroupDocsMergerCloud.Configuration(clientId, clientSecret);
const mergeApi = new GroupDocsMergerCloud.MergeApi(config);

// ==== Input / Output paths ====
const inputFiles = [
    "documents/input1.docx",
    "documents/input2.docx",
    "documents/input3.docx"
];
const outputFile = "documents/combined_output.docx";

// ==== Build request ====
const documentEntries = inputFiles.map(path => {
    return new GroupDocsMergerCloud.Models.DocumentEntry({
        fileInfo: new GroupDocsMergerCloud.Models.FileInfo({
            filePath: path
        })
    });
});

const mergeRequest = new GroupDocsMergerCloud.Models.MergeDocumentsRequest({
    documents: documentEntries,
    outputPath: outputFile
});

// ==== Execute merge ====
(async () => {
    try {
        const result = await mergeApi.mergeDocuments(mergeRequest);
        console.log(`Merge completed successfully. Output file: ${result.path}`);
    } catch (error) {
        console.error("Error during merging:", error);
    }
})();

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (input1.docx, combined_output.docx, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Merging Word Documents via REST API Using cURL

If you prefer a pure REST approach, the same merge operation can be performed with cURL commands. Below is a concise workflow that authenticates, uploads source files, runs the merge, and downloads the result.

1. Authenticate and Get Access Token

curl -X POST "https://api.groupdocs.cloud/v2.0/merger/authenticate" \
-H "Content-Type: application/json" \
-d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'

The response contains an access_token used in subsequent calls.

2. Upload the Source Files

Repeat this step for each DOCX file you want to merge.

curl -X POST "https://api.groupdocs.cloud/v2.0/storage/file/upload?path=documents/input1.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@path/to/input1.docx"

3. Execute the Merge Operation

curl -X POST "https://api.groupdocs.cloud/v2.0/merger/merge" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
    "documents": [
        {"fileInfo": {"filePath": "documents/input1.docx"}},
        {"fileInfo": {"filePath": "documents/input2.docx"}},
        {"fileInfo": {"filePath": "documents/input3.docx"}}
    ],
    "outputPath": "documents/combined_output.docx"
}'

The API returns the path of the merged file.

4. Download the Merged Document

curl -X GET "https://api.groupdocs.cloud/v2.0/storage/file/download?path=documents/combined_output.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o combined_output.docx

For more details on request payloads and options, see the official API documentation.

How Merge Multiple Word Documents in Node.JS Works

The SDK abstracts the merge process into a few logical steps:

  1. Configuration Creation - new GroupDocsMergerCloud.Configuration(clientId, clientSecret) builds the authentication context. See the class reference in the API Reference.

    const config = new GroupDocsMergerCloud.Configuration(clientId, clientSecret);
    
  2. API Instance - new GroupDocsMergerCloud.MergeApi(config) provides access to merge operations.

    const mergeApi = new GroupDocsMergerCloud.MergeApi(config);
    
  3. Document Entries - Each source file is wrapped in a DocumentEntry that contains a FileInfo with the file path.

    const documentEntries = inputFiles.map(path => {
        return new GroupDocsMergerCloud.Models.DocumentEntry({
            fileInfo: new GroupDocsMergerCloud.Models.FileInfo({ filePath: path })
        });
    });
    
  4. Merge Request - MergeDocumentsRequest bundles the entries and the desired output path.

    const mergeRequest = new GroupDocsMergerCloud.Models.MergeDocumentsRequest({
        documents: documentEntries,
        outputPath: outputFile
    });
    
  5. Execution - mergeApi.mergeDocuments(mergeRequest) sends the request to the cloud service. The promise resolves with a result object containing the output file path.

    const result = await mergeApi.mergeDocuments(mergeRequest);
    console.log(`Merge completed successfully. Output file: ${result.path}`);
    

These steps together enable you to combine multiple Word documents efficiently without handling low‑level file streams.

Getting the Environment Ready for GroupDocs.Merger

  1. Install the SDK

    npm install groupdocs-merger-cloud
    

    The package is available from the official download page.

  2. Node.js Version - Ensure you are running Node.js 12 or later.

  3. Configure Credentials - Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET in the code with the values from your GroupDocs Cloud account.

  4. Create a Project Folder - Place the source DOCX files in a documents/ subfolder as shown in the example.

With these steps completed, you are ready to run the merge script or invoke the REST endpoints.

Conclusion

Combining multiple Word documents in Node.JS becomes straightforward with the GroupDocs.Merger Cloud SDK for Node.js. The SDK handles file uploads, merging logic, and output generation, allowing you to focus on business logic rather than low‑level file manipulation. Remember to secure a proper license for production use; you can start with a temporary license and upgrade to a full subscription as your needs grow. Start merging today and streamline your document workflows.

FAQs

  • How do I combine multiple Word documents in Node.JS using the SDK?
    Use the MergeApi class to create a MergeDocumentsRequest that lists each DOCX file as a DocumentEntry. The SDK merges them in the order provided and saves the result to the specified output path.

  • Is it possible to merge PDFs together with the same SDK?
    Yes, the Merger SDK supports PDF, DOCX, ODT, and several other formats. Simply change the file extensions in the inputFiles array and the SDK will handle the conversion automatically. See the official documentation for format details.

  • What are the limits on the number of documents I can merge in a single request?
    The cloud service imposes a size limit rather than a strict count. As long as the combined size stays within your account’s quota, you can merge dozens of files. For very large batches, consider uploading files to GroupDocs storage first and referencing them by ID.

  • Do I need to worry about licensing for development versus production?
    Development and testing can use a temporary license obtained from the temporary license page. For production deployments, purchase a full license to unlock unlimited merges and priority support.

Read More