> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://support.pdfgeneratorapi.com/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# 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](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/encryptDocument) and [Decrypt PDF](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/decryptDocument) 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](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/encryptDocument) 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.

| OWNER PASSWORD allows you to change permissions for Printing, Copying Text or Graphics, Inserting, Rotating or Deleting Pages, Adding Annotations or Signatures and Filling in Existing Form Fields.

#### Endpoint Details

* **HTTP Method:** `POST`
* **Request URL:** `https://us1.pdfgeneratorapi.com/api/v4/pdfservices/encrypt`
* **Documentation:&#32;**[Official API Docs](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/encryptDocument)

###### 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](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/decryptDocument) 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:&#32;**[Official API Docs](https://docs.pdfgeneratorapi.com/v4#tag/Services/operation/decryptDocument)

###### 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

* [Generate QR Code via API](https://support.pdfgeneratorapi.com/en/article/generate-qr-code-via-api-1k3vqj8/)
* [How to create a JSON Web Token (JWT)?](https://support.pdfgeneratorapi.com/en/article/how-to-create-a-json-web-token-jwt-anv0ot/)
* [How to generate your first document with API v4 using Postman](https://support.pdfgeneratorapi.com/en/article/how-to-generate-your-first-document-with-api-v4-using-postman-1k4uuph/)