Add a password to a PDF document

Use this action to encrypt and protect a PDF document with a password to prevent unauthorized access.

Endpoints

POST https://api.pdfblocks.com/v1/add_password

Global endpoint (default)

POST https://eu.api.pdfblocks.com/v1/add_password

European-only endpoint

Parameters

The endpoint accepts multipart/form-data request bodies with the folowing parameters:

file Required

The content of the input PDF document.

password Required

The password required to open the file.

  • The length is between 4 and 32 characters.

  • The characters in the password must be in the ASCII printable range (between positions 32 and 126). This range includes lowercase and uppercase letters, numbers, space, and some commonly used symbols like . , : ; # $ % & ( ) * + = ? !

  • Both conditions as a regex pattern: ^[\x20-\x7e]{4,32}$

encryption_algorithm optional

The algorithm used to encrypt the file.

  • Valid values are AES-128 and AES-256
  • The default value is AES-128

Returns

If successful, the call returns a response with Content-Type: application/pdf with the content of the password-protected PDF document. Otherwise, this call returns an error.

Code examples

curl

curl \
-F file=@input.pdf \
-F password='correct horse' \
-F encryption_algorithm=AES-128 \
-o encrypted.pdf \
-H 'X-Api-Key: your_api_key' \
https://api.pdfblocks.com/v1/add_password

Python

# pip install requests

import requests

url = 'https://api.pdfblocks.com/v1/add_password'

body = {
  'file' : open('input.pdf', 'rb'),
  'password' : 'correct horse',
  # 'encryption_algorithm' : 'AES-128',
}

headers = { 'X-Api-Key' : 'your_api_key' }

response = requests.post(url, files=body, headers=headers)

if response.status_code == 200:
  with open('encrypted.pdf', 'wb') as output:
    for chunk in response.iter_content():
        output.write(chunk)

Ruby

# gem install http

require 'http'

url = 'https://api.pdfblocks.com/v1/add_password'

body = {
  file: HTTP::FormData::File.new('input.pdf'),
  password: 'correct horse',
  # encryption_algorithm: 'AES-128',
}

headers = { 'X-Api-Key' => 'your_api_key' }

response = HTTP.post(url, form: body, headers: headers)

if response.status.ok?
  File.open('encrypted.pdf', 'wb') do |output|
    response.body.each { |chunk| output.write(chunk) }
  end
end

C# (.NET Core)

// Using .NET Core 3.1

using System.IO;
using System.Net.Http;

class Program
{
    static void Main()
    {
        string url = "https://api.pdfblocks.com/v1/add_password";

        var inputFile = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);

        var body = new MultipartFormDataContent();
        body.Add(new StreamContent(inputFile), "file");
        body.Add(new StringContent("correct horse"), "password");
        // body.Add(new StringContent("AES-128"), "encryption_algorithm");

        var message = new HttpRequestMessage(HttpMethod.Post, url);
        message.Content = body;
        message.Headers.Add("X-Api-Key", "your_api_key");

        var client = new HttpClient();
        var response = client.SendAsync(message).Result;
        if(response.IsSuccessStatusCode)
        {
            using var output = new FileStream("encrypted.pdf", FileMode.Create, FileAccess.Write);
            response.Content.CopyToAsync(output).Wait();
        }
    }
}