Skip to content

Use Cases

Store session data with automatic expiration:

await kv.put('myapp', `session:${sessionId}`, {
userId: '123',
loginTime: Date.now()
});

Toggle features dynamically:

const { value } = await kv.get('myapp', 'config:features');
if (value.newFeature) {
// Show new feature
}

Cache API responses:

const cacheKey = `cache:api:${endpoint}`;
let data = await kv.get('myapp', cacheKey).catch(() => null);
if (!data) {
data = await fetchFromAPI(endpoint);
await kv.put('myapp', cacheKey, data);
}

Store user settings:

await kv.put('myapp', `user:${userId}:settings`, {
theme: 'dark',
notifications: true
});

Track analytics:

const date = new Date().toISOString().split('T')[0];
const key = `stats:daily:${date}`;
const { value } = await kv.get('myapp', key);
await kv.put('myapp', key, { count: (value?.count || 0) + 1 });