0
# Storage & Data Management
1
2
Client-side storage management for persisting application data across sessions with key-value storage operations.
3
4
## Capabilities
5
6
### Storage Operations
7
8
Store, retrieve, and manage persistent data on the client side.
9
10
```typescript { .api }
11
/**
12
* Set storage value for a key
13
* @param props.key - Storage key identifier
14
* @param props.value - Value to store (string)
15
*/
16
function send(method: 'VKWebAppStorageSet', props: {
17
key: string;
18
value: string;
19
}): Promise<{ result: true }>;
20
21
/**
22
* Get storage values for specified keys
23
* @param props.keys - Array of storage keys to retrieve
24
*/
25
function send(method: 'VKWebAppStorageGet', props: {
26
keys: string[];
27
}): Promise<{
28
keys: Array<{ key: string; value: string }>;
29
}>;
30
31
/**
32
* Get list of storage keys with pagination
33
* @param props.count - Number of keys to retrieve
34
* @param props.offset - Offset for pagination
35
*/
36
function send(method: 'VKWebAppStorageGetKeys', props: {
37
count: number;
38
offset: number;
39
}): Promise<{
40
keys: string[];
41
}>;
42
```
43
44
**Usage Examples:**
45
46
```typescript
47
// Store user preferences
48
await bridge.send('VKWebAppStorageSet', {
49
key: 'user_theme',
50
value: 'dark'
51
});
52
53
await bridge.send('VKWebAppStorageSet', {
54
key: 'user_settings',
55
value: JSON.stringify({
56
notifications: true,
57
sound: false,
58
language: 'en'
59
})
60
});
61
62
// Retrieve stored data
63
const settings = await bridge.send('VKWebAppStorageGet', {
64
keys: ['user_theme', 'user_settings', 'last_visit']
65
});
66
67
settings.keys.forEach(item => {
68
console.log(`${item.key}: ${item.value}`);
69
});
70
71
// Parse JSON data
72
const userSettings = settings.keys.find(item => item.key === 'user_settings');
73
if (userSettings) {
74
const parsedSettings = JSON.parse(userSettings.value);
75
console.log('User settings:', parsedSettings);
76
}
77
78
// Get all available keys
79
const allKeys = await bridge.send('VKWebAppStorageGetKeys', {
80
count: 100,
81
offset: 0
82
});
83
84
console.log('Stored keys:', allKeys.keys);
85
```
86
87
## Storage Best Practices
88
89
### Data Serialization
90
91
```typescript
92
// Helper functions for storage
93
const storage = {
94
async setObject(key: string, value: any): Promise<void> {
95
await bridge.send('VKWebAppStorageSet', {
96
key,
97
value: JSON.stringify(value)
98
});
99
},
100
101
async getObject<T>(key: string): Promise<T | null> {
102
const result = await bridge.send('VKWebAppStorageGet', { keys: [key] });
103
const item = result.keys.find(k => k.key === key);
104
105
if (item && item.value) {
106
try {
107
return JSON.parse(item.value);
108
} catch (error) {
109
console.error('Failed to parse stored object:', error);
110
return null;
111
}
112
}
113
114
return null;
115
},
116
117
async setString(key: string, value: string): Promise<void> {
118
await bridge.send('VKWebAppStorageSet', { key, value });
119
},
120
121
async getString(key: string): Promise<string | null> {
122
const result = await bridge.send('VKWebAppStorageGet', { keys: [key] });
123
const item = result.keys.find(k => k.key === key);
124
return item ? item.value : null;
125
}
126
};
127
128
// Usage
129
await storage.setObject('app_config', {
130
version: '1.0.0',
131
features: ['feature1', 'feature2'],
132
lastUpdated: Date.now()
133
});
134
135
const config = await storage.getObject('app_config');
136
console.log('App config:', config);
137
```
138
139
### Batch Operations
140
141
```typescript
142
// Batch storage operations
143
async function saveBatchData(data: Record<string, any>): Promise<void> {
144
const promises = Object.entries(data).map(([key, value]) =>
145
bridge.send('VKWebAppStorageSet', {
146
key,
147
value: typeof value === 'string' ? value : JSON.stringify(value)
148
})
149
);
150
151
await Promise.all(promises);
152
}
153
154
async function loadBatchData(keys: string[]): Promise<Record<string, any>> {
155
const result = await bridge.send('VKWebAppStorageGet', { keys });
156
const data: Record<string, any> = {};
157
158
result.keys.forEach(item => {
159
try {
160
data[item.key] = JSON.parse(item.value);
161
} catch {
162
data[item.key] = item.value; // Keep as string if not JSON
163
}
164
});
165
166
return data;
167
}
168
```