Read key-value pairs
To get the value for a given key, call the get() method on your KV instance.
To get the value for a given key, call the get() method on your KV instance:
The get() method returns a promise you can await to get the value.
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.
You can also request an array of keys. The return value will be an object with
key-value pairs, where keys not found have null values.
Reference
The following methods are provided to read from KV Store:
get() method
Use the get() method to get a single value, or multiple values if given
multiple keys.
Request a single key
To get the value for a single key, call the get() method on your KV instance:
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key of the KV pair. |
type | "text" | "json" | "arrayBuffer" | "stream" | Optional. The type of the value to be returned. text is the default. |
options | { cacheTtl?: number } | Optional. Object containing the cacheTtl property. |
Options
| Option | Type | Description |
|---|---|---|
cacheTtl | number | Time in seconds to cache the result locally. Minimum value is 60. |
Response
| Response | Type | Description |
|---|---|---|
response | Promise<string | object | ArrayBuffer | ReadableStream | null> | The value for the requested KV pair, or null if not found. |
The response type depends on the type parameter:
| Type | Returns |
|---|---|
"text" | A string (default) |
"json" | An object decoded from a JSON string |
"arrayBuffer" | An ArrayBuffer instance |
"stream" | A ReadableStream |
Request multiple keys
To get the values for multiple keys, call the get() method with an array:
Parameters
| Parameter | Type | Description |
|---|---|---|
keys | string[] | The keys of the KV pairs. |
type | "text" | "json" | Optional. The type of the value to be returned. text is the default. |
Response
| Response | Type | Description |
|---|---|---|
response | Promise<object> | An object with key-value pairs. Keys not found have null values. |
getWithMetadata() method
Use the getWithMetadata() method to get a single value along with its
metadata, or multiple values with their metadata.
Request a single key with metadata
To get the value for a given key along with its metadata:
Metadata is a serializable value you append to each KV entry when using put().
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key of the KV pair. |
type | "text" | "json" | "arrayBuffer" | "stream" | Optional. The type of the value to be returned. text is the default. |
options | { cacheTtl?: number } | Optional. Object containing the cacheTtl property. |
Response
| Response | Type | Description |
|---|---|---|
response | Promise<{ value: string | object | ArrayBuffer | ReadableStream | null, metadata: object | null }> | An object containing the value and metadata. |
If there is no metadata associated with the requested key-value pair, null
will be returned for metadata.
Request multiple keys with metadata
To get the values for multiple keys along with their metadata:
Parameters
| Parameter | Type | Description |
|---|---|---|
keys | string[] | The keys of the KV pairs. |
type | "text" | "json" | Optional. The type of the value to be returned. text is the default. |
Response
| Response | Type | Description |
|---|---|---|
response | Promise<object> | An object with keys mapped to { value, metadata } objects. |
Guidance
Type parameter
For simple values, use the default text type which provides your value as a
string. For convenience, a json type is also specified which will convert
a JSON value into an object before returning it. For large values, use stream
to request a ReadableStream. For binary values, use arrayBuffer to request
an ArrayBuffer.
For large values, the choice of type can have a noticeable effect on latency
and CPU usage. For reference, the type can be ordered from fastest to slowest
as stream, arrayBuffer, text, and json.
CacheTtl parameter
cacheTtl is a parameter that defines the length of time in seconds that a KV
result is cached in the global network location where it is accessed.
Defining the length of time in seconds is useful for reducing cold read latency
on keys that are read relatively infrequently. cacheTtl is useful if your
data is write-once or write-rarely.
cacheTtl is not recommended if your data is updated often and you need to
see updates shortly after they are written, because writes from other global
network locations will not be visible until the cached value expires.
The minimum value for cacheTtl is 60 seconds.
Handling non-existent keys
When a key doesn’t exist, get() returns null:
Reading streams
For large values stored as streams, process the data incrementally:
Working with metadata
Retrieve both value and metadata in a single call: