List key-value pairs

Hi everyone!

We are currently storing some key-value pairs in Cloudflare’s worker KV storage but there is a growing need to list all values with metadata using the API.

To exemplify, the following is a sample of our key-value.

key: id-foo
value: value-bar

The value is always a short string, around 20 characters, but we have several key-value pairs and we need to list all of them. Currently, what we do is list all the keys and for each one, look for its value.

Ideally, we would love to be able to list all the keys via the API, returning the value and their metadata, since this would drastically increase performance.

I imagine it would be something like this:

GET accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values

returning:

{
  "result": [
    {
      "name": "id-foo:1",
      "value": "value-bar-1",
      "metadata": {
        "modifiedAt": "2021-08-10T17:40:42.515"
      }
    },
    {
      "name": "id-foo:2",
      "value": "value-bar-2",
      "metadata": {
        "modifiedAt": "2021-08-10T17:40:42.515"
      }
    }
  ],
  "success": true,
  "errors": [],
  "messages": [],
  "result_info": {
    "count": 2,
    "cursor": ""
  }
}

Is there a way to do this?

:+1: I would love this as well. In my opinion, most functions available in the kv runtime api should have an equivalent in the rest api.

In case you haven’t seen it, here is another thread with a similar question, and no good answer: Workers KV API how to read values with metadata

1 Like