Get started
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.
This guide instructs you through:
- Creating a KV namespace.
 - Writing key-value pairs to your KV namespace from a Cloudflare Worker.
 - Reading key-value pairs from a KV namespace.
 
To continue:
- Sign up for a Cloudflare account ↗ if you have not already.
 - Install 
npm↗. - Install 
Node.js↗. Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler requires a Node version of16.13.0or later. - Update your Wrangler installation to the most updated version.
 
Create a new Worker to read and write to your KV namespace.
- 
Create a new project named
kv-tutorialby running:Terminal window npm create cloudflare@latest -- kv-tutorialTerminal window yarn create cloudflare@latest kv-tutorialTerminal window pnpm create cloudflare@latest kv-tutorialFor setup, select the following options:
- For What would you like to start with?, choose 
Hello World example. - For Which template would you like to use?, choose 
Hello World Worker. - For Which language do you want to use?, choose 
TypeScript. - For Do you want to use git for version control?, choose 
Yes. - For Do you want to deploy your application?, choose 
No(we will be making some changes before deploying). 
This creates a new
kv-tutorialdirectory, illustrated below.Directorykv-tutorial/
Directorynode_modules/
- …
 
Directorytest/
- …
 
Directorysrc
- index.ts
 
- package-lock.json
 - package.json
 - testconfig.json
 - vitest.config.mts
 - worker-configuration.d.ts
 - wrangler.toml
 
Your new
kv-tutorialdirectory includes:- A 
"Hello World"Worker inindex.ts. - A 
wrangler.tomlconfiguration file.wrangler.tomlis how yourkv-tutorialWorker accesses your kv database. 
 - For What would you like to start with?, choose 
 - 
Change into the directory you just created for your Worker project:
Terminal window cd kv-tutorial 
A KV namespace is a key-value database replicated to Cloudflare’s global network.
You can create a KV namespace via Wrangler or the dashboard.
Wrangler allows you to put, list, get, and delete entries within your KV namespace.
To create a KV namespace via Wrangler:
- 
Open your terminal and run the following command:
Terminal window npx wrangler kv namespace create <YOUR_NAMESPACE>The
npx wrangler kv namespace create <YOUR_NAMESPACE>subcommand takes a new binding name as its argument. A KV namespace is created using a concatenation of your Worker’s name (from yourwrangler.tomlfile) and the binding name you provide. Theidis randomly generated for you.Terminal window npx wrangler kv namespace create <YOUR_NAMESPACE>🌀 Creating namespace with title <YOUR_WORKER-YOUR_NAMESPACE>✨ Success!Add the following to your configuration file:kv_namespaces = [{ binding = <YOUR_BINDING>, id = "e29b263ab50e42ce9b637fa8370175e8" }] - 
In your
wrangler.tomlfile, add the following with the values generated in your terminal:kv_namespaces = [{ binding = "<YOUR_BINDING>", id = "<YOUR_ID>" }]Binding names do not need to correspond to the namespace you created. Binding names are only a reference. Specifically:
- The value (string) you set for 
<YOUR_BINDING>is used to reference this database in your Worker. In this tutorial, name your bindingDB. - The binding must be a valid JavaScript variable name ↗. For example, 
binding = "MY_KV"orbinding = "routingConfig"would both be valid names for the binding. - Your binding is available in your Worker at 
env.<YOUR_BINDING>from within your Worker. 
 - The value (string) you set for 
 
- Log in to the Cloudflare dashboard ↗.
 - Select Workers & Pages > KV.
 - Select Create a namespace.
 - Enter a name for your namespace.
 - Select Add.
 
You can interact with your KV namespace via Wrangler or directly from your Workers application.
To write a value to your empty KV namespace using Wrangler:
- 
Run the
wrangler kv key putsubcommand in your terminal, and input your key and value respectively.<KEY>and<VALUE>are values of your choice.Terminal window npx wrangler kv key put --binding=<YOUR_BINDING> "<KEY>" "<VALUE>"Writing the value "<VALUE>" to key "<KEY>" on namespace e29b263ab50e42ce9b637fa8370175e8. 
Instead of using --binding, you can also use --namespace-id to specify which KV namespace should receive the operation:
npx wrangler kv key put --namespace-id=e29b263ab50e42ce9b637fa8370175e8 "<KEY>" "<VALUE>"Writing the value "<VALUE>" to key "<KEY>" on namespace e29b263ab50e42ce9b637fa8370175e8.To create a key and a value in local mode, add the --local flag at the end of the command:
npx wrangler kv key put --namespace-id=xxxxxxxxxxxxxxxx "<KEY>" "<VALUE>" --localTo access the value using Wrangler:
- 
Run the
wrangler kv key getsubcommand in your terminal, and input your key value:Terminal window # Replace [OPTIONS] with --binding or --namespace-idnpx wrangler kv key get [OPTIONS] "<KEY>"A KV namespace can be specified in two ways:
With a
--bindingTerminal window npx wrangler kv key get --binding=<YOUR_BINDING> "<KEY>"With a
--namespace-idTerminal window npx wrangler kv key get --namespace-id=<YOUR_ID> "<KEY>" 
You can add a --preview flag to interact with a preview namespace instead of a production namespace.
Refer to the kv bulk documentation to write a file of multiple key-value pairs to a given KV namespace.
You can access the binding from within your Worker.
- 
In your Worker script, add your KV namespace in the
Envinterface:interface Env {YOUR_KV_NAMESPACE: KVNamespace;// ... other binding types} - 
Use the
put()method onYOUR_KV_NAMESPACEto create a new key-value pair, or to update the value for a particular key:let value = await env.YOUR_KV_NAMESPACE.put(key, value); - 
Use the KV
get()method to fetch the data you stored in your KV database:let value = await env.YOUR_KV_NAMESPACE.get("KEY"); 
Your Worker code should look like this:
export interface Env {  YOUR_KV_NAMESPACE: KVNamespace;}
export default {  async fetch(request, env, ctx): Promise<Response> {    try {      await env.YOUR_KV_NAMESPACE.put("KEY", "VALUE");      const value = await env.YOUR_KV_NAMESPACE.get("KEY");      if (value === null) {        return new Response("Value not found", { status: 404 });      }      return new Response(value);    } catch (err) {      // In a production application, you could instead choose to retry your KV      // read or fall back to a default code path.      console.error(`KV returned error: ${err}`);      return new Response(err, { status: 500 });    }  },} satisfies ExportedHandler<Env>;The code above:
- Writes a key to 
YOUR_KV_NAMESPACEusing KV’sput()method. - Reads the same key using KV’s 
get()method, and returns an error if the key is null (or in case the key is not set, or does not exist). - Uses JavaScript’s 
try...catch↗ exception handling to catch potential errors. When writing or reading from any service, such as Workers KV or external APIs usingfetch(), you should expect to handle exceptions explicitly. 
To run your project locally, enter the following command within your project directory:
npx wrangler devWhen you run wrangler dev, Wrangler provides a URL (usually a localhost:8787) to review your Worker. The browser prints your value when you visit the URL provided by Wrangler.
- 
Run the following command to deploy KV to Cloudflare’s global network:
Terminal window npx wrangler deploy - 
Visit the URL for your newly created Workers KV application.
For example, if the URL of your new Worker is
kv-tutorial.<YOUR_SUBDOMAIN>.workers.dev, accessinghttps://kv-tutorial.<YOUR_SUBDOMAIN>.workers.dev/sends a request to your Worker that writes (and reads) from Workers KV. 
By finishing this tutorial, you have:
- Created a KV namespace
 - Created a Worker that writes and reads from that namespace
 - Deployed your project globally.
 
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the Cloudflare Developers community on Discord ↗.
- Learn more about the KV API.
 - Understand how to use Environments with Workers KV.
 - Read the Wrangler 
kvcommand documentation.