Skip to main content

Command Palette

Search for a command to run...

Chrome Extension Storage API: Saving and Retrieving Data

Updated
3 min read
Chrome Extension Storage API: Saving and Retrieving Data
M

Easily monetize your browser extensions. Mellowtel is an open-source, ethical monetization engine that respects user privacy and maximizes your revenue.

The Chrome Extension Storage API provides a way to store, retrieve, and track changes to user data in your extension. This guide will walk you through using the Storage API in your Chrome extension.

What is the Chrome Extension Storage API?

The Storage API allows you to store and retrieve data in a way that's optimized for extension performance. It provides two main types of storage:

  1. chrome.storage.sync: Syncs data across all Chrome browsers that the user is logged into.

  2. chrome.storage.local: Stores data locally on the user's device.

If you're new to Chrome extension development, start with our guide on How to Create Your First Chrome Extension.

Using the Storage API

1. Request Permission

First, request permission to use the Storage API in your manifest.json:

{
  "permissions": ["storage"]
}

2. Saving Data

To save data, use the set method:

chrome.storage.sync.set({key: value}, function() {
  console.log('Value is set to ' + value);
});

3. Retrieving Data

To retrieve data, use the get method:

chrome.storage.sync.get(['key'], function(result) {
  console.log('Value currently is ' + result.key);
});

4. Listening for Changes

You can listen for changes to stored data:

chrome.storage.onChanged.addListener(function(changes, namespace) {
  for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
    console.log(
      `Storage key "${key}" in namespace "${namespace}" changed.`,
      `Old value was "${oldValue}", new value is "${newValue}".`
    );
  }
});

Storage Limits

It's important to be aware of the storage limits when using the Chrome Extension Storage API:

chrome.storage.sync

  • Total storage limit: 100KB of data synced across devices

  • Per-item limit: 8KB

  • Maximum number of items: 512

chrome.storage.local

  • Total storage limit: 10MB (may be less on some devices)

  • Per-item limit: 5MB

  • No limit on the number of items

Be mindful of these limits when designing your extension. If you need to store larger amounts of data, consider using chrome.storage.local or even IndexedDB for very large datasets.

Best Practices

  1. Use sync storage for user preferences and small amounts of data.

  2. Use local storage for larger amounts of data or data that doesn't need to be synced.

  3. Be mindful of storage limits: sync has stricter limits than local.

  4. Always handle errors and edge cases when reading from or writing to storage.

Using Storage in Different Contexts

The Storage API can be used in various parts of your extension:

Making API Calls with Stored Data

You might want to use stored data when Making API Calls in Chrome Extensions, such as using stored API keys or user preferences to customize API requests.

Conclusion

The Chrome Extension Storage API is a powerful tool for managing data in your extension. By following this guide, you've learned how to save, retrieve, and listen for changes to stored data.

As you develop more complex extensions, consider how you can use the Storage API in conjunction with other extension features to create a seamless user experience.