@@ -5,7 +5,17 @@ sidebar:
55 order : 1
66---
77
8- import { CardGrid , Description , Feature , LinkTitleCard , Plan , RelatedProduct , Tabs , TabItem , LinkButton } from " ~/components" ;
8+ import {
9+ CardGrid ,
10+ Description ,
11+ Feature ,
12+ LinkTitleCard ,
13+ Plan ,
14+ RelatedProduct ,
15+ Tabs ,
16+ TabItem ,
17+ LinkButton ,
18+ } from " ~/components" ;
919
1020<Description >
1121
@@ -38,70 +48,104 @@ Access your Workers KV namespace from Cloudflare Workers using the Workers Bindi
3848 export default {
3949 async fetch(request , env , ctx ): Promise <Response > {
4050
41- // write a key-value pair
42- await env .KV_BINDING .put (' KEY' , ' VALUE' );
43-
44- // read a key-value pair
45- const value = await env .KV_BINDING .get (' KEY' );
46-
47- // list all key-value pairs
48- const allKeys = await env .KV_BINDING .list ();
49-
50- // delete a key-value pair
51- await env .KV_BINDING .delete (' KEY' );
52-
53- // return a Workers response
54- return new Response (
55- JSON .stringify ({
56- value: value ,
57- allKeys: allKeys ,
58- }),
59- );
60- },
61- };
62-
63- ```
64- </TabItem >
65- <TabItem label = " wrangler.jsonc" >
66- ``` json
67- {
68- "$schema" : " node_modules/wrangler/config-schema.json" ,
69- "name" : " WORKER-NAME" ,
70- "main" : " src/index.ts" ,
71- "compatibility_date" : " 2025-02-04" ,
72- "observability" : {
73- "enabled" : true
74- },
75-
76- "kv_namespaces" : [
77- {
78- "binding" : " YOUR_BINDING" ,
79- "id" : " <YOUR_BINDING_ID>"
80- }
81- ]
82- ```
83- </TabItem>
84- </Tabs>
85-
86- 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/).
51+ // write a key-value pair
52+ await env .KV_BINDING .put (' KEY' , ' VALUE' );
53+
54+ // read a key-value pair
55+ const value = await env .KV_BINDING .get (' KEY' );
56+
57+ // list all key-value pairs
58+ const allKeys = await env .KV_BINDING .list ();
59+
60+ // delete a key-value pair
61+ await env .KV_BINDING .delete (' KEY' );
62+
63+ // return a Workers response
64+ return new Response (
65+ JSON .stringify ({
66+ value: value ,
67+ allKeys: allKeys ,
68+ }),
69+ );
70+ },
71+ };
72+
73+ ```
74+ </TabItem >
75+ <TabItem label = " wrangler.jsonc" >
76+ ``` json
77+ {
78+ "$schema" : " node_modules/wrangler/config-schema.json" ,
79+ "name" : " WORKER-NAME" ,
80+ "main" : " src/index.ts" ,
81+ "compatibility_date" : " 2025-02-04" ,
82+ "observability" : {
83+ "enabled" : true
84+ },
85+
86+ "kv_namespaces" : [
87+ {
88+ "binding" : " YOUR_BINDING" ,
89+ "id" : " <YOUR_BINDING_ID>"
90+ }
91+ ]
92+ ```
93+ </TabItem>
94+ </Tabs>
95+
96+ 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/).
97+
8798</TabItem>
8899<TabItem label="REST API">
89- ```
90- curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
91- -X PUT \
92- -H 'Content-Type: multipart/form-data' \
93- -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
94- -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
95- -d '{
96- "value" : " Some Value"
97- }'
98-
99- curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
100- -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
101- -H "X-Auth-Key: $CLOUDFLARE_API_KEY"
102- ```
103-
104- 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/ ) .
100+
101+ <Tabs>
102+ <TabItem label="cURL">
103+ ```
104+ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
105+ -X PUT \
106+ -H 'Content-Type: multipart/form-data' \
107+ -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
108+ -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
109+ -d '{
110+ "value" : " Some Value"
111+ }'
112+
113+ curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
114+ -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
115+ -H "X-Auth-Key: $CLOUDFLARE_API_KEY"
116+ ```
117+ </TabItem >
118+ <TabItem label = " TypeScript" >
119+ ``` ts
120+ const client = new Cloudflare ({
121+ apiEmail: process .env [' CLOUDFLARE_EMAIL' ], // This is the default and can be omitted
122+ apiKey: process .env [' CLOUDFLARE_API_KEY' ], // This is the default and can be omitted
123+ });
124+
125+ const value = await client .kv .namespaces .values .update (' <KV_NAMESPACE_ID>' , ' KEY' , {
126+ account_id: ' <ACCOUNT_ID>' ,
127+ value: ' VALUE' ,
128+ });
129+
130+ const value = await client .kv .namespaces .values .get (' <KV_NAMESPACE_ID>' , ' KEY' , {
131+ account_id: ' <ACCOUNT_ID>' ,
132+ });
133+
134+ const value = await client .kv .namespaces .values .delete (' <KV_NAMESPACE_ID>' , ' KEY' , {
135+ account_id: ' <ACCOUNT_ID>' ,
136+ });
137+
138+ // Automatically fetches more pages as needed.
139+ for await (const namespace of client .kv .namespaces .list ({ account_id: ' <ACCOUNT_ID>' })) {
140+ console .log (namespace .id );
141+ }
142+
143+ ```
144+ </TabItem >
145+ </Tabs >
146+
147+ 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.
148+
105149</TabItem >
106150</Tabs >
107151
0 commit comments