Skip to content

Commit b65f42f

Browse files
thomasgauvinOxyjun
andauthored
Nits to overview (#20367)
* Update index.mdx * Update src/content/docs/kv/index.mdx * Update src/content/docs/kv/index.mdx * Update src/content/docs/kv/index.mdx * thomasgauvin: add ts example * thomasgauvin: more nits to overview kv * thomasgauvin: nits * thomasgauvin: remove ideal, which is repeated from first paragraph * thomasgauvin: typo * Update src/content/docs/kv/index.mdx Removing unnecessary linebreak * thomasgauvin: nits * thomasgauvin: add overview kv get started button --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 56574d8 commit b65f42f

File tree

1 file changed

+115
-69
lines changed

1 file changed

+115
-69
lines changed

src/content/docs/kv/index.mdx

Lines changed: 115 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -23,85 +33,121 @@ For example, you can use Workers KV for:
2333
- Storing user configurations / preferences.
2434
- Storing user authentication details.
2535

26-
Workers KV is ideal for projects that require:
27-
28-
- High volumes of reads and/or repeated reads to the same keys.
29-
- Globally distributed data.
30-
31-
Access your Workers KV namespace from Cloudflare Workers using the Workers Binding API or from your external application using the REST API:
36+
Access your Workers KV namespace from Cloudflare Workers using [Workers Bindings](/workers/runtime-apis/bindings/) or from your external application using the REST API:
3237

3338
<Tabs>
3439
<TabItem label="Workers Binding API">
3540
<Tabs>
3641
<TabItem label="index.ts">
37-
```ts
38-
export default {
39-
async fetch(request, env, ctx): Promise<Response> {
40-
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
42+
```ts
43+
export default {
44+
async fetch(request, env, ctx): Promise<Response> {
45+
// write a key-value pair
46+
await env.KV_BINDING.put('KEY', 'VALUE');
47+
48+
// read a key-value pair
49+
const value = await env.KV_BINDING.get('KEY');
50+
51+
// list all key-value pairs
52+
const allKeys = await env.KV_BINDING.list();
53+
54+
// delete a key-value pair
55+
await env.KV_BINDING.delete('KEY');
56+
57+
// return a Workers response
58+
return new Response(
59+
JSON.stringify({
60+
value: value,
61+
allKeys: allKeys,
62+
}),
63+
);
64+
},
65+
66+
} satisfies ExportedHandler<{ KV_BINDING: KVNamespace }>;
67+
68+
```
69+
</TabItem>
70+
<TabItem label="wrangler.jsonc">
71+
72+
```json
73+
{
74+
"$schema": "node_modules/wrangler/config-schema.json",
75+
"name": "WORKER-NAME",
76+
"main": "src/index.ts",
77+
"compatibility_date": "2025-02-04",
78+
"observability": {
79+
"enabled": true
80+
},
81+
82+
"kv_namespaces": [
6783
{
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>
84+
"binding": "KV_BINDING",
85+
"id": "<YOUR_BINDING_ID>"
86+
}
87+
]
88+
}
89+
```
90+
91+
</TabItem>
92+
</Tabs>
93+
94+
See the full [Workers KV binding API reference](/kv/api/read-key-value-pairs/).
95+
8596
</TabItem>
8697
<TabItem label="REST API">
87-
```
88-
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
89-
-X PUT \
90-
-H 'Content-Type: multipart/form-data' \
91-
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
92-
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
93-
-d '{
94-
"value": "Some Value"
95-
}'
96-
97-
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
98-
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
99-
-H "X-Auth-Key: $CLOUDFLARE_API_KEY"
100-
```
98+
99+
<Tabs>
100+
<TabItem label="cURL">
101+
```
102+
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
103+
-X PUT \
104+
-H 'Content-Type: multipart/form-data' \
105+
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
106+
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
107+
-d '{
108+
"value": "Some Value"
109+
}'
110+
111+
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
112+
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
113+
-H "X-Auth-Key: $CLOUDFLARE_API_KEY"
114+
```
115+
</TabItem>
116+
<TabItem label="TypeScript">
117+
```ts
118+
const client = new Cloudflare({
119+
apiEmail: process.env['CLOUDFLARE_EMAIL'], // This is the default and can be omitted
120+
apiKey: process.env['CLOUDFLARE_API_KEY'], // This is the default and can be omitted
121+
});
122+
123+
const value = await client.kv.namespaces.values.update('<KV_NAMESPACE_ID>', 'KEY', {
124+
account_id: '<ACCOUNT_ID>',
125+
value: 'VALUE',
126+
});
127+
128+
const value = await client.kv.namespaces.values.get('<KV_NAMESPACE_ID>', 'KEY', {
129+
account_id: '<ACCOUNT_ID>',
130+
});
131+
132+
const value = await client.kv.namespaces.values.delete('<KV_NAMESPACE_ID>', 'KEY', {
133+
account_id: '<ACCOUNT_ID>',
134+
});
135+
136+
// Automatically fetches more pages as needed.
137+
for await (const namespace of client.kv.namespaces.list({ account_id: '<ACCOUNT_ID>' })) {
138+
console.log(namespace.id);
139+
}
140+
141+
```
142+
</TabItem>
143+
</Tabs>
144+
145+
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.
146+
101147
</TabItem>
102148
</Tabs>
103149

104-
Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a Workers project.
150+
<LinkButton href="/kv/get-started/">Get started</LinkButton>
105151

106152
---
107153

0 commit comments

Comments
 (0)