JavaScript Document requestStorageAccess() Method

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

document.requestStorageAccess() is an instance method that lets third-party embeds ask for access to unpartitioned cookies and related state (see MDN Document: requestStorageAccess()). Learn the Storage Access flow, user-gesture rules, optional types / StorageAccessHandle, pairing with hasStorageAccess(), and five try-it labs.

01

Kind

Instance method

02

Returns

Promise

03

Context

3rd-party embed

04

Gesture

Often required

05

Secure

HTTPS needed

06

Status

Baseline

Introduction

Modern browsers often block third-party cookies and partition storage so trackers cannot quietly follow users across sites. Embedded widgets (chat, login, payment, comments) sometimes still need their own site’s cookies when shown inside an <iframe>.

MDN: requestStorageAccess() lets that embed request access to third-party cookies and unpartitioned state. It is part of the Storage Access API. First check with hasStorageAccess(); if access is missing, call requestStorageAccess() from a user gesture.

💡
Think: “Ask the browser for my cookies inside this iframe”

1) Feature-detect the API
2) await document.hasStorageAccess()
3) If false, on a click/tap call requestStorageAccess()
4) On grant, reload the embed so cookies are sent (MDN)

⚠️
Top-level pages vs embeds

The main use case is third-party iframes. On a normal first-party page, try-it labs still teach the API shape, but grants/denials may not match a real cross-site embed. Always test inside the iframe scenario you ship.

Related tutorials: hasStorageAccess(), hasUnpartitionedCookieAccess(), hasPrivateToken().

Understanding document.requestStorageAccess()

An instance method on Document (Storage Access API).

  • Audience — third-party embedded content (MDN).
  • Optional types — object selecting unpartitioned state (cookies, localStorage, indexedDB, …) (MDN).
  • Return — a Promise: undefined on cookie grant without types; StorageAccessHandle when types is used; rejects on deny (MDN).
  • Gesture — usually needs transient activation unless already granted (MDN).
  • Secure context — required; otherwise NotAllowedError (MDN).
  • After grant — reload the embed so unpartitioned cookies are included (MDN).
  • Policy — may be blocked by a storage-access Permissions Policy (MDN).

📝 Syntax

General forms of Document.requestStorageAccess (MDN):

JavaScript
requestStorageAccess()
requestStorageAccess(types)

Parameters

  • types Optional — an object controlling which unpartitioned state becomes accessible. Properties default to false when omitted. MDN lists flags such as all, cookies, sessionStorage, localStorage, indexedDB, locks, caches, getDirectory, estimate, createObjectURL, revokeObjectURL, BroadcastChannel, and SharedWorker.

Return value

A Promise that fulfills with undefined if third-party cookie access was granted and no types parameter was provided; fulfills with a StorageAccessHandle if types requested unpartitioned state; and rejects if access was denied (MDN).

Exceptions

  • InvalidStateError DOMException — Document not yet active, or types is provided with all properties false (MDN).
  • NotAllowedError DOMException — not a secure context, blocked by Permissions Policy, null origin, sandbox missing allow-storage-access-by-user-activation, or the user agent denies permission (MDN).

MDN basic samples

JavaScript
document.requestStorageAccess().then(
  () => {
    console.log("cookie access granted");
  },
  () => {
    console.log("cookie access denied");
  },
);

document.requestStorageAccess({ localStorage: true }).then(
  (handle) => {
    console.log("localStorage access granted");
    handle.localStorage.setItem("foo", "bar");
  },
  () => {
    console.log("localStorage access denied");
  },
);

⚡ Quick Reference

GoalCode / note
Feature-detecttypeof document.requestStorageAccess === "function"
Check firstawait document.hasStorageAccess()
Request cookiesawait document.requestStorageAccess()
Request storage handleawait document.requestStorageAccess({ localStorage: true })
Permission querynavigator.permissions.query({ name: "storage-access" }) (MDN)
After grantReload the embed (MDN)
MDN statusBaseline Widely available (Dec 2023; some parts vary)

🔍 At a Glance

Four facts about document.requestStorageAccess().

Returns
Promise

MDN

Args
types?

optional

Gesture
click/tap

usual

Status
Baseline

Dec 2023

📋 Promise outcomes & gestures

OutcomeMeaningGesture note (MDN)
Promise resolvesAccess grantedUser gesture is not consumed — script can still call other gesture APIs
Promise rejectsAccess deniedUser gesture is consumed — blocks request loops until the user accepts
Already grantedMay succeed without a new gestureStill feature-detect and handle reject
With typesResolves to StorageAccessHandleSupport for individual flags can vary

Examples Gallery

Examples follow MDN Document: requestStorageAccess(). Labs show API shape on this page; real grants belong in third-party iframes under HTTPS.

📚 Getting Started

Detect the API and understand the Promise grant/deny handlers.

Example 1 — Feature-detect first

Always check the method exists before calling.

JavaScript
const supported = typeof document.requestStorageAccess === "function";
console.log("requestStorageAccess:", supported);
console.log("secure context:", window.isSecureContext);

if (!supported) {
  console.log("Storage Access API not available here");
}
Try It Yourself

How It Works

MDN also requires a secure context. Pair detection with window.isSecureContext.

Example 2 — MDN: basic request with then/catch

Promise fulfills on grant, rejects on deny.

JavaScript
if (typeof document.requestStorageAccess !== "function") {
  console.log("unsupported");
} else {
  document.requestStorageAccess().then(
    () => {
      console.log("cookie access granted");
    },
    () => {
      console.log("cookie access denied");
    },
  );
}
Try It Yourself

How It Works

Without a gesture, many engines deny first-time requests. Use a button for production flows (next example).

📈 Practical Patterns

User gestures, check-then-request, and optional StorageAccessHandle types.

Example 3 — Request from a user gesture

MDN: first grants usually need a tap or click (transient activation).

JavaScript
const btn = document.getElementById("enable");
const log = document.getElementById("log");

btn.addEventListener("click", async () => {
  if (typeof document.requestStorageAccess !== "function") {
    log.textContent = "unsupported";
    return;
  }
  try {
    await document.requestStorageAccess();
    log.textContent = "granted — reload embed in real iframes";
  } catch (err) {
    log.textContent = "denied: " + (err && err.name ? err.name : err);
  }
});
Try It Yourself

How It Works

If the Promise rejects, MDN notes the gesture is consumed so scripts cannot spam prompts in a loop.

Example 4 — Check with hasStorageAccess, then request

Recommended Storage Access flow for embeds.

JavaScript
async function ensureStorageAccess() {
  if (typeof document.hasStorageAccess !== "function") {
    return "hasStorageAccess unsupported";
  }
  if (await document.hasStorageAccess()) {
    return "already has access";
  }
  if (typeof document.requestStorageAccess !== "function") {
    return "requestStorageAccess unsupported";
  }
  try {
    await document.requestStorageAccess();
    return "granted";
  } catch (err) {
    return "denied";
  }
}

// Call ensureStorageAccess() from a click handler
Try It Yourself

How It Works

Avoid prompting when access already exists. See hasStorageAccess().

Example 5 — Optional types & StorageAccessHandle

MDN: request specific unpartitioned state such as localStorage.

JavaScript
document.requestStorageAccess({ localStorage: true }).then(
  (handle) => {
    console.log("localStorage access granted");
    handle.localStorage.setItem("foo", "bar");
    console.log(handle.localStorage.getItem("foo"));
  },
  () => {
    console.log("localStorage access denied");
  },
);
Try It Yourself

How It Works

When types is provided, a successful call resolves with a StorageAccessHandle instead of undefined (MDN). Passing types with every property false throws InvalidStateError.

🚀 Common Use Cases

  • Embedded login / SSO widgets — restore the embedder’s cookies inside an iframe (MDN use case).
  • Chat / comment / payment embeds — personalized state blocked by default third-party cookie rules.
  • Storage Access flow — check with hasStorageAccess(), then request on click (MDN).
  • Unpartitioned storage — request localStorage / indexedDB via types where supported (MDN).
  • Permission query UX — combine with Permissions.query({ name: "storage-access" }) (MDN).
  • Post-grant reload — reload the embed so requests include unpartitioned cookies (MDN).

🧠 How requestStorageAccess() Works

1

Embed checks access

hasStorageAccess() or Permissions storage-access (MDN).

Check
2

User activates a control

Click/tap provides transient activation for a first grant (MDN).

Gesture
3

requestStorageAccess runs

Browser may prompt or apply policy / allowlist heuristics (MDN).

Request
4

Grant → reload; deny → graceful fallback

MDN: reload so unpartitioned cookies ride along on the next request.

📝 Notes

  • MDN: Baseline Widely available since December 2023; some parts may vary.
  • Not Deprecated, Experimental, or Non-standard.
  • Requires a secure context (HTTPS / localhost) (MDN).
  • May be blocked by a storage-access Permissions Policy (MDN).
  • Sandboxed iframes need allow-storage-access-by-user-activation (MDN).
  • Related: hasStorageAccess(), hasUnpartitionedCookieAccess(), hasPrivateToken().

Browser Support

Document.requestStorageAccess() is Baseline Widely available on MDN (since December 2023). Some parts of this feature may have varying levels of support (for example optional types). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.requestStorageAccess()

Storage Access API request for third-party unpartitioned cookies and related state in embeds.

Baseline Widely available
Google Chrome 119+
Yes
Mozilla Firefox 65+
Yes
Apple Safari 11.1+
Yes
Microsoft Edge 85+
Yes
Opera 105+
Yes
Internet Explorer Not supported
No
requestStorageAccess() Wide

Bottom line: Call from a user gesture in third-party iframes after hasStorageAccess() is false. Reload on grant, and always handle denial gracefully.

Conclusion

document.requestStorageAccess() is how third-party embeds ask for unpartitioned cookie (and related) access under the Storage Access API. Check first with hasStorageAccess(), request from a user gesture, reload on grant, and design a clear fallback when the Promise rejects.

Continue with requestStorageAccessFor(), hasStorageAccess(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect and require a secure context
  • Call hasStorageAccess() before prompting (MDN)
  • Trigger first requests from a click/tap handler (MDN)
  • Reload the embed after a grant (MDN)
  • Provide a non-personalized fallback when denied

❌ Don’t

  • Spam requestStorageAccess() in a reject loop (MDN gesture rules)
  • Ignore Permissions Policy / sandbox token requirements
  • Assume every types flag works in every browser
  • Treat a top-level demo as proof for cross-site iframes
  • Skip error handling for cookie / storage failures

Key Takeaways

Knowledge Unlocked

Five things to remember about requestStorageAccess()

Baseline Storage Access request for third-party embeds.

5
Core concepts
🔓02

Gesture

often needed

MDN
🔍03

Check

hasStorageAccess

first
🔄04

Grant

reload embed

MDN
🛡05

Status

Baseline

2023

❓ Frequently Asked Questions

MDN: Document.requestStorageAccess() lets content loaded in a third-party context (for example an iframe) request access to third-party cookies and unpartitioned state. It is part of the Storage Access API.
No. MDN marks Document.requestStorageAccess() as Baseline Widely available (across browsers since December 2023). Some parts of the feature may have varying support. It is not Deprecated, Experimental, or Non-standard.
Yes in the common case. MDN: requests are automatically denied unless the embed is processing a user gesture (transient activation), or permission was already granted previously.
MDN: fulfills with undefined if cookie access was granted and no types parameter was provided; fulfills with a StorageAccessHandle if types requested unpartitioned state; rejects if access was denied.
Call document.hasStorageAccess() (or hasUnpartitionedCookieAccess()). You can also use Permissions.query() with the feature name "storage-access" (MDN).
MDN: after an embed activates storage-access permission via requestStorageAccess(), it should reload itself so the browser re-requests the resource with third-party unpartitioned cookies included.
Did you know?

MDN points out that when the Promise rejects, the user gesture is deliberately consumed. That stops malicious pages from calling requestStorageAccess() in a tight loop until the user gives up and accepts a permission prompt.

Next: requestStorageAccessFor()

Learn the Deprecated Document.requestStorageAccessFor() top-level Storage Access extension and how it differs from requestStorageAccess().

requestStorageAccessFor() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful