Security API

Add password protection, remove restrictions, and secure your PDF documents with enterprise-grade encryption.

Protect API
Add password protection and permission restrictions to PDF documents

The Protect API allows you to secure PDF documents with password protection and control permissions like printing, copying, and editing. This helps you safeguard sensitive information and control how recipients can interact with your documents.

Endpoint

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

Authentication

Authenticate requests using an API key in the x-api-key header.

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

Request Parameters

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

ParameterTypeDescriptionRequired
fileFilePDF file to protect (max 50MB)Yes
passwordStringPassword to set for the PDF (minimum 4 characters)Yes
permissionStringPermission level: restricted (apply specific permissions) or all (grant all permissions)No (default: restricted)
allowPrintingBooleanAllow document printingNo (default: false)
allowCopyingBooleanAllow content copyingNo (default: false)
allowEditingBooleanAllow content editingNo (default: false)

Example Request

Add password protection to a PDF with restricted permissions using cURL:

curl -X POST https://api.mega-pdf.com/api/pdf/protect \
  -H "x-api-key: your-api-key" \
  -F "file=@/path/to/document.pdf" \
  -F "password=SecureP@ssword" \
  -F "permission=restricted" \
  -F "allowPrinting=true"

Response Format

Successful responses include the protected file URL:

{
  "success": true,
  "message": "PDF protected with password successfully",
  "fileUrl": "/api/file?folder=protected&filename=uuid-protected.pdf",
  "filename": "uuid-protected.pdf",
  "originalName": "document.pdf",
  "methodUsed": "pdfcpu",
  "billing": {
    "usedFreeOperation": true,
    "freeOperationsRemaining": 9,
    "currentBalance": 10.50,
    "operationCost": 0.00
  }
}

Error responses:

{
  "success": false,
  "error": "Password must be at least 4 characters"
}

Code Examples

Using the Protect API with JavaScript:

const formData = new FormData();
formData.append('file', fs.createReadStream('document.pdf'));
formData.append('password', 'SecureP@ssword');
formData.append('permission', 'all'); // Grant all permissions

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