---
title: "Restrict access to PDF with HTTP basic authentication | Nutrient"
canonical_url: "https://www.nutrient.io/guides/web/open-a-document/secured-documents/http-authenticated/"
md_url: "https://www.nutrient.io/guides/web/open-a-document/secured-documents/http-authenticated.md"
last_updated: "2026-05-20T19:49:34.899Z"
description: "Sometimes you want to restrict access to your PDFs so they aren't publicly available on the internet. The easiest way to achieve this is with HTTP basic authentication."
---

# Restrict access to PDFs with HTTP basic authentication

Sometimes you want to restrict access to your PDFs so they aren't publicly available on the internet. The easiest way to achieve this is with [HTTP basic authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme).

If you try to load a PDF URL that's protected by HTTP basic authentication, Nutrient will ask you for a username and password. To avoid entering these, you can fetch the PDF yourself, passing the credentials as an `Authorization` header. Once you've received the response, the `load` method accepts the content as an `ArrayBuffer` ([try it in the Playground](https://www.nutrient.io/demo/sandbox?p=eyJ2IjoxLCJzZXR0aW5ncyI6eyJmaWxlTmFtZSI6ImJhc2ljLnBkZiJ9LCJqcyI6ImNvbnNvbGUubG9nKFwiRGVtb25zdHJhdGluZyBIVFRQIEJhc2ljIEF1dGhlbnRpY2F0aW9uIGZvciBQREZzLi4uXCIpO1xuXG5jb25zb2xlLmxvZyhcIlxcbkhUVFAgQmFzaWMgQXV0aCBzY2VuYXJpb3M6XCIpO1xuY29uc29sZS5sb2coXCIxLiBGZXRjaCBQREYgd2l0aCBjcmVkZW50aWFscyBpbiBBdXRob3JpemF0aW9uIGhlYWRlclwiKTtcbmNvbnNvbGUubG9nKFwiMi4gQ29udmVydCByZXNwb25zZSB0byBBcnJheUJ1ZmZlclwiKTtcbmNvbnNvbGUubG9nKFwiMy4gTG9hZCBBcnJheUJ1ZmZlciBkaXJlY3RseSBpbnRvIE51dHJpZW50XCIpO1xuXG5jb25zb2xlLmxvZyhcIlxcbk5vdGU6IFBsYXlncm91bmQgdXNlcyBwdWJsaWMgUERGcywgdGhpcyBkZW1vIHNob3dzIHRoZSBwYXR0ZXJu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CJjc3MiOiIvKiBBZGQgeW91ciBDU1MgaGVyZSAqL1xuXHQifQ%253D%253D)):

```js

async function loadProtectedPDF(url, username, password) {
  // Base-64 encode your credentials and set them as an `Authorization` header.
  const headers = new Headers();
  const encodedCredentials = btoa(`${username}:${password}`);
  headers.set("Authorization", `Basic ${encodedCredentials}`);

  try {
    // Fetch the PDF and read the response as an `ArrayBuffer`.
    const pdfResponse = await fetch(url, { headers });
    if (!pdfResponse.ok) {
      throw new Error(`HTTP error: ${pdfResponse.status}`);
    }
    const document = await pdfResponse.arrayBuffer();

    // Pass the `ArrayBuffer` as a PDF option instead of a URL.
    const instance = await NutrientViewer.load({
      container: "#pspdfkit",

      document
    });
    console.log("Protected document loaded successfully");
    return instance;
  } catch (error) {
    if (error.message.includes("401")) {
      console.error("Authentication failed: Invalid credentials");
    } else {
      console.error("Failed to load protected document:", error.message);
    }
    throw error;
  }
}

```
---

## Related pages

- [Create and open encrypted PDFs using JavaScript](/guides/web/standalone/accessing-protected-files.md)
- [Open password-protected PDFs in JavaScript](/guides/web/features/password-protected-pdfs.md)

