Skip to content

Commit 8241a05

Browse files
Oxyjunthomasgauvin
andauthored
[KV] Drafting a more detailed Overview page for Workers KV. (#19830)
* Drafting a more detailed Overview page for Workers KV. * Improving code example, adding Wrangler config. * Adding a heading "What is Workers KV?" * Implementing feedback. * Update src/content/docs/kv/index.mdx Co-authored-by: Thomas Gauvin <[email protected]> * Fixing broken links. * Implementing feedback part 1. * Using embedded tabs to also showcase wrangler file. * Update src/content/docs/kv/index.mdx * Update src/content/docs/kv/index.mdx --------- Co-authored-by: Thomas Gauvin <[email protected]>
1 parent d8cd818 commit 8241a05

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

src/content/docs/kv/index.mdx

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ sidebar:
55
order: 1
66
---
77

8-
import {
9-
CardGrid,
10-
Description,
11-
Feature,
12-
LinkTitleCard,
13-
Plan,
14-
RelatedProduct,
15-
} from "~/components";
8+
import { CardGrid, Description, Feature, LinkTitleCard, Plan, RelatedProduct, Tabs, TabItem, LinkButton } from "~/components";
169

1710
<Description>
1811

@@ -24,6 +17,92 @@ Create a global, low-latency, key-value data storage.
2417

2518
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.
2619

20+
For example, you can use Workers KV for:
21+
22+
- Caching API responses.
23+
- Storing user configurations / preferences.
24+
- Storing user authentication details.
25+
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:
32+
33+
<Tabs>
34+
<TabItem label="Workers Binding API">
35+
<Tabs>
36+
<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
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+
</TabItem>
86+
<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+
```
101+
</TabItem>
102+
</Tabs>
103+
104+
Learn more about Workers KV [key concepts](/kv/concepts/how-kv-works/), or [get started](/kv/get-started/) with a Workers project.
105+
27106
---
28107

29108
## Features

0 commit comments

Comments
 (0)