You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/kv/api/read-key-value-pairs.mdx
+123-9
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ To get the value for a given key, call the `get()` method of the [KV binding](/k
11
11
env.NAMESPACE.get(key);
12
12
```
13
13
14
-
The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.
14
+
The `get()` method returns a promise you can `await` on to get the value.
15
+
If you request a single key as a string, you will get a single response in the promise. If the key is not found, the promise will resolve with the literal value `null`.
16
+
Instead of a single key, you can also request an array of keys. In this case the response is a map with the key-value pairs.
The following methods are provided to read from KV:
@@ -43,17 +61,19 @@ The following methods are provided to read from KV:
43
61
44
62
### `get()` method
45
63
46
-
To get the value for a given key, call the `get()` method on any KV namespace you have bound to your Worker code:
64
+
The `get()` method can be used to get a single value, or multiple values, if given multiple keys
65
+
66
+
#### Requesting a single key
67
+
68
+
To get the value for a single key, call the `get()` method on any KV namespace you have bound to your Worker code with:
47
69
48
70
```js
49
71
env.NAMESPACE.get(key, type?);
50
72
// OR
51
73
env.NAMESPACE.get(key, options?);
52
74
```
53
75
54
-
The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.
55
-
56
-
#### Parameters
76
+
##### Parameters
57
77
58
78
- `key`: `string`
59
79
- The key of the KV pair.
@@ -65,7 +85,7 @@ The `get()` method returns a promise you can `await` on to get the value. If the
65
85
}`
66
86
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
@@ -76,8 +96,47 @@ The `get()` method returns a promise you can `await` on to get the value. If the
76
96
77
97
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
78
98
99
+
#### Requesting multiple keys
100
+
101
+
To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:
102
+
103
+
```js
104
+
env.NAMESPACE.get(keys, type?);
105
+
// OR
106
+
env.NAMESPACE.get(keys, options?);
107
+
```
108
+
109
+
##### Parameters
110
+
111
+
- `keys`: `string[]`
112
+
- The keys of the KV pairs. Max: 100 keys
113
+
- `type`: `"text"|"json"`
114
+
- Optional. The type of the value to be returned. `text` is the default.
115
+
- `options`: `{
116
+
cacheTtl?: number,
117
+
type?:"text"|"json"
118
+
}`
119
+
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
125
+
- `text`: A `string` (default).
126
+
- `json`: An object decoded from a JSON string.
127
+
128
+
When requesting multiple keys, the promise will never resolve to a null value. It may, however, resolve to a map that contains a key that corresponds to a null value, similarly to what happens for a single get.
129
+
130
+
You cannot request over 25MB of data. If you do, the request will fail with a `413` Error.
131
+
132
+
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
133
+
79
134
### `getWithMetadata()` method
80
135
136
+
The `getWithMetadata()` method can be used to get a single value or multiple values, if given multiple keys.
137
+
138
+
#### Requesting a single key
139
+
81
140
To get the value for a given key along with its metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code:
Metadata is a serializable value you append to each KV entry.
90
149
91
-
#### Parameters
150
+
##### Parameters
92
151
93
152
- `key`: `string`
94
153
- The key of the KV pair.
@@ -100,7 +159,7 @@ Metadata is a serializable value you append to each KV entry.
100
159
}`
101
160
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
@@ -117,7 +176,7 @@ If there is no metadata associated with the requested key-value pair, `null` wil
117
176
118
177
The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
119
178
120
-
#### Example
179
+
##### Example
121
180
122
181
An example of reading a key with metadata from within a Worker:
123
182
@@ -139,6 +198,61 @@ export default {
139
198
};
140
199
```
141
200
201
+
#### Requesting multiple keys
202
+
203
+
To get the values for a given set of keys along with their metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code with:
204
+
205
+
```js
206
+
env.NAMESPACE.getWithMetadata(keys, type?);
207
+
// OR
208
+
env.NAMESPACE.getWithMetadata(keys, options?);
209
+
```
210
+
211
+
##### Parameters
212
+
213
+
- `keys`: `string[]`
214
+
- The keys of the KV pairs. Max: 100 keys
215
+
- `type`: `"text"|"json"`
216
+
- Optional. The type of the value to be returned. `text` is the default.
217
+
- `options`: `{
218
+
cacheTtl?: number,
219
+
type?:"text"|"json"
220
+
}`
221
+
- Optional. Object containing the optional `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
222
+
223
+
##### Response
224
+
225
+
- `response`: `Promise<Map<string, {
226
+
value: string |Object|null,
227
+
metadata: string |Object|null
228
+
}>`
229
+
230
+
- An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
231
+
- `text`: A `string` (default).
232
+
- `json`: An object decoded from a JSON string.
233
+
- The type of the metadata will just depend on what is stored, which can be either a string or an object.
234
+
235
+
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
236
+
237
+
You cannot request over 25MB of data. If you do, the request will fail with a `413` Error .
238
+
##### Example
239
+
240
+
An example of reading a key with metadata from within a Worker:
0 commit comments