StorageArea.set()

Stores one or more items in the storage area or updates stored items.

When you store or update a value using this API, the storage.onChanged event fires.

Note that when storing items in the sync storage area, the browser enforces quotas on the amount of data each extension can store. See Storage quotas for sync data.

This is an asynchronous function that returns a Promise.

Syntax

js
let settingItem = browser.storage.<storageType>.set(
  keys             // object
)

Where <storageType> is one of the writable storage types: storage.local, storage.session, or storage.sync.

Parameters

keys

An object containing one or more key/value pairs to be stored. If an item is in storage, its value is updated.

Values can be primitive (such as a number, boolean, or string), Array, or Object types.

It's generally not possible to store other types, such as Function, Date, RegExp, Set, Map, ArrayBuffer, and so on. Some unsupported types restore as an empty object, while others cause set() to throw an error. The behavior is browser-specific.

Note: If you want to remove keys from storage, use storage.storageArea.remove. If you want to overwrite a value with a void value, use null, i.e., key: null.

Return value

A Promise that is fulfilled with no arguments if the operation succeeds. If the operation fails, the promise is rejected with an error message.

Examples

js
function setItem() {
  console.log("OK");
}

function gotKitten(item) {
  console.log(`${item.kitten.name} has ${item.kitten.eyeCount} eyes`);
}

function gotMonster(item) {
  console.log(`${item.monster.name} has ${item.monster.eyeCount} eyes`);
}

function onError(error) {
  console.log(error);
}

// define 2 objects
let monster = {
  name: "Kraken",
  tentacles: true,
  eyeCount: 10,
};

let kitten = {
  name: "Moggy",
  tentacles: false,
  eyeCount: 2,
};

// store the objects
browser.storage.local.set({ kitten, monster }).then(setItem, onError);

browser.storage.local.get("kitten").then(gotKitten, onError);
browser.storage.local.get("monster").then(gotMonster, onError);

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
set
Chrome – Full support
Chrome 19 (Release date: 2012-05-15)
footnote Full support
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote
footnote storage is limited to 1MB per value.
Firefox – Full support
Firefox 45 (Release date: 2016-03-08)
footnote Full support
Opera – Full support
Opera 33 (Release date: 2015-10-27)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 48 (Release date: 2016-08-02)
footnote
footnote Doesn't support the managed storage area.
footnote Data isn't synchronized with the user's Mozilla account and the storage quota limits are not enforced in the sync storage area. See bug 1625257
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

Note: This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.