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.
You can perform these tasks through the CLI or through the Cloudflare dashboard.
Prerequisites
- Sign up for a Cloudflare account ↗.
- Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
1. Create a Worker project
Create a new Worker to read and write to your KV namespace.
-
Create a new project named
kv-tutorial
by running:For 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-tutorial
directory, 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-tutorial
directory includes:- A
"Hello World"
Worker inindex.ts
. - A
wrangler.toml
configuration file.wrangler.toml
is how yourkv-tutorial
Worker accesses your kv database.
- For What would you like to start with?, choose
-
Change into the directory you just created for your Worker project:
- Log in to your Cloudflare dashboard and select your account.
- Go to your account > Workers & Pages > Overview ↗.
- Select Create.
- Select Create Worker.
- Name your Worker. For this tutorial, name your Worker
kv-tutorial
. - Select Deploy.
2. Create a KV namespace
A KV namespace is a key-value database replicated to Cloudflare’s global network.
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:
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 yourwrangler.toml
file) and the binding name you provide. ABINDING_ID
is randomly generated for you.For this tutorial, use the binding name
BINDING_NAME
.
- Go to Storage & Databases > KV ↗.
- Select Create a namespace.
- Enter a name for your namespace. For this tutorial, use
kv_tutorial_namespace
. - Select Add.
3. Bind your Worker to your KV namespace
You must create a binding to connect your Worker with your KV namespace. Bindings allow your Workers to access resources, like KV, on the Cloudflare developer platform.
To bind your KV namespace to your Worker:
-
In your
wrangler.toml
file, add the following with the values generated in your terminal from step 2: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
<BINDING_NAME>
is used to reference this KV namespace in your Worker. For this tutorial, this should beBINDING_NAME
. - 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.<BINDING_NAME>
from within your Worker.
- The value (string) you set for
- Go to Workers & Pages > Overview ↗.
- Select the
kv-tutorial
Worker you created in step 1. - Select Settings.
- Scroll to Bindings, then select Add.
- Select KV namespace.
- Name your binding (
BINDING_NAME
) in Variable name, then select the KV namespace (kv_tutorial_namespace
) you created in step 2 from the dropdown menu. - Select Deploy to deploy your binding.
4. Interact with your KV namespace
You can interact with your KV namespace via Wrangler or directly from your Workers application.
Write a value
To write a value to your empty KV namespace using Wrangler:
-
Run the
wrangler kv key put
subcommand in your terminal, and input your key and value respectively.<KEY>
and<VALUE>
are values of your choice.
Instead of using --binding
, you can also use --namespace-id
to specify which KV namespace should receive the operation:
To create a key and a value in local mode, add the --local
flag at the end of the command:
- Go to Storage & Databases > KV ↗.
- Select the KV namespace you created (
kv_tutorial_namespace
), then select View. - Select KV Pairs.
- Enter a
<KEY>
of your choice. - Enter a
<VALUE>
of your choice. - Select Add entry.
Get a value
To access the value using Wrangler:
-
Run the
wrangler kv key get
subcommand in your terminal, and input your key value:A KV namespace can be specified in two ways:
With a
--binding
With a
--namespace-id
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 view key-value pairs directly from the dashboard.
- Go to your account > Storage & Databases > KV.
- Go to the KV namespace you created (
kv_tutorial_namespace
), then select View. - Select KV Pairs.
5. Access your KV namespace from your Worker
-
In your Worker script, add your KV binding in the
Env
interface: -
Use the
put()
method onBINDING_NAME
to create a new key-value pair, or to update the value for a particular key: -
Use the KV
get()
method to fetch the data you stored in your KV database:
Your Worker code should look like this:
The code above:
- Writes a key to
BINDING_NAME
using 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:
When 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.
The browser should simply return the VALUE
corresponding to the KEY
you have specified with the get()
method.
-
Go to Workers & Pages > Overview.
-
Go to the
kv-tutorial
Worker you created. -
Select Edit Code.
-
Clear the contents of the
workers.js
file, then paste the following code.The code above:
- Writes a key to
BINDING_NAME
using 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.
The browser should simply return the
VALUE
corresponding to theKEY
you have specified with theget()
method. - Writes a key to
-
Select Save.
6. Deploy your KV
-
Run the following command to deploy KV to Cloudflare’s global network:
-
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.
-
Go to Workers & Pages > Overview.
-
Select your
kv-tutorial
Worker. -
Select Deployments.
-
From the Version History table, select Deploy version.
-
From the Deploy version page, select Deploy.
This deploys the latest version of the Worker code to production.
Summary
By finishing this tutorial, you have:
- Created a KV namespace
- Created a Worker that writes and reads from that namespace
- Deployed your project globally.
Next steps
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
kv
command documentation.