# Remove the password from a PDF

Decrypt a password-protected PDF so it no longer requires a password to open. Supply the password that opens the file and get back a document that opens freely.

Remove the password from an encrypted PDF. Supply the password that currently
opens the file and get back a document that opens without one. The API is
stateless: your document is processed in-region and never stored.

## Endpoint

<Endpoint method="POST" path="/v1/remove_password" />

Available in every region. See [Regions & data residency](/docs/api/regions-and-data-residency)
for routing and data residency.

| Region         | URL                                              |
| -------------- | ------------------------------------------------ |
| Global         | `https://api.pdfblocks.com/v1/remove_password`   |
| United States  | `https://us.api.pdfblocks.com/v1/remove_password` |
| US HIPAA       | `https://hipaa.api.pdfblocks.com/v1/remove_password` |
| European Union | `https://eu.api.pdfblocks.com/v1/remove_password` |
| United Kingdom | `https://uk.api.pdfblocks.com/v1/remove_password` |
| Canada         | `https://ca.api.pdfblocks.com/v1/remove_password` |
| Australia      | `https://au.api.pdfblocks.com/v1/remove_password` |
| Japan          | `https://jp.api.pdfblocks.com/v1/remove_password` |
| India          | `https://in.api.pdfblocks.com/v1/remove_password` |
| Brazil         | `https://br.api.pdfblocks.com/v1/remove_password` |

## Authentication

Authenticate every request with your secret API key in the `X-API-Key` header,
over HTTPS. Create and manage keys from the
[dashboard](https://dashboard.pdfblocks.com). See
[Authentication](/docs/api/authentication) for details.

## Request

The endpoint accepts a `multipart/form-data` request body.

<ParamField name="file" type="file" required>
  The encrypted input PDF document.
</ParamField>

<ParamField name="password" type="string" required>
  The password that currently opens the file. Maximum 256 characters.
</ParamField>

<Note>
  Removing the password requires knowing it: supply the password that currently
  opens the document. There is no recovery or brute-force path.
</Note>

## Examples

Remove the password from an encrypted PDF:

<CodeGroup>

```bash title="cURL"
curl https://api.pdfblocks.com/v1/remove_password \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F password='0pen-Sesame' \
  -o unlocked.pdf
```

```python title="Python"
# pip install requests
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/remove_password',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'password': '0pen-Sesame'},
    )

response.raise_for_status()
with open('unlocked.pdf', 'wb') as output:
    output.write(response.content)
```

```javascript title="Node.js"
// Node.js 18+
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('password', '0pen-Sesame');

const response = await fetch('https://api.pdfblocks.com/v1/remove_password', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('unlocked.pdf', Buffer.from(await response.arrayBuffer()));
```

```php title="PHP"
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/remove_password');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'password' => '0pen-Sesame',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('unlocked.pdf', $pdf);
}
```

```ruby title="Ruby"
# gem install http
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/remove_password', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    password: '0pen-Sesame',
  })

File.write('unlocked.pdf', response.body) if response.status.success?
```

```go title="Go"
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("password", "0pen-Sesame")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_password", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("unlocked.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
```

```csharp title="C#"
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("0pen-Sesame"), "password" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/remove_password", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "unlocked.pdf", await response.Content.ReadAsByteArrayAsync());
```

</CodeGroup>

## Response

On success, the response is `200 OK` with the decrypted PDF as the body:

```http
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 48213
```

The output document no longer requires a password to open: its pages and content
are unchanged. Stream the body straight to a file, as the examples above do;
nothing is stored on our side.

## Errors

Failed requests return an `application/problem+json` body. The most common one
for this endpoint is a `400`, returned when the supplied `password` doesn't open
the file. The `errors` object names each field:

```json
{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "password": ["The password is incorrect."]
  }
}
```

A missing or invalid `X-API-Key` returns a `401`. See
[Errors](/docs/api/errors) for every status code and the full response shape.

## Related actions

<CardGroup cols={2}>

<Card title="Add a password" href="/docs/api/add-password-to-pdf">
  Encrypt a PDF with a password.
</Card>

<Card title="Remove restrictions" href="/docs/api/remove-restrictions-from-pdf">
  Clear permission flags.
</Card>

<Card title="Add restrictions" href="/docs/api/add-restrictions-to-pdf">
  Set permission flags.
</Card>

<Card title="Remove signatures" href="/docs/api/remove-signatures-from-pdf">
  Strip signatures from a document.
</Card>

</CardGroup>
