> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cskn.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Storage

> Key-value storage scoped to your app, plain or encrypted.

Both stores are scoped to your app: you can only see your own keys, and no other app can see them.

## Storage

For any JSON value: preferences, drafts, caches.

```tsx theme={null}
import { useStorage } from '@cskn/sdk';

function ThemeToggle() {
  const storage = useStorage();

  const save = () => storage.set('theme', { mode: 'dark' });
  const load = async () => (await storage.get('theme')) ?? { mode: 'light' };

  return <button onClick={save}>Dark mode</button>;
}
```

| Hook method | Function | Returns |
| - | - | - |
| `set(key, value)` | `storageSet` | `void` |
| `get(key)` | `storageGet` | `Promise<any>`, `null` when the key is missing |
| `remove(key)` | `storageRemove` | `void` |
| `keys()` | `storageKeys` | `Promise<string[]>` |
| `clear()` | `storageClear` | `void`, removes only your app's keys |

## Secure storage

For small secrets such as a PIN or a token. Values are strings, encrypted with the iOS Keychain or the Android Keystore. On iOS they can survive the app being uninstalled.

```tsx theme={null}
import { useSecureStorage } from '@cskn/sdk';

const secure = useSecureStorage();
secure.set('pin', '1234');
const pin = await secure.get('pin'); // '1234' | null
secure.remove('pin');
```

Functions: `secureStorageSet`, `secureStorageGet`, `secureStorageRemove`.

## In a browser

There is no store: writes do nothing, `get` resolves `null` and `keys` resolves `[]`, as if the store were empty. Fall back to `localStorage`, and keep secrets out of browser storage entirely:

```tsx theme={null}
import { isAvailable, storageGet, storageSet } from '@cskn/sdk';

export const prefs = {
  async get<T>(key: string): Promise<T | null> {
    if (isAvailable()) return storageGet(key);
    const raw = localStorage.getItem(key);
    return raw ? (JSON.parse(raw) as T) : null;
  },
  set(key: string, value: unknown) {
    if (isAvailable()) storageSet(key, value);
    else localStorage.setItem(key, JSON.stringify(value));
  },
};
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.