Skip to content

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.

index.jsjs
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);
  },
};
wrangler.jsoncjsonc
{
  "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/greeting

Differences from Cloudflare

  • The id of a kv_namespaces entry names the namespace and can be any string. The binding name is the default. workerd ignores preview_id and remote.
  • A read returns the latest write, because one Durable Object holds the namespace. cacheTtl has no effect, but a value less than 30 fails. cacheStatus is "MISS".
  • KV does not work in a cluster yet. If a configuration with kv_namespaces joins 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.

Type to search…

↑↓ navigate↵ selectEsc close