Below is an end-to-end example of submitting a Merge request using Python 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 appId variable.
#	2) Place your Application Password in the password variable
#
# 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 Requests Python library.
# More information about this library can be found here: http://docs.python-requests.org/en/master/

import requests
import sys
import time
import json
from enum import Enum

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

# The application ID.
appId = "Replace with Application ID"

# The application password.
password = "Replace with Application Password"

# Conversion output formats.
class FormatsEnum(Enum):
    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

# Function to upload a file from a URL to the LEADTOOLS Cloud Services to be used with the Merge API command.
def upload_url_for_merge(fileUrl):
    baseUploadUrl = '{}UploadFile?fileurl={}&forMerge=true'
    formattedUploadUrl = baseUploadUrl.format(servicesUrl, fileUrl)
    request = requests.post(formattedUploadUrl, auth=(appId, password))

    if request.status_code != 200:
        print("Error sending the upload request for file: " + fileUrl)
        print(request.text)
        sys.exit()

    return request.text

# Function to upload a file to the LEADTOOLS Cloud Services to be used with the Merge API command.
def upload_file_for_merge(path):
    baseUploadUrl ='{}UploadFile&forMerge=true'
    formattedUploadUrl = baseUploadUrl.format(servicesUrl)
    file = {'file' : open(path, 'rb')}
    request = requests.post(formattedUploadUrl, auth=(appId, password), files = file)

    if request.status_code != 200:
        print("Error sending the upload request for file: " + path)
        print(request.text)
        sys.exit()

    return request.text

# Function to check if a file has passed the verification process.
def check_file_verification(id):
    baseQueryUrl = '{}Query?id={}'
    formattedQueryUrl = baseQueryUrl.format(servicesUrl, id)
    returnedData = ""
    while True:
        request = requests.post(formattedQueryUrl, auth=(appId, password))
        returnedData = request.json()
        # We will check to make sure that the file has passed verification.  For the full list of possible File Statuses, refer to our documentation here:
        # https://services.leadtools.com/documentation/api-reference/query
        if returnedData['FileStatus'] != 123:
            break
        time.sleep(5)

    if returnedData['FileStatus'] != 122:
        print("The file has failed the verification process with File Status: " + returnedData['FileStatus'])
        return False

    return True

# We will be uploading the file via a URl.  Files can also be passed by adding a PostFile to the request.  Only 1 file will be accepted per request.
# The Merge Api requires you to upload your files with the forMerge parameter set.  A file that has been uploaded and tagged as forMerge is not compatible with other LEADTOOLS Cloud Services API functions.
tiffFileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif'
firstFileId = upload_url_for_merge(tiffFileURL)
print("First File ID: " + firstFileId)

# At this point the file has been successfully uploaded to the LEADTOOLS Cloud Services and is undergoing verification.  Before we continue, we need to make sure that the uploaded file has passed verification
if check_file_verification(firstFileId) == False:
    sys.exit()

# We will now upload the second file we would like to merge
pdfFileUrl = 'https://demo.leadtools.com/images/pdf/leadtools.pdf'
secondFileId = upload_url_for_merge(pdfFileUrl)

print("Second File ID: " + secondFileId)
if check_file_verification(secondFileId) == False:
    sys.exit()


# We now need to create the JSON body response for the Merge Api Command
data = []

firstFileData = {}
# pass every page in the file
firstFileData['firstPage'] = 1
firstFileData['lastPage'] = -1
firstFileData['fileId'] = firstFileId
data.append(firstFileData)

secondFileData = {}
# Make sure this file's pages are merged in a specific order.
secondFileData['pages'] = [5,1,2,4,3]
secondFileData['fileId'] = secondFileId
data.append(secondFileData)

# The output format of the merged files
mergeFormat = FormatsEnum.Pdf.value

baseMergeUrl = '{}Conversion/Merge?format={}'
formattedMergeUrl = baseMergeUrl.format(servicesUrl, mergeFormat)
request = requests.post(formattedMergeUrl, auth=(appId, password), json=data)
if request.status_code != 200:
        print("Error sending merge request")
        print(request.text)
        sys.exit()

# At this point the merge request has been successfully received by the server, and is being processed.
baseQueryUrl = '{}Query?id={}'
formattedQueryUrl = baseQueryUrl.format(servicesUrl, firstFileId)

# Poll the services to determine if the request has finished processing
while True:
    request = requests.post(formattedQueryUrl, auth=(appId, password))
    returnedData = request.json()
    if returnedData['FileStatus'] != 100:
        break
    time.sleep(5)

print("File finished processing with file status: " +
    str(returnedData['FileStatus']))

if returnedData['FileStatus'] != 200:
    sys.exit()

try:
    print("Results:")
    returnedJson = returnedData['RequestData']
    for requestObject in returnedJson:
        print("Service Type: " + requestObject['ServiceType'])
        if requestObject['ServiceType'] == 'Conversion':
            print("Returned URLS:")
            for url in requestObject['urls']:
                print(url)
except Exception as e:
    print("Failed to Parse JSON")
    print(e)