KV is a key-value store that a Worker reaches through a binding. workerd
keeps each namespace in one Durable Object, and each value over 64 KiB in
a separate file. Both are in .wrangler/state/workerd/kv/. Read the
Cloudflare KV documentation
for the API.
Example
The KV example reads, writes, and deletes values in a KV namespace.
export default {
async fetch(request, env) {
const key = new URL(request.url).pathname.slice(1);
if (!key) return new Response("Use /KEY.", { status: 400 });
if (request.method === "PUT") {
await env.VALUES.put(key, request.body);
return new Response(null, { status: 204 });
}
if (request.method === "DELETE") {
await env.VALUES.delete(key);
return new Response(null, { status: 204 });
}
const value = await env.VALUES.get(key, "stream");
return value === null
? new Response("Not found.", { status: 404 })
: new Response(value);
},
};{
"name": "kv",
"main": "index.js",
"compatibility_date": "2026-01-01",
"no_bundle": true,
"kv_namespaces": [
{
"binding": "VALUES",
"id": "example-values"
}
]
}KV is experimental, so it needs --experimental:
npx workerd serve --experimental ./kv/wrangler.jsonc
curl -X PUT --data 'hello' http://localhost:8787/greeting
curl http://localhost:8787/greeting
curl -X DELETE http://localhost:8787/greetingDifferences from Cloudflare
- The
idof akv_namespacesentry names the namespace and can be any string. The binding name is the default. workerd ignorespreview_idandremote. - A read returns the latest write, because one Durable Object holds the
namespace.
cacheTtlhas no effect, but a value less than 30 fails.cacheStatusis"MISS". - KV does not work in a cluster yet. If a configuration with
kv_namespacesjoins a cluster with--cluster-addr, workerd does not start. - A namespace has the write throughput of one Durable Object. Use more namespaces to increase it.
See also Cloudflare compatibility.