@@ -40,45 +40,25 @@ Workers KV is ideal for projects that require:
4040- High volumes of reads and/or repeated reads to the same keys.
4141- Globally distributed data.
4242
43- ** Code examples **
43+ Access your Workers KV namespace from Cloudflare Workers using the Workers Binding API or from your external application using the REST API:
4444
4545<Tabs >
46- <TabItem label = " REST API" >
47- ```
48- curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
49- -X PUT \
50- -H 'Content-Type: multipart/form-data' \
51- -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
52- -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
53- -d '{
54- "value": "Some Value"
55- }'
56-
57- curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
58- -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
59- -H "X-Auth-Key: $CLOUDFLARE_API_KEY"
60- ```
61- </TabItem >
6246<TabItem label = " Workers Binding API" >
6347``` ts
64- export interface Env {
65- YOUR_BINDING: KVNamespace ;
66- }
67-
6848export default {
6949 async fetch(request , env , ctx ): Promise <Response > {
7050
7151 // write a key-value pair
72- await env .YOUR_BINDING .put (' KEY' , ' VALUE' );
52+ await env .KV_BINDING .put (' KEY' , ' VALUE' );
7353
7454 // read a key-value pair
75- const value = await env .YOUR_BINDING .get (' KEY' );
55+ const value = await env .KV_BINDING .get (' KEY' );
7656
7757 // list all key-value pairs
78- const allKeys = await env .YOUR_BINDING .list ();
58+ const allKeys = await env .KV_BINDING .list ();
7959
8060 // delete a key-value pair
81- await env .YOUR_BINDING .delete (' KEY' );
61+ await env .KV_BINDING .delete (' KEY' );
8262
8363 // return a Workers response
8464 return new Response (
@@ -92,23 +72,20 @@ export default {
9272
9373```
9474</TabItem >
95- <TabItem label = " wrangler.json " >
96- ``` json
97- {
98- "$schema" : " node_modules/wrangler/config-schema.json " ,
99- "name" : " WORKER-NAME " ,
100- "main" : " src/index.ts " ,
101- "compatibility_date" : " 2025-02-04 " ,
102- "observability" : {
103- "enabled " : true
104- },
75+ <TabItem label = " REST API " >
76+ ```
77+ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
78+ -X PUT \
79+ -H 'Content-Type: multipart/form-data' \
80+ -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
81+ -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
82+ -d ' {
83+ "value ": "Some Value"
84+ }'
10585
106- "kv_namespaces" : [
107- {
108- "binding" : " YOUR_BINDING" ,
109- "id" : " <YOUR_BINDING_ID>"
110- }
111- ]
86+ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
87+ -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
88+ -H "X-Auth-Key: $CLOUDFLARE_API_KEY"
11289```
11390</TabItem >
11491</Tabs >
0 commit comments