How to Upload a Single Binary File to pdfRest with JavaScript in NodeJS

Send a single raw binary file from NodeJS to pdfRest and capture the resource information used by later calls.
Share this page

Follow this JavaScript in NodeJS walkthrough to upload one raw binary file to pdfRest with the Upload Files API Tool. You'll see the complete Upload a Single Binary to pdfRest sample first, then examine the transport code and API fields that matter when this JavaScript in NodeJS pattern moves into an application.

Why Upload a Single Binary File to pdfRest with JavaScript in NodeJS?

A managed upload is useful when several document operations will consume the same source. NodeJS can transfer the file once, store the returned ID with application state, and refer to that ID in compatible JSON or multipart requests.

For example, a document worker may stage a large PDF before sending it through extraction and security steps. Reusing the resource avoids repeating the original upload at every stage and makes the workflow easier to resume.

The raw-upload form differs from the multipart upload used for several files. Axios streams the file directly as the request data while headers provide the API key, binary media type, and filename.

JavaScript in NodeJS Code Example

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

// 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";

var upload_data = 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
    "Content-Filename": "filename.pdf",
    "Content-Type": "application/octet-stream",
  },
  data: upload_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 a Single Binary 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 a Single Binary 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 a Single Binary 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";

var upload_data = fs.createReadStream("/path/to/file");

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

Open the file as the request body

var upload_data = fs.createReadStream("/path/to/file");

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

This JavaScript in NodeJS raw-upload request places file bytes directly in the HTTP body. Because this JavaScript in NodeJS Upload a Single Binary to pdfRest call has no multipart file part, content-filename supplies the resource name.

Configure the binary headers and endpoint

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
    "Content-Filename": "filename.pdf",
    "Content-Type": "application/octet-stream",
  },
  data: upload_data, // set the data to be sent with the request
};

// send request and handle response or error
axios(config)

The JavaScript in NodeJS request joins the Upload a Single Binary to pdfRest endpoint, authentication header, and prepared body. For this Upload a Single Binary 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 uploaded resource response

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

The final Upload a Single Binary to pdfRest handling in JavaScript in NodeJS exposes the HTTP result and JSON body. Before using a Upload a Single Binary 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 can now stage one file with a raw binary JavaScript in NodeJS request and carry its managed resource ID into compatible pdfRest operations.

Handle the file stream and API response errors before persisting the resource ID. Keep the ID associated with its original filename and region, then delete the resource explicitly when early cleanup is appropriate.

Try the Upload a Single Binary 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 a Single Binary 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.