Skip to main content
Supported on
Snapchat

Storage

The storage capability is a small key-value store for your Lens, tied to the player's Snapchat account. It keeps data between launches, unlike localStorage and IndexedDB, which start empty every time the Lens opens.

Storage is available on iOS only for now. Check snapchat.isSupported('storage') rather than the platform, and keep the game playable without it.

Limits​

Each Lens has its own store for each player.

LimitValue
ValueAny JSON value, up to 32 KiB
KeyA string of 1 to 256 bytes
Total size100 KiB
Number of keys256

A write that would go over a limit rejects with ERR_QUOTA_EXCEEDED and leaves the store unchanged, so the previous value is still there. getInfo() reports how much of each limit the store uses.

Read and write​

const result = await snapchat.storage.get('save');
const save = result.found ? result.value : { level: 1, coins: 0 };

await snapchat.storage.set('save', { level: 3, coins: 120 });
  • get() resolves with found, which tells a missing key apart from a stored null.
  • set() replaces any previous value.
  • remove() deletes one key, and resolves even if the key doesn't exist.
  • clear() deletes the Lens's whole store for the current player.
  • keys() lists the stored keys, which is useful for save slots:
const { keys } = await snapchat.storage.keys();
const slots = keys.filter((key) => key.startsWith('slot:'));

When data doesn't persist​

  • ERR_STORAGE_UNAVAILABLE means there's no durable storage on this device or in this session.
  • The desktop stub keeps storage in memory, so it's gone after a reload. getInfo() reports persistenceMode: 'memory' there, and 'disk' where data persists.
const { persistenceMode } = await snapchat.storage.getInfo();
if (persistenceMode === 'memory') {
console.warn('Progress will not survive a restart here');
}

Save game​

Load the save at startup and write it at checkpoints. Treat both as optional, so the game plays on without storage:

async function loadSave() {
if (!snapchat.isSupported('storage')) return null;
try {
const result = await snapchat.storage.get('save');
return result.found ? result.value : null;
} catch (e) {
console.warn('No save:', e.message); // for example, ERR_STORAGE_UNAVAILABLE
return null;
}
}

async function writeSave(state) {
if (!snapchat.isSupported('storage')) return;
try {
await snapchat.storage.set('save', state); // any JSON up to 32 KiB
} catch (e) {
console.warn('Save failed:', e.message); // the previous save is unchanged
}
}

StorageAPI in the API reference lists every parameter and return value.

Was this page helpful?
Yes
No