diff --git a/src/content/docs/kv/index.mdx b/src/content/docs/kv/index.mdx index 1faf23faa6b571d..6ee4bfae4187a53 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"; @@ -23,85 +33,121 @@ 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 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: - ```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, - }), - ); - }, - }; - - ``` - - - ```json +```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": [ { - "$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": "" - } - ] - ``` - - + "binding": "KV_BINDING", + "id": "" + } + ] +} +``` + + + + +See the full [Workers KV 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" -``` + + + + ``` + 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. + -Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a Workers project. +Get started ---