Read and write data in Workers KV from a Cloudflare Worker, including expiring keys, per-key metadata, cache-tuned reads, and paginated key listing.

domain: developers.cloudflare.com · 7 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Bind a KV namespace to your Worker via kv_namespaces in the wrangler config, then access it in code as env.NAMESPACE. Docs: https://developers.cloudflare.com/kv/api/write-key-value-pairs/
  2. Write a pair with await env.NAMESPACE.put(key, value, options?). options supports expiration (seconds since UNIX epoch), expirationTtl (seconds from now, minimum 60), and metadata (a JSON-serializable object, max 1024 bytes serialized). Keys max 512 bytes, values max 25 MiB. https://developers.cloudflare.com/kv/api/write-key-value-pairs/
  3. Read with await env.NAMESPACE.get(key, { cacheTtl, type }); cacheTtl (seconds, minimum 30) sets how long the value is cached at that edge location. Use getWithMetadata(key) to fetch metadata alongside the value. https://developers.cloudflare.com/kv/api/read-key-value-pairs/
  4. List keys with await env.NAMESPACE.list({ prefix, limit, cursor }); prefix filters by key prefix, limit defaults to and caps at 1000, response includes list_complete and cursor. https://developers.cloudflare.com/kv/api/list-keys/
  5. Paginate by re-calling list({ cursor: value.cursor }) while list_complete is false; do not treat an empty keys array as 'done' since list_complete is the authoritative signal. https://developers.cloudflare.com/kv/api/list-keys/
  6. Design around eventual consistency: writes are visible immediately in the writing location but can take up to 60s (or cacheTtl) to propagate globally, and concurrent writes to the same key resolve last-write-wins. https://developers.cloudflare.com/kv/api/write-key-value-pairs/
  7. Throttle writes to any single key to at most 1 per second; catch and back off on 429 'Too Many Requests' errors rather than issuing parallel writes to the same key. https://developers.cloudflare.com/kv/api/write-key-value-pairs/

Known gotchas

Related routes

Deploy a Cloudflare Worker with KV and secrets via Wrangler
cloudflare.com · 4 steps · unrated
Read and write data in Workers KV from a Cloudflare Worker, including expiring keys, per-key metadata, cache-tuned reads, and paginated key listing.
developers.cloudflare.com · 7 steps · unrated
Read, write, list and delete Cloudflare Workers KV keys via the REST API, including bulk operations, expiration and metadata
developers.cloudflare.com · 9 steps · unrated

Give your agent this knowledge — and 16,900+ more routes

One MCP install gives any agent live access to the full route map across 5,900+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans