How to Encrypt and Decrypt PDFs via API
Securing sensitive documents is crucial for protecting confidential information. With password protection, you can control who views or modifies your PDFs. Using our API’s Encrypt PDF and Decrypt PDF endpoints, you can easily add or remove password protection from your documents, ensuring your data remains secure and accessible only to authorised users. In this tutorial, we’ll use a sample document to demonstrate how to programmatically encrypt and decrypt a PDF.
1. Encrypt a PDF
The first step in securing your document is to apply a password. The Encrypt PDF endpoint takes a PDF file (via URL or base64 string) and a password, returning a new, encrypted version of the file that requires the password to be opened.
You can set two types of passwords for granular control:
owner_password
– (required) The owner password required to open and view the document.user_password
– An optional, separate password that grants access to the document.
Endpoint Details
- HTTP Method:
POST
- Request URL:
https://us1.pdfgeneratorapi.com/api/v4/pdfservices/encrypt
- Documentation: Official API Docs
Request Body
This request protects the document with a password required for opening.
{
"file_url": "https://us1.pdfgeneratorapi.com/api/v4/documents/66/3fb326f98fcb0cded972d6901e921d95/share",
"owner_password": "1234",
"output": "url",
"name": "Encrypted_Document"
}
Response
The API returns a direct URL to the newly created, password-protected PDF.
{
"response": "https://us1.pdfgeneratorapi.com/api/v4/documents/66/3fb326f98fcb0cded972d6901e921d95/share",
"meta": {
"name": "Encrypted_Document.pdf",
"display_name": "Encrypted_Document",
"encoding": "binary",
"content-type": "application/pdf",
"public_id": "a1b23c4d5e6f7a8b9c0d1e2f3a4b5c6d"
}
}
2. Decrypt a PDF
If you need to remove password protection from a PDF for further processing or distribution, the Decrypt PDF endpoint handles it seamlessly. It takes an encrypted file and the correct password, providing an unrestricted, decrypted version of the document in return.
Endpoint Details
- HTTP Method:
POST
- Request URL:
https://us1.pdfgeneratorapi.com/api/v4/pdfservices/decrypt
- Documentation: Official API Docs
Request Body
Provide the URL of the encrypted file and the password used to protect it.
{
"file_url": "https://us1.pdfgeneratorapi.com/api/v4/documents/66/3fb326f98fcb0cded972d6901e921d95/share",
"owner_password": "1234",
"output": "url",
"name": "Decrypted_Document"
}
Response
The response contains a URL to the decrypted PDF file, which can now be opened without a password.
{
"response": "https://us1.pdfgeneratorapi.com/api/v4/documents/66/3fb326f98fcb0cded972d6901e921d95/share",
"meta": {
"name": "Decrypted_Document.pdf",
"display_name": "Decrypted_Document",
"encoding": "binary",
"content-type": "application/pdf",
"public_id": "f9e8d7c6b5a4f3e2d1c0b9a8f7e6d5c4"
}
}
Related articles you may find helpful
- Polish Your PDFs – Add Watermarks & Optimize
- Extract and Fill Existing PDF Forms Programmatically
- Generate QR Code via API
Updated on: 11/09/2025
Thank you!