# Document.requestStorageAccess()

> How requestStorageAccess() and hasStorageAccess() give a cross-site iframe its unpartitioned cookies, the user-activation rule, and the rejections.

`document.requestStorageAccess()` lets a document in a cross-site `<iframe>` request access to
its own unpartitioned cookies after the browser has blocked or partitioned them. These are the
cookies the document would see as a first party. `document.hasStorageAccess()` reports whether the
document has that access now. The API exists so that embedded sign-in, payment, and comment
widgets keep working in Firefox, Safari, and Chrome without third-party cookies being re-enabled
site-wide.

## Syntax

```js
document.hasStorageAccess()
document.requestStorageAccess()
document.requestStorageAccess(types)
document.hasUnpartitionedCookieAccess()
```

`hasStorageAccess()` and `hasUnpartitionedCookieAccess()` resolve to a boolean;
`requestStorageAccess()` resolves with no value when access is granted and rejects otherwise.
Firefox 65, Safari 11.1, and Chrome 119 support `hasStorageAccess()` and `requestStorageAccess()`;
only Chrome 125 supports the `types` argument and `hasUnpartitionedCookieAccess()` (BCD
`api.Document.requestStorageAccess`). Safari grants client-side storage access per page rather
than per frame (BCD note on `api.Document.requestStorageAccess`), so a grant obtained in one
iframe also applies to a sibling iframe of the same embedded site on that page.

## Platform support

Safari shipped the API first (11.1, 2018) and Firefox followed in 65; Chrome exposed it in 119
once third-party cookie restrictions made it necessary (BCD `api.Document.requestStorageAccess`).
The `types` extension and `hasUnpartitionedCookieAccess()` are Chrome 125+ only. Android WebView
has no implementation, so an embedded sign-in widget inside a native app's WebView falls through
to the no-API branch of the detection example.

## Parameters

| Parameter | Type | Meaning |
|---|---|---|
| `types` | object, optional (Chrome 125+) | Which unpartitioned storage to expose. Boolean members: `all`, `cookies`, `sessionStorage`, `localStorage`, `indexedDB`, `locks`, `caches`, `getDirectory`, `estimate`, `createObjectURL`, `revokeObjectURL`, `BroadcastChannel`, `SharedWorker`. Omitted: cookies only. |

With `types`, the promise resolves to a `StorageAccessHandle` whose members (`handle.localStorage`,
`handle.indexedDB`, and so on) are the unpartitioned objects; the document's own globals stay
partitioned. Without `types`, the grant applies to cookies on subsequent requests made by the
iframe and to `document.cookie`.

## Exceptions

| Exception | When |
|---|---|
| `InvalidStateError` `DOMException` | The document is not fully active, or `types` was passed with every member `false`. |
| `NotAllowedError` `DOMException` | The window is not a secure context; the `storage-access` Permissions Policy blocks the feature; the document or the top-level document has an opaque origin; the iframe is sandboxed without `allow-storage-access-by-user-activation`; the call did not run during transient user activation and no grant exists yet; the user denied the prompt; or an engine-specific check failed (Safari requires prior first-party interaction with the embedded site, Chrome applies its site-pair heuristics). |

Missing user activation is a common cause of failure. Chrome's Console message for it reads
`requestStorageAccess: Must be handling a user gesture to use.`
([storage_access_grant_permission_context.cc](https://chromium.googlesource.com/chromium/src/+/main/chrome/browser/storage_access_api/storage_access_grant_permission_context.cc),
chromium.googlesource.com). A document that already holds a grant for the current `<top-level site, embedded site>`
pair may call the method outside a gesture; each new iframe instance or tab still has to call it
once to activate the grant in that context.

## Examples

Run the code inside the embedded document; the API only takes effect there.

### Gating a sign-in widget on storage access

Check first, request only from a click, and keep the whole widget usable without cookies until the
promise resolves. The button is the activation the API needs.

```js
async function initWidget(button) {
  if (await document.hasStorageAccess()) {
    return renderSignedIn();
  }
  renderSignedOut();
  button.addEventListener('click', async () => {
    try {
      await document.requestStorageAccess();
      renderSignedIn(); // document.cookie now includes the first-party session cookie
    } catch (err) {
      if (err.name === 'NotAllowedError') renderDeniedHint();
      else throw err;
    }
  });
}
```

Safari shows a prompt naming both sites the first time; Firefox shows one and remembers the
answer for the site pair; Chrome decides without a prompt for pairs in the same Related Website
Set and prompts otherwise ([Using the Storage Access API](https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/Using),
developer.mozilla.org). The code is the same in each case.

### Reading unpartitioned localStorage through a handle

With `types`, Chrome returns a handle and leaves the globals unchanged. Use the handle for the
unpartitioned data and leave `window.localStorage` for state that should stay per top-level site.

```js
async function loadSharedPrefs() {
  const handle = await document.requestStorageAccess({ localStorage: true });
  return JSON.parse(handle.localStorage.getItem('prefs') ?? '{}');
}
```

The call still requires activation and a secure context, and it rejects with `InvalidStateError`
if every requested type is `false`; request only the types the widget reads.

### Detecting the API and degrading to partitioned state

Engines without the API have either unpartitioned third-party cookies (older Chrome with cookies
allowed) or no cross-site cookies at all. Both cases are handled by trying the cookie-dependent
path and treating failure as signed out.

```js
async function cookieAccess() {
  if (!('requestStorageAccess' in document)) {
    return 'unknown'; // no Storage Access API: proceed as if cookies may or may not arrive
  }
  if (await document.hasStorageAccess()) return 'granted';
  return 'needs-gesture'; // show a button and call requestStorageAccess() from its click
}
```

A sandboxed `<iframe>` needs `allow-storage-access-by-user-activation` together with
`allow-scripts` and `allow-same-origin` in its `sandbox` attribute, or every request rejects with
`NotAllowedError` regardless of the gesture.

:::caution
A request that reaches the activation check without transient user activation is denied. The Storage Access API specification runs its permission steps with `denied` when the document has no transient activation at that point, so the promise rejects; earlier steps, such as a top-level or same-site caller or an existing grant, can settle the request first. Chromium also logs a console error with the text `requestStorageAccess: Must be handling a user gesture to use.`, which its `storage_access_grant_permission_context.cc` adds before denying the request ([The Storage Access API](https://privacycg.github.io/storage-access/#dom-document-requeststorageaccess), privacycg.github.io, and [storage_access_grant_permission_context.cc](https://chromium.googlesource.com/chromium/src/+/main/chrome/browser/storage_access_api/storage_access_grant_permission_context.cc), chromium.googlesource.com). Call `requestStorageAccess()` from a click or similar handler.
:::

## See also

- [The Storage Access API: requestStorageAccess()](https://privacycg.github.io/storage-access/#dom-document-requeststorageaccess) (privacycg.github.io)
- [Document: requestStorageAccess() method](https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess) (developer.mozilla.org)
- [Partitioned cookies (CHIPS)](/reference/storage/partitioned-storage-chips/)
- [localStorage and sessionStorage](/reference/storage/localstorage/)
- [Credential Management API](/reference/capabilities/credential-management/)