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
title: Read multiple keys from Workers KV with bulk reads
3
+
description: You can now retrieve up to 100 keys in a single bulk read request to Workers KV.
4
+
products:
5
+
- kv
6
+
date: 2025-04-17T14:00:00Z
7
+
---
8
+
9
+
You can now retrieve up to 100 keys in a single bulk read request made to Workers KV using the binding.
10
+
11
+
This makes it easier to request multiple KV pairs within a single Worker invocation. Retrieving many key-value pairs using the bulk read operation is more performant than making individual requests since bulk read operations are not affected by [Workers simultaneous connection limits](/workers/platform/limits/#simultaneous-open-connections).
12
+
```js
13
+
// Read single key
14
+
constkey="key-a";
15
+
constvalue=awaitenv.NAMESPACE.get(keys);
16
+
17
+
// Read multiple keys
18
+
constkeys= ["key-a", "key-b", "key-c", ...] // up to 100 keys
To get the value for a given key, call the `get()` method of the [KV binding](/kv/concepts/kv-bindings/) on any [KV namespace](/kv/concepts/kv-namespaces/) you have bound to your Worker code:
9
9
10
10
```js
11
+
// Read individual key
11
12
env.NAMESPACE.get(key);
13
+
14
+
// Read multiple keys
15
+
env.NAMESPACE.get(keys);
12
16
```
13
17
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`.
18
+
The `get()` method returns a promise you can `await` on to get the value.
15
19
16
-
#### Example
20
+
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`.
17
21
18
-
An example of reading a key from within a Worker:
22
+
You can also request an array of keys. The return value with be a `Map` of the key-value pairs found,
23
+
with keys not found having `null` values.
19
24
20
25
```js
21
26
exportdefault {
22
27
asyncfetch(request, env, ctx) {
23
28
try {
29
+
// Read single key, returns value or null
24
30
constvalue=awaitenv.NAMESPACE.get("first-key");
25
31
26
-
if (value ===null) {
27
-
returnnewResponse("Value not found", { status:404 });
`get()` and `getWithMetadata()` methods 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.
57
+
58
+
:::
59
+
60
+
37
61
## Reference
38
62
39
63
The following methods are provided to read from KV:
To get the value for a given key, call the `get()` method on any KV namespace you have bound to your Worker code:
70
+
Use the `get()` method to get a single value, or multiple values if given multiple keys:
71
+
72
+
- Read single keys with [get(key: string)](#request-a-single-key-with-getkey-string)
73
+
- Read multiple keys with [get(keys: string[])](#request-multiple-keys-with-getkeys-string)
74
+
75
+
#### Request a single key with `get(key: string)`
76
+
77
+
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
78
48
79
```js
49
80
env.NAMESPACE.get(key, type?);
50
81
// OR
51
82
env.NAMESPACE.get(key, options?);
52
83
```
53
84
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
85
+
##### Parameters
57
86
58
87
- `key`: `string`
59
88
- The key of the KV pair.
@@ -65,7 +94,7 @@ The `get()` method returns a promise you can `await` on to get the value. If the
65
94
}`
66
95
- 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:
@@ -74,10 +103,52 @@ The `get()` method returns a promise you can `await` on to get the value. If the
74
103
- `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
75
104
- `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
76
105
77
-
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.
106
+
#### Request multiple keys with `get(keys: string[])`
107
+
108
+
To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:
109
+
110
+
```js
111
+
env.NAMESPACE.get(keys, type?);
112
+
// OR
113
+
env.NAMESPACE.get(keys, options?);
114
+
```
115
+
116
+
##### Parameters
117
+
118
+
- `keys`: `string[]`
119
+
- The keys of the KV pairs. Max: 100 keys
120
+
- `type`: `"text"|"json"`
121
+
- Optional. The type of the value to be returned. `text` is the default.
122
+
- `options`: `{
123
+
cacheTtl?: number,
124
+
type?:"text"|"json"
125
+
}`
126
+
- 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.
127
+
128
+
:::note
129
+
130
+
The `.get()` function to read multiple keys does not support `arrayBuffer` or `stream` return types. If you need to read multiple keys of `arrayBuffer` or `stream` types, consider using the `.get()` function to read individual keys in parallel with `Promise.all()`.
- The value for the requested KV pair. If no key is found, `null` is returned for the key. The response type will depend on the `type` parameter provided for the `get()` command as follows:
138
+
- `text`: A `string` (default).
139
+
- `json`: An object decoded from a JSON string.
140
+
141
+
The limit of the response size is 25 MB. Responses above this size will fail with a `413Error` error message.
78
142
79
143
### `getWithMetadata()` method
80
144
145
+
Use the `getWithMetadata()` method to get a single value along with its metadata, or multiple values with their metadata:
146
+
147
+
- Read single keys with [getWithMetadata(key: string)](#request-a-single-key-with-getwithmetadatakey-string)
148
+
- Read multiple keys with [getWithMetadata(keys: string[])](#request-multiple-keys-with-getwithmetadatakeys-string)
149
+
150
+
#### Request a single key with `getWithMetadata(key: string)`
151
+
81
152
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
161
91
-
#### Parameters
162
+
##### Parameters
92
163
93
164
- `key`: `string`
94
165
- The key of the KV pair.
@@ -100,7 +171,7 @@ Metadata is a serializable value you append to each KV entry.
100
171
}`
101
172
- 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.
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
117
188
118
-
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.
189
+
#### Request multiple keys with `getWithMetadata(keys: string[])`
119
190
120
-
#### Example
121
-
122
-
An example of reading a key with metadata from within a Worker:
191
+
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:
123
192
124
193
```js
125
-
exportdefault {
126
-
asyncfetch(request, env, ctx) {
127
-
try {
128
-
const { value, metadata } =
129
-
awaitenv.NAMESPACE.getWithMetadata("first-key");
130
-
131
-
if (value ===null) {
132
-
returnnewResponse("Value not found", { status:404 });
133
-
}
134
-
returnnewResponse(value);
135
-
} catch (e) {
136
-
returnnewResponse(e.message, { status:500 });
137
-
}
138
-
},
139
-
};
194
+
env.NAMESPACE.getWithMetadata(keys, type?);
195
+
// OR
196
+
env.NAMESPACE.getWithMetadata(keys, options?);
140
197
```
141
198
199
+
##### Parameters
200
+
201
+
- `keys`: `string[]`
202
+
- The keys of the KV pairs. Max: 100 keys
203
+
- `type`: `"text"|"json"`
204
+
- Optional. The type of the value to be returned. `text` is the default.
205
+
- `options`: `{
206
+
cacheTtl?: number,
207
+
type?:"text"|"json"
208
+
}`
209
+
- 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.
210
+
211
+
:::note
212
+
The `.get()` function to read multiple keys does not support `arrayBuffer` or `stream` return types. If you need to read multiple keys of `arrayBuffer` or `stream` types, consider using the `.get()` function to read individual keys in parallel with `Promise.all()`.
213
+
:::
214
+
215
+
##### Response
216
+
217
+
- `response`: `Promise<Map<string, {
218
+
value: string |Object|null,
219
+
metadata: string |Object|null
220
+
}>`
221
+
222
+
- 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:
223
+
- `text`: A `string` (default).
224
+
- `json`: An object decoded from a JSON string.
225
+
- The type of the metadata will just depend on what is stored, which can be either a string or an object.
226
+
227
+
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
228
+
229
+
The limit of the response size is 25 MB. Responses above this size will fail with a `413Error` error message.
230
+
142
231
## Guidance
143
232
144
233
### Type parameter
@@ -167,4 +256,3 @@ The effective `cacheTtl` of an already cached item can be reduced by getting it
167
256
## Other methods to access KV
168
257
169
258
You can [read key-value pairs from the command line with Wrangler](/kv/reference/kv-commands/#kv-key-get) and [from the REST API](/api/resources/kv/subresources/namespaces/subresources/values/methods/get/).
0 commit comments