Manipulation API

Merge, split, compress, rotate, and modify PDFs with simple API calls to enhance your document processing workflows.

Merge API
Combine multiple PDFs into a single document with customizable order

The Merge API allows you to combine multiple PDF files into a single PDF document. You can specify the order of the files, and the API handles large merges efficiently using a staged approach for more than 10 files.

Endpoint

POST https://mega-pdf.com/api/merge

Authentication

Authenticate requests using an API key in the x-api-key header or as a query parameter (api_key).

// Header example
x-api-key: your-api-key

// Query parameter example
https://mega-pdf.com/api/merge?api_key=your-api-key

Request Parameters

The API accepts multipart/form-data requests with the following parameters:

ParameterTypeDescriptionRequired
filesFile[]Array of PDF files to merge (minimum 2 files)Yes
orderString (JSON array)Array of indices specifying the merge order (e.g., [0, 1, 2])No

Example Request

Merge two PDFs using cURL:

curl -X POST https://mega-pdf.com/api/merge \
  -H "x-api-key: your-api-key" \
  -F "files=@/path/to/doc1.pdf" \
  -F "files=@/path/to/doc2.pdf" \
  -F "order=[0,1]"

Response Format

Successful responses include the merged file URL and metadata:

{
  "success": true,
  "message": "PDF merge successful",
  "fileUrl": "/api/file?folder=merges&filename=uuid-merged.pdf",
  "filename": "uuid-merged.pdf",
  "mergedSize": 1234567,
  "totalInputSize": 2345678,
  "fileCount": 2
}

Error responses:

{
  "success": false,
  "error": "At least two PDF files are required for merging"
}

Code Examples

Using the Merge API with JavaScript (Node.js):

const formData = new FormData();
formData.append('files', fs.createReadStream('doc1.pdf'));
formData.append('files', fs.createReadStream('doc2.pdf'));
formData.append('order', JSON.stringify([0, 1]));

fetch('https://mega-pdf.com/api/merge', {
  method: 'POST',
  headers: {
    'x-api-key': 'your-api-key'
  },
  body: formData
})
  .then(response => response.json())
  .then(data => {
    if (data.success) {
      console.log('Merge successful:', data.fileUrl);
    } else {
      console.error('Merge failed:', data.error);
    }
  })
  .catch(error => console.error('Error:', error));