KV Namespace
Config
Section titled “Config”import { defineConfig, kv, workerEnv } from "wrangler-deploy";
const cache = kv("app-cache");
export const api = workerEnv({ CACHE: cache,});
export default defineConfig({ version: 1, workers: ["."], resources: { "app-cache": { type: "kv", bindings: { ".": "CACHE" }, }, },});Wrangler config
Section titled “Wrangler config”{ "kv_namespaces": [{ "binding": "CACHE", "id": "placeholder" }],}Worker code
Section titled “Worker code”import type { api } from "../wrangler-deploy.config.ts";type Env = typeof api.Env;// Env.CACHE is typed as KVNamespace
export default { async fetch(req: Request, env: Env) { const value = await env.CACHE.get("key"); return new Response(value ?? "not found"); },};Multiple workers sharing a KV namespace
Section titled “Multiple workers sharing a KV namespace”resources: { "shared-cache": { type: "kv", bindings: { "apps/api": "CACHE", "apps/worker": "CACHE", }, },}Both workers bind to the same namespace per stage.