Add password protection, remove restrictions, and secure your PDF documents with enterprise-grade encryption.
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.
POST https://api.mega-pdf.com/api/pdf/protect
Authenticate requests using an API key in the x-api-key
header.
// Header example
x-api-key: your-api-key
The API accepts multipart/form-data
requests with the following parameters:
Parameter | Type | Description | Required |
---|---|---|---|
file | File | PDF file to protect (max 50MB) | Yes |
password | String | Password to set for the PDF (minimum 4 characters) | Yes |
permission | String | Permission level: restricted (apply specific permissions) or all (grant all permissions) | No (default: restricted) |
allowPrinting | Boolean | Allow document printing | No (default: false) |
allowCopying | Boolean | Allow content copying | No (default: false) |
allowEditing | Boolean | Allow content editing | No (default: false) |
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"
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"
}
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));