Create a Cloudflare Workers KV Namespace

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 weeks ago

Now that we have our Workers project up and running, we need to create a KV namespace. A KV namespace is a key-value database that is replicated to Cloudflare's edge. Using the terminal, we will create our namespace and add it to our wrangler.toml file.

Instructor: [0:00] Now, to work with Workers KV, we need to start by creating a KV namespace. A KV namespace is the actual data store where all of our keys and values are stored. Basically, the two things we need to do are, first, create a new KV namespace, and then second, add it to our Workers Project using our wrangler.toml file.

[0:19] Open up the terminal and run wrangler kv:namespace create, and then give it a KV namespace name. I'll call this my_first_kv. You'll see it says, "Creating namespace with the title, my-kv-project-my_first_kv."

[0:36] The structure here is, your project name, which is my-kv-project, and then the name of the KV namespace itself, which is my_first_kv. The second part here says, "Success! Add the following to your configuration file, kv_namespaces," which is an array, and then, inside of that, this KV namespace object, which has a binding, my_first_kv, and an ID, which is the KV namespace ID.

[1:03] I'm just going to take this, I'm going to copy it, and in wrangler.toml, I'm just going to paste that in here. I'll also just clean up the spacing a little bit. You can see, I now have kv_namespaces equals an array with my_first_kv, as well as the KV namespace ID.

[1:21] That's all we need to do to add KV namespaces to our Workers Project. Any time that we publish this project, the KV namespaces and bindings associated to it will be updated, and we'll be able to make use of this KV namespace directly inside of our code.