Workers KV over the Cloudflare API: namespaces, key listing (cursor paged), and values. KV stores raw bytes — read returns Bytes; read-text and read-json decode them. Values written through the API become visible at the edge within about 60 seconds.

Context Vars

Optional
  • cloudflare.token
  • cloudflare.account.id

Values

namespaces-suffix

namespaces-suffix: Str "/storage/kv/namespaces"

Functions

create-namespace

fn (title: Str): Map
fn (title: Str, opts: Map): Map

POST /accounts/{account_id}/storage/kv/namespaces - Create a namespace titled title. Returns {id, title, supports_url_encoding}.

Example

created ::cloudflare::kv/create-namespace("sessions")
created.id

delete-bulk

fn (namespace-id: Str, key-names: Vec<Str>): Map
fn (namespace-id: Str, key-names: Vec<Str>, opts: Map): Map

POST .../bulk/delete - Delete up to 10,000 keys in one request.

Example

::cloudflare::kv/delete-bulk("0f2ac74b498b48028cb68387c421e279", ["flag:a", "session:abc"])

delete-key

fn (namespace-id: Str, key: Str): Any
fn (namespace-id: Str, key: Str, opts: Map): Any

DELETE .../values/{key} - Remove a key (succeeds for absent keys too).

Example

::cloudflare::kv/delete-key("0f2ac74b498b48028cb68387c421e279", "session:abc")

delete-namespace

fn (namespace-id: Str): Any
fn (namespace-id: Str, opts: Map): Any

DELETE a namespace and every key in it.

Example

::cloudflare::kv/delete-namespace("0f2ac74b498b48028cb68387c421e279")

is-not-found

fn (result: Result): Bool

True when result is an Err for a missing key (HTTP 404). Use it to tell "absent" from real failures after read.

Example

missing err(::cloudflare::api/HttpError({status: 404, headers: {}, body: "", errors: [], message: "HTTP 404"}))
::cloudflare::kv/is-not-found(missing)  // true
::cloudflare::kv/is-not-found("value")  // false

list-all-keys

fn (namespace-id: Str): Vec
fn (namespace-id: Str, params: Map): Vec
fn (namespace-id: Str, params: Map, max: Int): Vec
fn (namespace-id: Str, params: Map, opts: Map): Vec
fn (namespace-id: Str, params: Map, max: Int, opts: Map): Vec

Every key matching params (usually {prefix}), following cursors (1000 per request) up to max keys (default 10000).

Example

keys-found ::cloudflare::kv/list-all-keys("0f2ac74b498b48028cb68387c421e279", {prefix: "session:"}, 5000)
map(keys-found, %.name)

list-all-namespaces

fn (): Vec
fn (max: Int): Vec
fn (opts: Map): Vec
fn (max: Int, opts: Map): Vec

Every namespace, following pages (1000 per request) up to max items (default 10000).

Example

map(::cloudflare::kv/list-all-namespaces(500), %.title)

list-keys

fn (namespace-id: Str): ::cloudflare::api/ListPage
fn (namespace-id: Str, params: Map): ::cloudflare::api/ListPage
fn (namespace-id: Str, params: Map, opts: Map): ::cloudflare::api/ListPage

GET .../namespaces/{id}/keys - One page of keys (names, expiration, metadata; never values). params: prefix, limit (10–1000), cursor (from the previous page's result_info.cursor). Returns a ListPage; ::cloudflare::api/next-cursor(page.result_info) is null on the last page.

Example

namespace-id "0f2ac74b498b48028cb68387c421e279"
page ::cloudflare::kv/list-keys(namespace-id, {prefix: "session:", limit: 100})
map(page.result, %.name)

list-namespaces

fn (): ::cloudflare::api/ListPage
fn (params: Map): ::cloudflare::api/ListPage
fn (params: Map, opts: Map): ::cloudflare::api/ListPage

GET /accounts/{account_id}/storage/kv/namespaces - One page of namespaces (page, per_page up to 1000, order: id/title, direction). Returns a ListPage.

Example

page ::cloudflare::kv/list-namespaces({per_page: 100, order: "title"})
map(page.result, %.title)

read

fn (namespace-id: Str, key: Str): Bytes
fn (namespace-id: Str, key: Str, opts: Map): Bytes

GET .../values/{key} - A value's raw bytes. An absent key is an Err carrying HttpError with status 404 (see is-not-found).

Example

namespace-id "0f2ac74b498b48028cb68387c421e279"
png ::cloudflare::kv/read(namespace-id, "avatar:42")

read-json

fn (namespace-id: Str, key: Str): Any
fn (namespace-id: Str, key: Str, opts: Map): Any

A value parsed as JSON (an Err when it is not valid JSON).

Example

settings ::cloudflare::kv/read-json("0f2ac74b498b48028cb68387c421e279", "settings")
settings.theme

read-metadata

fn (namespace-id: Str, key: Str): Any
fn (namespace-id: Str, key: Str, opts: Map): Any

GET .../metadata/{key} - The JSON metadata stored with a key (null when none).

Example

meta-data ::cloudflare::kv/read-metadata("0f2ac74b498b48028cb68387c421e279", "session:abc")
meta-data.user

read-text

fn (namespace-id: Str, key: Str): Str
fn (namespace-id: Str, key: Str, opts: Map): Str

A value decoded as UTF-8 text.

Example

::cloudflare::kv/read-text("0f2ac74b498b48028cb68387c421e279", "flag:checkout-v2")

write

fn (namespace-id: Str, key: Str, value: Any): Any
fn (namespace-id: Str, key: Str, value: Any, options: Map): Any
fn (namespace-id: Str, key: Str, value: Any, options: Map, opts: Map): Any

PUT .../values/{key} - Store value (Str or Bytes; a Map/Vec is stored as JSON). options:

  • expiration_ttl — seconds until the key expires (minimum 60)
  • expiration — absolute expiry, seconds since the epoch
  • metadata — JSON (up to 1024 bytes serialized) kept alongside the key

A write without metadata sends the raw bytes; with metadata it uses the multipart form the API requires.

Example

namespace-id "0f2ac74b498b48028cb68387c421e279"
::cloudflare::kv/write(namespace-id, "flag:checkout-v2", "on")
::cloudflare::kv/write(namespace-id, "session:abc", to-json({cart: ["sku-1"]}), {expiration_ttl: 3600, metadata: {user: "u_42"}})

write-bulk

fn (namespace-id: Str, entries: Vec): Map
fn (namespace-id: Str, entries: Vec, opts: Map): Map

PUT .../bulk - Write up to 10,000 entries in one request. Each entry is {key, value, expiration_ttl?, expiration?, metadata?, base64?}; value must be a string (set base64: true and base64-encode binary values). Returns {successful_key_count, unsuccessful_keys}.

Example

out ::cloudflare::kv/write-bulk("0f2ac74b498b48028cb68387c421e279", [
  {key: "flag:a", value: "on"},
  {key: "session:abc", value: "{}", expiration_ttl: 3600}
])
out.successful_key_count

write-parts

fn (value: Any, metadata: Any): Vec

The multipart parts for a write that carries metadata: the value part (Str or Bytes) and a JSON metadata part. Exposed for testing.

Example

parts ::cloudflare::kv/write-parts("on", {user: "u_42"})
map(parts, %.name)  // ["value", "metadata"]
parts[1].value  // "{\"user\":\"u_42\"}"

write-query

fn (options: Map): Str

The ?expiration_ttl=…&expiration=… suffix for a write; unset options are omitted. Exposed for testing.

Example

::cloudflare::kv/write-query({expiration_ttl: 3600})  // "?expiration_ttl=3600"
::cloudflare::kv/write-query({})  // ""