From e958e28254e464c89d78fa197dbb22f83b537e5e Mon Sep 17 00:00:00 2001 From: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com> Date: Thu, 27 Feb 2025 10:23:18 -0500 Subject: [PATCH 01/12] Update index.mdx --- src/content/docs/kv/index.mdx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 1faf23faa6b571d..b064b1f5fc96b7a 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -38,19 +38,19 @@ Access your Workers KV namespace from Cloudflare Workers using the Workers Bindi export default { async fetch(request, env, ctx): Promise { - //write a key-value pair + // write a key-value pair await env.KV_BINDING.put('KEY', 'VALUE'); // read a key-value pair const value = await env.KV_BINDING.get('KEY'); - //list all key-value pairs + // list all key-value pairs const allKeys = await env.KV_BINDING.list(); - //delete a key-value pair + // delete a key-value pair await env.KV_BINDING.delete('KEY'); - //return a Workers response + // return a Workers response return new Response( JSON.stringify({ value: value, @@ -82,6 +82,8 @@ Access your Workers KV namespace from Cloudflare Workers using the Workers Bindi ``` + +Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/ from [Cloudflare Workers](/workers) with the full [binding API reference](/kv/api/read-key-value-pairs/). ``` @@ -98,10 +100,12 @@ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namesp -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \ -H "X-Auth-Key: $CLOUDFLARE_API_KEY" ``` + +Learn more about using Workers KV's REST API from external services with the full [REST API reference](/api/resources/kv/subresources/namespaces/methods/list/). -Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a Workers project. +Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a [Cloudflare Workers](/workers) project. --- From eda93c73d499b5aaa6737a455fe408193dcfd19a Mon Sep 17 00:00:00 2001 From: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com> Date: Thu, 27 Feb 2025 10:24:10 -0500 Subject: [PATCH 02/12] Update src/content/docs/kv/index.mdx --- src/content/docs/kv/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index b064b1f5fc96b7a..b274e96caa34bf8 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -101,7 +101,7 @@ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namesp -H "X-Auth-Key: $CLOUDFLARE_API_KEY" ``` -Learn more about using Workers KV's REST API from external services with the full [REST API reference](/api/resources/kv/subresources/namespaces/methods/list/). +Learn more about using Workers KV's REST API from external services with the full [REST API and SDK reference](/api/resources/kv/subresources/namespaces/methods/list/). From 924ca34d4cadec2a947a9b6c257092c247bbd7bb Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Thu, 27 Feb 2025 15:31:25 +0000 Subject: [PATCH 03/12] Update src/content/docs/kv/index.mdx --- src/content/docs/kv/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index b274e96caa34bf8..24f5065335b910d 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -83,7 +83,7 @@ Access your Workers KV namespace from Cloudflare Workers using the Workers Bindi -Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/ from [Cloudflare Workers](/workers) with the full [binding API reference](/kv/api/read-key-value-pairs/). +Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/) from [Cloudflare Workers](/workers/) with the full [binding API reference](/kv/api/read-key-value-pairs/). ``` From a48fb4aa56fc3b126646d45f87ab8b4fc3742b50 Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Thu, 27 Feb 2025 15:33:52 +0000 Subject: [PATCH 04/12] Update src/content/docs/kv/index.mdx --- src/content/docs/kv/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 24f5065335b910d..92fd3f99ba15223 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -105,7 +105,7 @@ Learn more about using Workers KV's REST API from external services with the ful -Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a [Cloudflare Workers](/workers) project. +Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a [Cloudflare Workers](/workers/) project. --- From 6ee8092bcd1340f3f4b148900a1ced3858c930e9 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 10:33:19 -0500 Subject: [PATCH 05/12] thomasgauvin: add ts example --- src/content/docs/kv/index.mdx | 170 +++++++++++++++++++++------------- 1 file changed, 107 insertions(+), 63 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 92fd3f99ba15223..11909f599a4c0b2 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -5,7 +5,17 @@ sidebar: order: 1 --- -import { CardGrid, Description, Feature, LinkTitleCard, Plan, RelatedProduct, Tabs, TabItem, LinkButton } from "~/components"; +import { + CardGrid, + Description, + Feature, + LinkTitleCard, + Plan, + RelatedProduct, + Tabs, + TabItem, + LinkButton, +} from "~/components"; @@ -38,70 +48,104 @@ Access your Workers KV namespace from Cloudflare Workers using the Workers Bindi export default { async fetch(request, env, ctx): Promise { - // write a key-value pair - await env.KV_BINDING.put('KEY', 'VALUE'); - - // read a key-value pair - const value = await env.KV_BINDING.get('KEY'); - - // list all key-value pairs - const allKeys = await env.KV_BINDING.list(); - - // delete a key-value pair - await env.KV_BINDING.delete('KEY'); - - // return a Workers response - return new Response( - JSON.stringify({ - value: value, - allKeys: allKeys, - }), - ); - }, - }; - - ``` - - - ```json - { - "$schema": "node_modules/wrangler/config-schema.json", - "name": "WORKER-NAME", - "main": "src/index.ts", - "compatibility_date": "2025-02-04", - "observability": { - "enabled": true - }, - - "kv_namespaces": [ - { - "binding": "YOUR_BINDING", - "id": "" - } - ] - ``` - - - -Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/) from [Cloudflare Workers](/workers/) with the full [binding API reference](/kv/api/read-key-value-pairs/). + // write a key-value pair + await env.KV_BINDING.put('KEY', 'VALUE'); + + // read a key-value pair + const value = await env.KV_BINDING.get('KEY'); + + // list all key-value pairs + const allKeys = await env.KV_BINDING.list(); + + // delete a key-value pair + await env.KV_BINDING.delete('KEY'); + + // return a Workers response + return new Response( + JSON.stringify({ + value: value, + allKeys: allKeys, + }), + ); + }, + }; + + ``` + + + ```json + { + "$schema": "node_modules/wrangler/config-schema.json", + "name": "WORKER-NAME", + "main": "src/index.ts", + "compatibility_date": "2025-02-04", + "observability": { + "enabled": true + }, + + "kv_namespaces": [ + { + "binding": "YOUR_BINDING", + "id": "" + } + ] + ``` + + + +Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/) from [Cloudflare Workers](/workers) with the full [binding API reference](/kv/api/read-key-value-pairs/). + -``` -curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \ - -X PUT \ - -H 'Content-Type: multipart/form-data' \ - -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \ - -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \ - -d '{ - "value": "Some Value" - }' - -curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \ - -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \ - -H "X-Auth-Key: $CLOUDFLARE_API_KEY" -``` - -Learn more about using Workers KV's REST API from external services with the full [REST API and SDK reference](/api/resources/kv/subresources/namespaces/methods/list/). + + + + ``` + curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \ + -X PUT \ + -H 'Content-Type: multipart/form-data' \ + -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \ + -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \ + -d '{ + "value": "Some Value" + }' + + curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \ + -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \ + -H "X-Auth-Key: $CLOUDFLARE_API_KEY" + ``` + + + ```ts + const client = new Cloudflare({ + apiEmail: process.env['CLOUDFLARE_EMAIL'], // This is the default and can be omitted + apiKey: process.env['CLOUDFLARE_API_KEY'], // This is the default and can be omitted + }); + + const value = await client.kv.namespaces.values.update('', 'KEY', { + account_id: '', + value: 'VALUE', + }); + + const value = await client.kv.namespaces.values.get('', 'KEY', { + account_id: '', + }); + + const value = await client.kv.namespaces.values.delete('', 'KEY', { + account_id: '', + }); + + // Automatically fetches more pages as needed. + for await (const namespace of client.kv.namespaces.list({ account_id: '' })) { + console.log(namespace.id); + } + + ``` + + + +See the full Workers KV [REST API and SDK reference](/api/resources/kv/subresources/namespaces/methods/list/) for details on using REST API from external applications, with pre-generated SDK's for external TypeScript, Python, or Go applications. + From dc8adac92fa8854b66c40399cdddcda420844bc6 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 10:38:05 -0500 Subject: [PATCH 06/12] thomasgauvin: more nits to overview kv --- src/content/docs/kv/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 11909f599a4c0b2..257b82ed6adbd19 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -38,7 +38,7 @@ Workers KV is ideal for projects that require: - High volumes of reads and/or repeated reads to the same keys. - Globally distributed data. -Access your Workers KV namespace from Cloudflare Workers using the Workers Binding API or from your external application using the REST API: +Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings](/workers/runtime-apis/bindings/) or from your external application using the REST API: From 3477ba37a0d98c5f6a9a482f67bf642e5bbde308 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 10:41:06 -0500 Subject: [PATCH 07/12] thomasgauvin: nits --- src/content/docs/kv/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 257b82ed6adbd19..5be4d013692c71d 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -93,7 +93,7 @@ Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings -Learn more about using [Workers KV's binding](/kv/concepts/kv-bindings/) from [Cloudflare Workers](/workers) with the full [binding API reference](/kv/api/read-key-value-pairs/). +See the full [Workers KV binding API reference](/kv/api/read-key-value-pairs/). From 9b6a657e79d5245abbc393c5ffe9e2d6103be2cb Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 11:12:44 -0500 Subject: [PATCH 08/12] thomasgauvin: remove ideal, which is repeated from first paragraph --- src/content/docs/kv/index.mdx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 5be4d013692c71d..b2ef478d54f106e 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -25,7 +25,8 @@ Create a global, low-latency, key-value data storage. -Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites that support high read volumes with low latency. +Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites +that support high read volumes with low latency. For example, you can use Workers KV for: @@ -33,11 +34,6 @@ For example, you can use Workers KV for: - Storing user configurations / preferences. - Storing user authentication details. -Workers KV is ideal for projects that require: - -- High volumes of reads and/or repeated reads to the same keys. -- Globally distributed data. - Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings](/workers/runtime-apis/bindings/) or from your external application using the REST API: From b4d266fb5940c52338fdeccc8499bad1284f9059 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 11:26:23 -0500 Subject: [PATCH 09/12] thomasgauvin: typo --- src/content/docs/kv/index.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index b2ef478d54f106e..24f7ebaba54e884 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -25,8 +25,8 @@ Create a global, low-latency, key-value data storage. -Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites -that support high read volumes with low latency. +Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites +that support high read volumes with low latency. For example, you can use Workers KV for: @@ -85,6 +85,7 @@ Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings "id": "" } ] + } ``` From c29f15688f3de1de1c739a92ae98df64a1a5f06e Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Thu, 27 Feb 2025 16:44:52 +0000 Subject: [PATCH 10/12] Update src/content/docs/kv/index.mdx Removing unnecessary linebreak --- src/content/docs/kv/index.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 24f7ebaba54e884..533544762dddc71 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -25,8 +25,7 @@ Create a global, low-latency, key-value data storage. -Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites -that support high read volumes with low latency. +Workers KV is a data storage that allows you to store and retrieve data globally. With Workers KV, you can build dynamic and performant APIs and websites that support high read volumes with low latency. For example, you can use Workers KV for: From b568421ff94fb71f6974459894cce38ab3aee3a5 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 12:11:23 -0500 Subject: [PATCH 11/12] thomasgauvin: nits --- src/content/docs/kv/index.mdx | 89 +++++++++++++++++------------------ 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 533544762dddc71..5f6f0d8f58a459f 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -39,53 +39,52 @@ Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings - ```ts - export default { - async fetch(request, env, ctx): Promise { - - // write a key-value pair - await env.KV_BINDING.put('KEY', 'VALUE'); - - // read a key-value pair - const value = await env.KV_BINDING.get('KEY'); - - // list all key-value pairs - const allKeys = await env.KV_BINDING.list(); - - // delete a key-value pair - await env.KV_BINDING.delete('KEY'); - - // return a Workers response - return new Response( - JSON.stringify({ - value: value, - allKeys: allKeys, - }), - ); - }, - }; - - ``` +```ts +export default { + async fetch(request, env, ctx): Promise { + // write a key-value pair + await env.KV_BINDING.put('KEY', 'VALUE'); + + // read a key-value pair + const value = await env.KV_BINDING.get('KEY'); + + // list all key-value pairs + const allKeys = await env.KV_BINDING.list(); + + // delete a key-value pair + await env.KV_BINDING.delete('KEY'); + + // return a Workers response + return new Response( + JSON.stringify({ + value: value, + allKeys: allKeys, + }), + ); + }, +} satisfies ExportedHandler<{ KV_BINDING: KVNamespace }>; + + ``` - ```json - { - "$schema": "node_modules/wrangler/config-schema.json", - "name": "WORKER-NAME", - "main": "src/index.ts", - "compatibility_date": "2025-02-04", - "observability": { - "enabled": true - }, - - "kv_namespaces": [ - { - "binding": "YOUR_BINDING", - "id": "" - } - ] - } - ``` +```json +{ + "$schema": "node_modules/wrangler/config-schema.json", + "name": "WORKER-NAME", + "main": "src/index.ts", + "compatibility_date": "2025-02-04", + "observability": { + "enabled": true + }, + + "kv_namespaces": [ + { + "binding": "KV_BINDING", + "id": "" + } + ] +} +``` From 6cee7216812b89a3cc5ec91ab900761828ba63af Mon Sep 17 00:00:00 2001 From: Thomas Gauvin Date: Thu, 27 Feb 2025 12:17:23 -0500 Subject: [PATCH 12/12] thomasgauvin: add overview kv get started button --- src/content/docs/kv/index.mdx | 41 +++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 5f6f0d8f58a459f..6ee4bfae4187a53 100644 --- a/src/content/docs/kv/index.mdx +++ b/src/content/docs/kv/index.mdx @@ -45,28 +45,30 @@ export default { // write a key-value pair await env.KV_BINDING.put('KEY', 'VALUE'); - // read a key-value pair - const value = await env.KV_BINDING.get('KEY'); - - // list all key-value pairs - const allKeys = await env.KV_BINDING.list(); - - // delete a key-value pair - await env.KV_BINDING.delete('KEY'); - - // return a Workers response - return new Response( - JSON.stringify({ - value: value, - allKeys: allKeys, - }), - ); - }, + // read a key-value pair + const value = await env.KV_BINDING.get('KEY'); + + // list all key-value pairs + const allKeys = await env.KV_BINDING.list(); + + // delete a key-value pair + await env.KV_BINDING.delete('KEY'); + + // return a Workers response + return new Response( + JSON.stringify({ + value: value, + allKeys: allKeys, + }), + ); + }, + } satisfies ExportedHandler<{ KV_BINDING: KVNamespace }>; - ``` + ``` + ```json { "$schema": "node_modules/wrangler/config-schema.json", @@ -85,6 +87,7 @@ export default { ] } ``` + @@ -144,7 +147,7 @@ See the full Workers KV [REST API and SDK reference](/api/resources/kv/subresour -Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a [Cloudflare Workers](/workers/) project. +Get started ---