Writing Data

KV is organized into Namespaces, each of which can store up to a billion key-value pairs. The keys and values are both allowed to be arbitrary byte sequences (with a few exceptions), but we most commonly see values stored as strings or JSON blobs.

Worker KV values are most commonly written using our API (and read in a Worker). All data is encrypted at rest with 256-bit AES-GCM, and only decrypted by the process executing your Worker scripts or responding to your API requests.

Writing Data

You will need a Cloudflare API key. If you don’t already have one, you can find it in the Cloudflare Dashboard.

You will also need your Account ID. You can find that ID in the URL of any page you view within the Cloudflare Dashboard after selecting the account which manages your Worker:

Account ID Screenshot

Once you have an API key and Account ID, the first step is to create a Namespace:

curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces" \
-X POST \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"title": "My First Namespace"}'

The response to that request will include the ID of your new Namespace. Use that ID to write your first KV pair:

curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/first-key" \
-X PUT \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_AUTH_KEY" \
--data 'My first value!'

It’s less commonly done, but you can also write from inside a Worker.

You’ve now written data to Workers KV! Learn about reading data from a Worker.