Skip to content

Commit 5329e57

Browse files
committed
Implementing feedback.
1 parent cd1f950 commit 5329e57

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/content/docs/kv/index.mdx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
Plan,
1414
RelatedProduct,
1515
Tabs,
16-
TabItem
16+
TabItem,
17+
LinkButton
1718
} from "~/components";
1819

1920
<Description>
@@ -28,41 +29,64 @@ Create a global, low-latency, key-value data storage.
2829

2930
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.
3031

32+
For example, you can use Workers KV for:
33+
34+
- Caching API responses.
35+
- Storing user configurations / preferences.
36+
- Storing user authentication details.
37+
3138
Workers KV is ideal for projects that require:
3239

3340
- High volumes of reads and/or repeated reads to the same keys.
3441
- Per-object time-to-live (TTL).
3542
- Distributed configuration.
3643

37-
For example, you can use Workers KV for:
38-
39-
- Caching API responses.
40-
- Storing user configurations / preferences.
41-
- Storing user authentication details.
44+
**Code examples**
4245

4346
<Tabs>
44-
<TabItem label="index.ts">
47+
<TabItem label="REST API">
48+
```
49+
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
50+
-X PUT \
51+
-H 'Content-Type: multipart/form-data' \
52+
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
53+
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
54+
-d '{
55+
"value": "Some Value"
56+
}'
57+
58+
curl https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces/$NAMESPACE_ID/values/$KEY_NAME \
59+
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
60+
-H "X-Auth-Key: $CLOUDFLARE_API_KEY"
61+
```
62+
</TabItem>
63+
<TabItem label="Workers Binding API">
4564
```ts
4665
export interface Env {
4766
YOUR_BINDING: KVNamespace;
4867
}
4968

5069
export default {
5170
async fetch(request, env, ctx): Promise<Response> {
71+
72+
//write a key-value pair
5273
await env.YOUR_BINDING.put('KEY', 'VALUE');
74+
75+
// read a key-value pair
5376
const value = await env.YOUR_BINDING.get('KEY');
77+
78+
//list all key-value pairs
5479
const allKeys = await env.YOUR_BINDING.list();
80+
81+
//delete a key-value pair
5582
await env.YOUR_BINDING.delete('KEY');
83+
84+
//return a Workers response
5685
return new Response(
5786
JSON.stringify({
5887
value: value,
5988
allKeys: allKeys,
6089
}),
61-
{
62-
headers: {
63-
'Content-Type': 'application/json'
64-
},
65-
}
6690
);
6791
},
6892
};
@@ -90,6 +114,8 @@ export default {
90114
</TabItem>
91115
</Tabs>
92116

117+
Learn more about Workers KV [key concepts](/kv/how-kv-works/), or [get started](kv/get-started/).
118+
93119
---
94120

95121
## Features

0 commit comments

Comments
 (0)