Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Key-Value Namespace Bindings, API Methods, CLI, and Local Development

How Workers KV namespaces, bindings, get/put/list/delete, Wrangler CLI, local dev, and REST access fit together for read-heavy edge storage.

This page summarizes Cloudflare Workers KV documentation: a global, low-latency key-value store you reach from Workers and Pages Functions through bindings, from your laptop through Wrangler, and from other systems through the REST API.

Workers KV is key-value storage replicated across Cloudflare’s network, aimed at high read volume and low-latency reads from Workers at the edge.

Create a global, low-latency, key-value data storage.

— Cloudflare, Workers KV

Workers KV provides low-latency, high-throughput global storage to your Cloudflare Workers applications. Workers KV is ideal for storing user configuration data, routing data, A/B testing configurations and authentication tokens, and is well suited for read-heavy workloads.

— Cloudflare, Get started

Typical uses in the docs include caching API responses, user or tenant configuration, authentication-related data, and serving or generating content from stored values.

Need Workers KV Workers Cache API (contrast)
Data survives after writes Yes; persists until expiry or deletion Ephemeral; can be evicted
Readable across locations Yes, globally Tied to originating data center
Good fit Config, cacheable reads, tokens Per-PoP HTTP response cache

Workers KV is an eventually consistent, global datastore. Any writes within a region are immediately reflected within that same region but will not be immediately available in other regions. However, those writes will eventually be available everywhere and, at that point, Workers KV guarantees that data within each region will be consistent.

— Cloudflare, Build a todo list Jamstack application

Reads can also be briefly stale after writes elsewhere: get() and getWithMetadata() may return stale values, with updates sometimes taking up to 60 seconds (or the cacheTtl you set on read). See Read key-value pairs.

KV namespace and KV binding: how connection works

Section titled “KV namespace and KV binding: how connection works”

A KV namespace is the account-level database (a title you choose plus an assigned namespace ID). A binding is the JavaScript identifier in your Worker that points at that namespace’s ID.

A KV namespace is a key-value database replicated to Cloudflare’s global network.

— Cloudflare, KV bindings

KV bindings allow for communication between a Worker and a KV namespace.

— Cloudflare, KV bindings

The name of your binding does not need to match the KV namespace’s name. Instead, the binding should be a valid JavaScript identifier, because the identifier will exist as a global variable within your Worker.

— Cloudflare, KV bindings

In ES modules Workers, you use env.<BINDING_NAME> (not a global). See Migrate from Service Workers to ES Modules.

Workers KV stores data in centralized data centers and caches it at edge locations after access:

Workers KV is a global, low-latency, key-value data store. It stores data in a small number of centralized data centers, then caches that data in Cloudflare’s data centers after access.

— Cloudflare, Wrangler configuration

From a project with account_id in the Wrangler file, create a namespace (binding name is the argument):

npx wrangler kv namespace create USERS_NOTIFICATION_CONFIG

The npx wrangler kv namespace create <BINDING_NAME> subcommand takes a new binding name as its argument. A KV namespace is created using a concatenation of your Worker’s name (from your Wrangler file) and the binding name you provide.

— Cloudflare, Get started

For a separate preview namespace during development:

npx wrangler kv namespace create "TODOS" --preview

See Wrangler KV commands and KV namespaces.

Optional KV Instant namespaces (private beta) use --experimental-mode instant on create; see Get started and limits noted there.

Bind a KV namespace in Wrangler or the dashboard

Section titled “Bind a KV namespace in Wrangler or the dashboard”

Add kv_namespaces to your Wrangler configuration (configuration reference):

{
"kv_namespaces": [
{
"binding": "USERS_NOTIFICATION_CONFIG",
"id": "<NAMESPACE_ID>",
"preview_id": "<PREVIEW_NAMESPACE_ID>"
}
]
}
Field Role
binding Name you use in code as env.USERS_NOTIFICATION_CONFIG
id Production namespace ID
preview_id Optional; used by wrangler dev for preview namespace (legacy configuration notes)

Pages Functions: configure the same way in the Wrangler file, or in the dashboard under Settings → Bindings → Add → KV namespace, then redeploy (Pages Functions bindings). Access in a Function via context.env.<BINDING_NAME>.

Workers for Platforms: user Workers can get a namespace via the Cloudflare API following the same binding pattern.

KV API in Worker code: get, put, list, and delete

Section titled “KV API in Worker code: get, put, list, and delete”

From a bound namespace, the runtime exposes KVNamespace methods. Overview from Workers KV:

  • get — read one or more keys
  • put — create or update
  • list — list keys (paginated)
  • delete — remove a key

get() and getWithMetadata(): read keys from KV

Section titled “get() and getWithMetadata(): read keys from KV”

To get the value for a given key, call the get() method of the KV binding on any KV namespace you have bound to your Worker code

— Cloudflare, Read key-value pairs

Single key: resolves to the value or null if missing. Up to 100 keys in one call returns a Map (multi-key get does not support arrayBuffer or stream; use parallel single-key reads if needed).

Optional type: text (default), json, arrayBuffer, stream. Optional cacheTtl (minimum 30 seconds) controls edge caching of read results.

getWithMetadata() returns { value, metadata } when you store metadata on put.

Example pattern from Get started:

const value = await env.USERS_NOTIFICATION_CONFIG.get("user_2");
if (value === null) {
return new Response("Value not found", { status: 404 });
}

To create a new key-value pair, or to update the value for a particular key, call the put() method of the KV binding on any KV namespace you have bound to your Worker code

— Cloudflare, Write key-value pairs

Limits from the same page:

  • Key: max 512 bytes; cannot be empty or exactly . or ..
  • Value: max 25 MiB (string, ReadableStream, or ArrayBuffer)
  • expirationTtl: minimum 60 seconds; metadata JSON up to 1024 bytes

Bulk writes (up to 10,000 pairs per request, whole request under 100 MB) are available via Wrangler or REST only—not through the binding put() (Write key-value pairs, Delete key-value pairs).

list(): list keys with prefix and pagination

Section titled “list(): list keys with prefix and pagination”

To list all the keys in your KV namespace, call the list() method of the KV binding

— Cloudflare, List keys

Options include prefix, limit (default 1000, max 1000), and cursor. Keys sort in lexicographic UTF-8 order. Use list_complete, not an empty keys array, to know if more pages exist.

For small values, storing data in metadata on put(key, "", { metadata: { value } }) can be more efficient than list() plus many get() calls (List keys).

await env.NAMESPACE.delete("first-key");

See Delete key-value pairs.

Wrangler kv commands: manage data outside Worker code

Section titled “Wrangler kv commands: manage data outside Worker code”

Wrangler manages namespaces and keys from the project root with account_id configured (Wrangler KV commands, legacy kv commands).

Target a namespace with --binding=<BINDING> or --namespace-id=<ID> (exactly one required). Optional --env, --preview, --local, --remote.

Put, get, list, and bulk upload from the terminal

Section titled “Put, get, list, and bulk upload from the terminal”

Single key (local dev storage):

npx wrangler kv key put "user_1" "enabled" --binding=USERS_NOTIFICATION_CONFIG --local

Read back:

npx wrangler kv key get "user_1" --binding=USERS_NOTIFICATION_CONFIG --text

Bulk JSON file (Adding local data):

npx wrangler kv bulk put pairs.json --binding=MY_KV --local

Bulk entry shape (legacy put documentation): each object needs key and value (string); optional expiration_ttl (≥ 60), expiration, base64.

Upload from file without putting secrets in shell history:

npx wrangler kv key put "key" --path value.txt --binding=MY_KV

List with prefix:

npx wrangler kv key list --binding=MY_KV --prefix="public"

Local development: local KV vs remote namespace

Section titled “Local development: local KV vs remote namespace”

When using Wrangler in the default local development mode, files will be written to local storage instead of the preview or production namespace.

— Cloudflare, Wrangler configuration

When using wrangler dev to develop locally, Wrangler defaults to using a local version of KV to avoid interfering with any of your live production data in KV. This means that reading keys that you have not written locally returns null.

— Cloudflare, Get started

Seed local data with --local on kv key put / kv bulk put (Adding local data). To write to the remote namespace from CLI, add --remote (Get started).

Pages local dev: run npx wrangler pages dev with Wrangler config, or pass --kv=TODO_LIST (CLI overrides config if both are set) (Pages Functions bindings).

For tests, Miniflare can declare kvNamespaces and preload values via getKVNamespace().

REST API and external writers: sync config into KV

Section titled “REST API and external writers: sync config into KV”

Workers are not the only writers. Workers KV documents REST PUT/GET/DELETE on namespace values and SDK usage. The distributed configuration example reads a database in Node.js and calls client.kv.namespaces.bulkUpdate() with retries—useful when the source of truth stays outside KV and Workers only read config at the edge.

That pattern matches caching: fetch on miss, put with expirationTtl on hit path (Cache data with Workers KV).

  • Expecting immediate global read-after-write — design for eventual consistency; do not use KV as a strongly consistent primary database for cross-region writes (Jamstack tutorial).
  • Using production namespace during wrangler dev — use preview_id or local --local storage; preview without preview_id risks writing production data (legacy commands).
  • Assuming list() returned everything — paginate with cursor while list_complete is false (List keys).
  • Storing JSON without stringifying in bulk uploads — bulk value must be a string; stringify JSON objects (legacy bulk put).
  • Relying on binding put() for large batch loads — use wrangler kv bulk put or REST bulk (10k pairs cap) instead (Write key-value pairs).
  • Forgetting to redeploy after dashboard binding changes on Pages (Pages Functions bindings).

Further examples: KV examples, tutorials, Workers KV from Rust, KV from Durable Objects.

  1. Confirm the namespace exists in the Workers KV area of the Cloudflare dashboard (KV Pairs tab) or with npx wrangler kv key list --binding=<BINDING> --remote.

  2. Verify the Worker or Pages project lists the binding under Workers & Pages → Settings → Bindings, matching binding and id in your Wrangler configuration.

  3. Write a test key with npx wrangler kv key put "test-key" "ok" --binding=<BINDING> --remote, then read it with npx wrangler kv key get "test-key" --binding=<BINDING> --text --remote (Get started).

  4. Deploy the Worker, call a route that uses env.<BINDING>.get(), and confirm behavior matches local tests; handle null for missing keys (Read key-value pairs).

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you question so it can be corrected.