Bind a KV store to Cloudflare Worker

InstructorJon Meyers

Share this video with your friends

Send Tweet

KV Storage is a cache that Cloudflare makes available within our Workers. It replicates across multiple CDN nodes, making it a super performant way to cache data.

In this lesson, we create a new KV store using the Wrangler CLI, and bind it to our Cloudflare worker using the wrangler.toml configuration file.

Additionally, we create a preview store to use in development mode.

Code Snippets

Create a KV Store

npx wrangler kv:namespace create "ARTICLES"

Create a preview KV Store

npx wrangler kv:namespace create "ARTICLES" --preview

Bind KV to Cloudflare Worker

kv_namespaces = [
  { binding = "replace-with-your-kv-name", id = "replace-with-your-kv-production-id", preview_id = "replace-with-your-kv-preview-id" }
]

Run wrangler development server

npx wrangler dev

Resources