Below is an end-to-end example of submitting a Merge request using Node.js programming language:


// Simple script to showcasing how to use the Merge API in the LEADTOOLS Cloud Services.
// In order to run this script, the following changes will need to be added:
//	1) Place your Application ID in the user portion of the getRequestOptions method.
//	2) Place your Application Password in the password portion of the getRequestOptions method.
//
// The script will perform the following operations in order:
//	1) Upload 2 files to the LEADTOOLS Cloud Services using the forMerge flag.
//   2) Make a call to the Merge end-point to combine the two files.
//   3) Parse the JSON output from the LEADTOOLS Cloud Services

// This script makes use of the following NodeJS libraries:
//  1) Request - More information about this library can be found here: https://github.com/request/request
//  2) File System - More information about this library can be found here: https://nodejs.org/api/fs.html

const request = require('request');
//If uploading a file as multi-part content, we will need the file-system library installed.
//const fs = require('fs');

let FormatsEnum = {
	"png": 1, "jpeg": 2, "tiff": 3, "pdf": 4, "pdfa": 5,
	"pdfImage": 6, "pdfImageOverText": 7, "pdfaImageOverText": 8,
	"docx": 9, "docxFramed": 10, "rtf": 11, "rtfFramed": 12,
	"txt": 13, "txtFramed": 14
 };

 var servicesUrl = "https://azure.leadtools.com/api/";

let tiffUrl = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif';
let pdfUrl = 'https://demo.leadtools.com/images/pdf/leadtools.pdf';

let firstFileId = '';
let secondFileId = '';
let outputFormat = FormatsEnum.pdf;

uploadFile(tiffUrl, firstFileUploadCallback);

function uploadFile(url, callback){
	let uploadUrl = `${servicesUrl}uploadFile?forMerge=true&fileurl=${url}`;
	request.post(getRequestOptions(uploadUrl), callback);
	//If uploading a file as multi-part content:
	//var uploadUrl = servicesUrl + "UploadFile?forMerge=true";
	//var req = request.post(getRequestOptions(uploadUrl), callback);
	//var form = req.form();
	//form.append('file', fs.createReadStream('path\to\inputFile'));
}

function firstFileUploadCallback(error, response, body){
	if (!error && response.statusCode === 200) {
		firstFileId = body;
		console.log("First File ID: " + firstFileId);

		checkVerification(firstFileId, uploadSecondFile);
	}
}

function uploadSecondFile(){
	uploadFile(pdfUrl, secondFileUploadCallback);
}

function secondFileUploadCallback(error, response, body){
	if (!error && response.statusCode === 200) {
		secondFileId = body;
		console.log("Second File ID: " + secondFileId);

		checkVerification(secondFileId, mergeFile);
	}
}

function mergeFile(){
	let mergeUrl = `${servicesUrl}Conversion/Merge?format=${outputFormat}`;
	let options = getRequestOptions(mergeUrl);
	options['json'] = [
		{
			'firstPage' : 1,
			'lastPage' : -1,
			'fileId' : firstFileId
		},
		{
			'pages' : [5,1,2,4,3],
			'fileId' : secondFileId
		}
	]

	request.post(options, (error, response, body) =>{
		if (!error && response.statusCode === 200) {
			console.log("Merge command was successful");
			queryServices(firstFileId);
		} else {
			console.log("Merge failed with HTTP code: " + response.statusCode);
			console.log(body);
		}
	})
}

function checkVerification(id, callback){
	var queryUrl = servicesUrl + "Query?id=" + id;
	request.post(getRequestOptions(queryUrl), async function (error, response, body) {
		var results = JSON.parse(body);
		if (!error && results['FileStatus'] !== 123) {

			console.log("Verification finished with return code: " + response.statusCode);

			if(results['FileStatus'] == 122){
				callback();
			}else{
				console.log("File failed verification with File Status: " + results['FileStatus']);
			}

		} else {
			//The file has not yet finished processing.
			await function () {
				return new Promise(resolve => setTimeout(resolve, 5000)); //Sleep for 5 seconds before trying again
			};
			checkVerification(id, callback); //Call the method again.
		}
	});
}

function queryServices(id) {
	//Function to query the status of a request.  If the request has not yet finished, this function will recursively call itself until the file has finished.
	var queryUrl = servicesUrl + "Query?id=" + id;
	request.post(getRequestOptions(queryUrl), async function (error, response, body) {
		var results = JSON.parse(body);
		if (!error && (results['FileStatus'] !== 100 && results['FileStatus'] !== 123)) {

			console.log("File finished processing with return code: " + response.statusCode);
			if (results['FileStatus'] !== 200) {
				return;
			}
			console.log("Results: \n");
			parseJson(results['RequestData']);
		} else {
			//The file has not yet finished processing.
			await function () {
				return new Promise(resolve => setTimeout(resolve, 5000)); //Sleep for 5 seconds before trying again
			};
			queryServices(id); //Call the method again.
		}
	});
}

function parseJson(jsonObject) {
	//Function to decode the JSON object that was returned by the LEADTOOLS CloudServices.
	for (let i = 0; i < jsonObject.length; i++) {
		var currentRequest = jsonObject[i];
		console.log("Service Type: " + currentRequest['ServiceType']);
		if (currentRequest['ServiceType'] === 'Conversion') {
			console.log("Urls: ");
			currentRequest['urls'].forEach(url => {
				console.log(url);
			});
		}
	}
}


function getRequestOptions(url) {
	//Function to generate and return HTTP request  options.
	var requestOptions = {
		url: url,
		auth: {
			user: "Replace with Application ID",
			password: "Replace with Application Password"
		}
	};
	return requestOptions;
}