KV Store
A simple key-value store for application state, feature flags, counters, and caching. Supports TTL for automatic expiration.
Set
ts
await ctx.kv.set("workspace.theme", "dark");With TTL
ts
await ctx.kv.set("session:abc123", { userId: 42 }, { ttlMs: 3_600_000 }); // 1 hourExpired keys are invisible immediately on read — no need to wait for cleanup.
Get
ts
const theme = await ctx.kv.get<string>("workspace.theme");
// "dark" or null if not set / expiredDelete
ts
await ctx.kv.delete("workspace.theme");List Keys
ts
// All keys
const keys = await ctx.kv.list();
// Keys with prefix
const workspaceKeys = await ctx.kv.list({ prefix: "workspace." });
// ["workspace.theme", "workspace.language", "workspace.timezone"]Common Patterns
Feature flags
ts
await ctx.kv.set("feature:dark-mode", true);
const darkMode = await ctx.kv.get<boolean>("feature:dark-mode");
if (darkMode) {
// ...
}Rate limiting / counters
ts
const key = `ratelimit:${userId}`;
const current = await ctx.kv.get<number>(key) ?? 0;
await ctx.kv.set(key, current + 1, { ttlMs: 60_000 }); // reset after 1 minCaching
ts
const cacheKey = `cache:dashboard:${projectId}`;
const cached = await ctx.kv.get<DashboardData>(cacheKey);
if (cached) return cached;
const data = await computeDashboard(ctx, projectId);
await ctx.kv.set(cacheKey, data, { ttlMs: 300_000 }); // 5 min TTL
return data;TTL Cleanup
Expired keys are automatically filtered on read. For storage reclamation, an optional cleanup cron can be enabled in the project configuration.