The tutorial and the sample source code elaborate on optimizing the PDF files using Web API in JavaScript (Node js). The users can use this web API to store reduce the file size of the PDFs available on their local storage or the cloud.

Here’s what we will cover in this tutorial:

Features of PDF Optimize Endpoint

  1. Endpoint Parameters
  2. Query Parameter
  3. Endpoint Method
  4. Sample Example Using Javascript
  5. Sample Code
  6. Output Document
  7. Step-by-Step Guide
  8. File Sizes
  9. Output Demo

The users can use this PDF from the URL endpoint of web API to reduce the size of a PDF file. This endpoint takes the URL link and other optional parameters to generate an output link to a compressed PDF file. Moreover, the users can choose to download the file using various filing modules such as “fs” to store them on their local storage.

Endpoint Parameters

Following are the parameters of the PDF Optimize endpoint:

1. URL

It is a required parameter: a string containing the link to the source PDF file that the user wants to optimize.  The users can provide links to their files hosted on online storage systems such as google drive, Dropbox, and cloud storage. Moreover, the users can check the files upload sections of the API to upload their files via API.

Note: If the users are getting “too many requests” or “access denied” error, they must try to add “cache” as a parameter to enable built-in caching.

2. httpusername

It is an optional parameter containing information regarding the HTTP auth user if it is essential for accessing the source URL.

3. httppassword

It is an optional parameter containing information regarding the HTTP auth password if it is essential to access the source document URL.

4. name

It is an optional parameter, a string containing the name of the output file. It is set to “result.pdf” by default.

5. expiration

It is an optional parameter containing information regarding the output link’s expiration in minutes. The default expiration time is 60 minutes or 1 hour, after which the system removes the generated file from temporary file storage. The users can use built-in files storage to store their files permanently.

6. password

It is an optional parameter, a string containing the password of the source file, in case the source file is password protected. The users can also explore the PDF password and security endpoint here to add or remove security from their PDF files.

7. async

It is an optional parameter that runs the processing asynchronously. Moreover, this parameter returns the JobId, which the users can utilize for checking the processing state. The processing state could be “working,” “failed,” “aborted”, and “success.”  Its value is set to “false” by default.

8. encrypt

It is an optional parameter, a string containing the value “true” or “false” to encrypt the source PDF file. Its value is set to “false” by default.

9. profiles

This optional parameter must be a string. The users can use this parameter for setting additional configurations for tuning and extra options.

Query Parameter

The query parameters are the parameters of the API endpoints that the users need to attach with the endpoint URLs. This endpoint does not require any query parameters.

Endpoint Method

This endpoint only supports the API “POST” method.

Sample Example Using Javascript

The following source code explains to users how to reduce the file size using Web API. This code takes the URL to a PDF file containing a sample invoice and optimizes it. Moreover, the parameter “async” is set to “false” to run the task synchronously. Furthermore, this code explores the use of the “expiration” parameter to set the expiration limit of the resulting file. Finally, the users can see the output file’s size and URL in the terminal and use that URL to see the resulting PDF file or use the various filing modules such as “fs” to download the file contents and store them on their local storage.

Sample Code

Below is the sample code to optimize the PDF file:

var request = require('request');
var API_KEY = "**********************";
var fileUrl = "";

var options = {
   'method': 'POST',
   'url': '',
   'headers': {
     'x-api-key': API_KEY

   formData: {
    "url": fileUrl,
    "async": "false",
    "name": "view-compressed",
    "expiration": "15"


 request(options, function (error, response) {
   if (error) throw new Error(error);

Output Document

Below is the screenshot of the output file obtained in the API response:

Output PDF Document

Step-by-Step Guide

  1. Install the “request” module using “npm install request” and import it into the code.
  2. Set the package.json file to run and test the javascript file.
  3. Declare and initialize the “API_KEY” variable with the API key obtained from the dashboard or sign up to get the API key. The users can create an env file to access the key in each file and hide it.
  4. Note: The users can visit here to sign into and use the available credits to call the API endpoints. Moreover, the users can sign in using their student accounts to get extra API credits.
  5. Declare and initialize the “fileUrl” variable with the source file URL. The users can then optimize their files by only changing this variable without going into the code and API details.
  6. Declare a variable named “options” and initialize it with the API options such as method, API endpoint URL, headers, and the form data. The form data is an object that contains the endpoint parameter’s values. For instance, the “async” variable value is false in the sample code. The users can add the above parameters to this object and modify these variables according to their optimization needs.
  7. The final step is to call the imported request method with the above arguments and print the API response in the user’s terminal. The users can pass this response to file streams to download the resulting file contents.

File Sizes

Below is the screenshot to demonstrate the size difference of both files.

File Sizes

Note: The users can find that there was not much difference between both file sizes and can explore the other parameter options and the file sizes to explore the API optimizations.

Output Demo

Below is the gif to demonstrate the working of the code:

Output Demo File

Note: The users can read more on this API endpoint here and find the various code samples. The samples are available in many programming languages to demonstrate the API working with the uploaded file and file URL synchronously and asynchronously.