How to Upload Multiple Files to pdfRest with JavaScript in NodeJS

Stage multiple local files with one NodeJS multipart upload and use the returned resource records in later calls.
Share this page

Learn how to upload multiple files to pdfRest in one multipart request using JavaScript in NodeJS and the Upload Files API Tool. This Upload Multiple to pdfRest tutorial keeps the official sample intact and breaks down authentication, payload construction, endpoint behavior, and result handling for JavaScript in NodeJS.

Why Upload Multiple Files to pdfRest with JavaScript in NodeJS?

A single multipart request can prepare all of the inputs needed by a larger pdfRest workflow. This reduces request setup and gives the NodeJS caller one response containing the uploaded file resources.

A closing-document service could upload a contract, exhibit, and signature image before it assembles the final packet. Each result ID can then be routed to the operation that understands that source.

The sample appends the file field three times to one FormData object. Axios uses the generated boundary headers from that object, so callers should not substitute a manually written multipart boundary.

JavaScript in NodeJS Code Example

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
// This request demonstrates how to upload a document to the service.

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

// Create a new form data object and append the PDF file and parameters to it
var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));
data.append("file", fs.createReadStream("/path/to/file"));
data.append("file", fs.createReadStream("/path/to/file"));

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: apiUrl + '/upload',
  headers: {
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // set headers for the request
  },
  data : data // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Source for Upload Multiple to pdfRest: View the JavaScript in NodeJS sample on GitHub.

Breaking Down the Code

Load the NodeJS request and file modules

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

Axios sends the HTTP calls for Upload Multiple to pdfRest, FormData constructs multipart bodies when needed, and Node's fs module opens local files as streams. Together, these packages provide the transport and file handling required by the JavaScript Upload Multiple to pdfRest sample.

Select the pdfRest service region

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

// Create a new form data object and append the PDF file and parameters to it

apiUrl centralizes regional routing for Upload Multiple to pdfRest. The NodeJS Upload Multiple to pdfRest flow can switch to the documented EU hostname without changing the endpoint paths that follow.

Append several files to one FormData body

var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));
data.append("file", fs.createReadStream("/path/to/file"));
data.append("file", fs.createReadStream("/path/to/file"));

// define configuration options for axios request
var config = {

Each local source is added by JavaScript in NodeJS under another file part in the same multipart body. The Upload Multiple to pdfRest response returns several resources whose filenames and IDs the JavaScript in NodeJS caller should keep paired.

Configure the multipart request

var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: apiUrl + '/upload',
  headers: {
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // set headers for the request
  },
  data : data // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
.then(function (response) {

The JavaScript in NodeJS request joins the Upload Multiple to pdfRest endpoint, authentication header, and prepared body. For this Upload Multiple to pdfRest call, the multipart metadata generated by JavaScript in NodeJS must remain paired with the body so pdfRest can separate files from options.

Read the collection of uploaded resources

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

The final Upload Multiple to pdfRest handling in JavaScript in NodeJS exposes the HTTP result and JSON body. Before using a Upload Multiple to pdfRest output or request ID, production JavaScript in NodeJS code should verify the status and retain the identifier required by the next step.

Beyond the Tutorial

You now know how JavaScript in NodeJS can send several files in one multipart body and preserve the resource information returned for each upload.

Check the upload response before launching dependent work and preserve names with IDs. If one local stream cannot be opened, fail the batch deliberately rather than allowing the remaining resources to masquerade as a complete set.

Try the Upload Multiple to pdfRest workflow with a representative file in API Lab, then adapt the request in JavaScript in NodeJS. The Upload Files API Tool documentation provides the complete JavaScript in NodeJS context for Upload Multiple to pdfRest values, defaults, and limitations.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.