In this tutorial, we will learn how to generate Word or PDF documents using the Documentero API service in JavaScript/Node. Documentero is an API service that allows you to generate documents based on templates and data provided.

Prerequisites

Before we begin, make sure you have the following:

  • Node.js installed on your machine
  • Documentero API access and your API secret key

Step 1: Setting up the Project

Let’s start by setting up a new Node.js project. Open your terminal and follow these steps:

  1. Create a new project directory:

    1
    2
    mkdir documentero-demo
    cd documentero-demo
  2. Initialize a new Node.js project:

    1
    npm init -y
  3. Install the required dependencies:

    1
    npm install axios

Step 2: Making a POST Request to Documentero API

Next, we’ll write the code to make a POST request to the Documentero API service and generate a document.

Create a new JavaScript file, e.g., generateDocument.js, and open it in your preferred text editor.

First, we need to import the required module and define the API endpoint:

1
2
const axios = require('axios');
const documenteroEndpoint = 'https://app.documentero.com/api';

Now, we’ll define a function to generate the document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
async function generateDocument() {
const requestBody = {
document: 'Pt04DVNbifqY4inserI2', // Replace with your template identifier
apiKey: 'YOUR_API_KEY', // Replace with your API secret key
format: 'docx', // Set the desired format: 'docx' for Word or 'pdf' for PDF
data: {
field: 'fieldvalue', // Replace with the data you want to pass to the document generation
},
};

try {
const response = await axios.post(documenteroEndpoint, requestBody, {
headers: {
'Content-Type': 'application/json',
},
});

const { status, message, data } = response.data;

if (status === 200) {
console.log('Document Generated Successfully');
console.log('Download Link:', data);
} else {
console.error('Document Generation Failed:', message);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
}

generateDocument();

Make sure to replace 'Pt04DVNbifqY4inserI2' with the actual template identifier provided by Documentero, and 'YOUR_API_KEY' with your API secret key obtained from Documentero.

Step 3: Running the Code

Save the changes to the generateDocument.js file. Open your terminal and navigate to the project directory.

To execute the code and generate the document, run the following command:

1
node generateDocument.js

If everything is set up correctly, you should see the following output:

1
2
Document Generated Successfully
Download Link: [ExpirableLinkToDownloadDocument]

The [ExpirableLinkToDownloadDocument] will be the actual link to download the generated document, which can be either a Word document (.docx) or a PDF file (.pdf).

Congratulations! You have successfully generated a document using the Documentero API service in JavaScript/Node.

Conclusion

In this tutorial, we learned how to generate Word or PDF documents in JavaScript/Node using the Documentero API service. We set up a Node.js project, made a POST request to the