Skip to main content

Storage API

The Storage API lets you persist key-value data scoped to the customer across sessions. Use this API to store customer preferences, dismissed states, or other lightweight data that should survive navigation and return visits.

  • Remember customer preferences: Save choices like preferred language, display options, or notification settings so they persist across visits.
  • Track dismissed content: Store whether a customer has dismissed a promotional banner or onboarding message so it doesn't reappear.
  • Cache lightweight data: Store small pieces of data to reduce redundant API calls, such as a customer's loyalty tier or last-viewed order.
Support
Targets (24)

The shopify global object provides key-value storage scoped to the customer. Access the following properties on shopify to read, write, and delete persistent data across sessions.

Anchor to storage
storage
required

Key-value storage that persists across customer sessions for this extension target. Use this to store preferences, dismiss states, or cached data without requiring a backend call.

Examples

jsx

import '@shopify/ui-extensions/preact';
import {render} from 'preact';
import {useEffect, useState} from 'preact/hooks';

export default async () => {
render(<Extension />, document.body);
};

function Extension() {
const [preference, setPreference] =
useState(null);

useEffect(() => {
async function loadPreference() {
const stored =
await shopify.storage.read('locale_pref');
if (stored) {
setPreference(stored);
}
}
loadPreference();
}, []);

async function handleSave(value) {
await shopify.storage.write(
'locale_pref',
value,
);
setPreference(value);
}

return (
<s-stack direction="block" gap="base">
<s-text>
{preference
? `Saved preference: ${preference}`
: 'No preference saved'}
</s-text>
<s-button onClick={() => handleSave('en-US')}>
Set preference to en-US
</s-button>
</s-stack>
);
}

  • Store only small values: Keep stored data lightweight. Storage is intended for simple key-value pairs like preferences and flags, not large datasets.
  • Use descriptive keys: Name your storage keys clearly (for example, promo_dismissed or locale_pref) so their purpose is obvious and conflicts with other extensions are unlikely.
  • Handle missing values gracefully: Always check for null or undefined when reading from storage, since the value may not exist on the customer's first visit.
  • Don't store sensitive data: Storage isn't encrypted. Don't store personal information, tokens, or anything that could compromise customer privacy.

  • Storage is scoped per customer and per extension. You can't share data between different extensions or access another customer's stored values.
  • On the pre-authenticated Order status page, storage is scoped to the customer associated with the order, not the browsing session.
  • All values are stored as strings. You must serialize and deserialize complex types (like JSON objects).

Was this page helpful?