How to Apply TDM Reservations to PDF with JavaScript in NodeJS

Learn how to protect documents from AI data mining using JavaScript with pdfRest TDM Reserve PDF API.
Share this page

Why Use TDM Reserve PDF with JavaScript?

The pdfRest TDM Reserve PDF API Tool offers developers a seamless way to embed protective metadata into PDF files, actively restricting automated text and data mining (TDM). This tutorial walks you through sending an API call to the TDM Reserve endpoint using JavaScript. By integrating this functionality into your frontend applications or Node.js backends, you can programmatically establish machine-readable boundaries that prevent AI crawlers from ingesting your digital assets without explicit permission.

Imagine a SaaS platform built with JavaScript that empowers independent authors to self-publish e-books and digital portfolios. To give creators peace of mind that their original work won't be harvested to train public Large Language Models, the platform’s developers can trigger the TDM Reserve PDF API during the file upload process. This automatically stamps every new document with a legally recognized W3C TDMRep policy, securing the creators' intellectual property right at the point of distribution.

TDM Reserve PDF with JavaScript Code Example

// This request demonstrates how to apply TDM rights metadata to a PDF.
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";

// Create a new form data object and append the PDF file and TDM policy to it.
var tdmReservedPdfData = new FormData();
tdmReservedPdfData.append('file', fs.createReadStream('/path/to/file'));
tdmReservedPdfData.append('policy', 'https://example.com/tdm-policy');
tdmReservedPdfData.append('output', 'pdfrest_tdm_reserved_pdf');

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

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

// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

Source: GitHub Repository

Breaking Down the Code

The code begins by importing necessary modules: axios for making HTTP requests, form-data for handling form submissions, and fs for file system operations.

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

The apiUrl variable is set to the US-based API endpoint by default. For users in Europe, there's an option to switch to the EU-based service for GDPR compliance.

var apiUrl = "https://api.pdfrest.com";

A FormData object is created to hold the PDF file and the TDM policy URL. The append method is used to add these fields to the form data.

var tdmReservedPdfData = new FormData();
tdmReservedPdfData.append('file', fs.createReadStream('/path/to/file'));
tdmReservedPdfData.append('policy', 'https://example.com/tdm-policy');
tdmReservedPdfData.append('output', 'pdfrest_tdm_reserved_pdf');

The configuration for the axios request is defined, specifying the HTTP method, URL, headers (including the API key), and the form data.

var tdmReservedPdfConfig = {
  method: 'post',
  maxBodyLength: Infinity,
  url: apiUrl + '/tdm-reserved-pdf',
  headers: {
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...tdmReservedPdfData.getHeaders()
  },
  data : tdmReservedPdfData
};

The request is sent using axios, and the response or any errors are logged to the console.

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

Beyond the Tutorial

In this tutorial, you learned how to make a multipart API call to the pdfRest TDM Reserve PDF endpoint using JavaScript. This allows you to apply TDM rights metadata to a PDF document programmatically. To explore more functionalities, you can demo all of the pdfRest API Tools in the API Lab. For further details, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.

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