Skip to content

KV namespaces

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

Bind your KV namespaces in your Worker configuration file or via the Cloudflare dashboard.

Jurisdictions

Namespaces can optionally be restricted to a jurisdiction to durably store data only within a specific region. Refer to Data location for more information.

Bind your KV namespace in configuration

To bind KV namespaces to your Worker in cloudflare.config.ts, add a property to worker.env using bindings.kv(). The property key is the binding name used to refer to the KV namespace in your Worker:

  • id string required

    • The ID of the KV namespace.
  • dev object optional

    • Local development options. Set dev: { remote: true } to connect to the remote KV namespace during cf dev.

cloudflare.config.ts does not have a preview_id field. During cf dev, bindings use local storage by default unless you set dev: { remote: true }. To use a different namespace id for Worker Previews or modes, pass a function to defineConfig(({ mode, isPreview }) => ...).

Example:

cloudflare.config.tsts
import { bindings, defineConfig } from "cf/config";
import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };

export default defineConfig({
	worker: {
		name: "my-worker",
		entrypoint,
		compatibilityDate: "<COMPATIBILITY_DATE>",
		env: {
			TEST_NAMESPACE: bindings.kv({ id: "<TEST_ID>" }),
		},
	},
});

To bind KV namespaces to your Worker in your Wrangler configuration file, assign an array of objects with the following properties to the kv_namespaces key:

  • binding string required

    • The binding name used to refer to the KV namespace.
  • id string required

    • The ID of the KV namespace.
  • preview_id string optional

    • The ID of the KV namespace used during wrangler dev.

Example:

{
	"kv_namespaces": [
		{
			"binding": "<TEST_NAMESPACE>",
			"id": "<TEST_ID>"
		}
	]
}
[[kv_namespaces]]
binding = "<TEST_NAMESPACE>"
id = "<TEST_ID>"

Bind your KV namespace via the dashboard

To bind the namespace to your Worker in the Cloudflare dashboard:

  1. In the Cloudflare dashboard, go to the Workers & Pages page.

    Go to Workers & Pages ↗
  2. Select your Worker.

  3. Select Settings > Bindings.

  4. Select Add.

  5. Select KV Namespace.

  6. Enter your desired variable name (the name of the binding).

  7. Select the KV namespace you wish to bind the Worker to.

  8. Select Deploy.

Was this helpful?