Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Workers KV and R2

Note

Cache control directives and expiration times are planned for a future release.

Workers KV

Cloudflare KV is a globally distributed key-value store.

Define a KV binding in your schema to generate a matching Wrangler configuration and a fully typed interface for querying that namespace in your application code.

kv MyNamespace {
    settings -> json {}

    // accept any number of parameters
    session -> SessionToken {
        token: string
    }

    // write custom key templates
    custom -> string {
        param1: string
        param2: string

        "path/to/{param1}/{param2}"
    }
}

A Wrangler configuration will be generated:

[[kv_namespaces]]
binding = "MyNamespace"
id = "replace_with_MyNamespace_id"

R2

Define a Cloudflare R2 binding in your schema to generate a matching Wrangler configuration and a fully typed interface for querying that bucket in your application code.

r2 MyBucket {
    getObject {}

    getObjectCustomKey {
        param1: string
        param2: string

        "path/to/{param1}/{param2}"
    }
}

Unlike Workers KV, R2 bindings do not have a return type, because they will always return the Cloudflare R2Object (a HEAD request to the object, not the full value).

Additionally, a Wrangler configuration will be generated:

[[r2_buckets]]
binding = "MyBucket"
bucket_name = "replace-with-mybucket-name"