$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-kvComplete knowledge domain for Cloudflare Workers KV - global, low-latency key-value storage on Cloudflare's edge network. Use when: creating KV namespaces, storing configuration data, caching API responses, managing user preferences, implementing TTL expiration, handling KV metad
| 1 | # Cloudflare Workers KV |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-21 |
| 5 | **Dependencies**: cloudflare-worker-base (for Worker setup) |
| 6 | **Latest Versions**: wrangler@4.43.0, @cloudflare/workers-types@4.20251014.0 |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (5 Minutes) |
| 11 | |
| 12 | ### 1. Create KV Namespace |
| 13 | |
| 14 | ```bash |
| 15 | # Create a new KV namespace |
| 16 | npx wrangler kv namespace create MY_NAMESPACE |
| 17 | |
| 18 | # Output includes namespace_id - save this! |
| 19 | # ✅ Success! |
| 20 | # Add the following to your wrangler.toml or wrangler.jsonc: |
| 21 | # |
| 22 | # [[kv_namespaces]] |
| 23 | # binding = "MY_NAMESPACE" |
| 24 | # id = "<UUID>" |
| 25 | ``` |
| 26 | |
| 27 | **For development (preview) namespace:** |
| 28 | |
| 29 | ```bash |
| 30 | npx wrangler kv namespace create MY_NAMESPACE --preview |
| 31 | |
| 32 | # Output: |
| 33 | # [[kv_namespaces]] |
| 34 | # binding = "MY_NAMESPACE" |
| 35 | # preview_id = "<UUID>" |
| 36 | ``` |
| 37 | |
| 38 | ### 2. Configure Bindings |
| 39 | |
| 40 | Add to your `wrangler.jsonc`: |
| 41 | |
| 42 | ```jsonc |
| 43 | { |
| 44 | "name": "my-worker", |
| 45 | "main": "src/index.ts", |
| 46 | "compatibility_date": "2025-10-11", |
| 47 | "kv_namespaces": [ |
| 48 | { |
| 49 | "binding": "MY_NAMESPACE", // Available as env.MY_NAMESPACE |
| 50 | "id": "<production-uuid>", // Production namespace ID |
| 51 | "preview_id": "<preview-uuid>" // Local dev namespace ID (optional) |
| 52 | } |
| 53 | ] |
| 54 | } |
| 55 | ``` |
| 56 | |
| 57 | **Or use `wrangler.toml`:** |
| 58 | |
| 59 | ```toml |
| 60 | name = "my-worker" |
| 61 | main = "src/index.ts" |
| 62 | compatibility_date = "2025-10-11" |
| 63 | |
| 64 | [[kv_namespaces]] |
| 65 | binding = "MY_NAMESPACE" |
| 66 | id = "<production-uuid>" |
| 67 | preview_id = "<preview-uuid>" # optional |
| 68 | ``` |
| 69 | |
| 70 | **CRITICAL:** |
| 71 | - `binding` is how you access the namespace in code (`env.MY_NAMESPACE`) |
| 72 | - `id` is the production namespace UUID |
| 73 | - `preview_id` is for local dev (optional, separate namespace) |
| 74 | - **Never commit real namespace IDs to public repos** - use environment variables or secrets |
| 75 | |
| 76 | ### 3. Write Your First Key-Value Pair |
| 77 | |
| 78 | ```typescript |
| 79 | import { Hono } from 'hono'; |
| 80 | |
| 81 | type Bindings = { |
| 82 | MY_NAMESPACE: KVNamespace; |
| 83 | }; |
| 84 | |
| 85 | const app = new Hono<{ Bindings: Bindings }>(); |
| 86 | |
| 87 | app.post('/set/:key', async (c) => { |
| 88 | const key = c.req.param('key'); |
| 89 | const value = await c.req.text(); |
| 90 | |
| 91 | // Simple write |
| 92 | await c.env.MY_NAMESPACE.put(key, value); |
| 93 | |
| 94 | return c.json({ success: true, key }); |
| 95 | }); |
| 96 | |
| 97 | app.get('/get/:key', async (c) => { |
| 98 | const key = c.req.param('key'); |
| 99 | const value = await c.env.MY_NAMESPACE.get(key); |
| 100 | |
| 101 | if (!value) { |
| 102 | return c.json({ error: 'Not found' }, 404); |
| 103 | } |
| 104 | |
| 105 | return c.json({ value }); |
| 106 | }); |
| 107 | |
| 108 | export default app; |
| 109 | ``` |
| 110 | |
| 111 | ### 4. Test Locally |
| 112 | |
| 113 | ```bash |
| 114 | # Start local development server |
| 115 | npm run dev |
| 116 | |
| 117 | # In another terminal, test the endpoints |
| 118 | curl -X POST http://localhost:8787/set/test -d "Hello KV" |
| 119 | # {"success":true,"key":"test"} |
| 120 | |
| 121 | curl http://localhost:8787/get/test |
| 122 | # {"value":"Hello KV"} |
| 123 | ``` |
| 124 | |
| 125 | --- |
| 126 | |
| 127 | ## Complete Workers KV API |
| 128 | |
| 129 | ### 1. Read Operations |
| 130 | |
| 131 | #### `get()` - Read Single Key |
| 132 | |
| 133 | ```typescript |
| 134 | // Get as string (default) |
| 135 | const value: string | null = await env.MY_KV.get('my-key'); |
| 136 | |
| 137 | // Get as JSON |
| 138 | const data: MyType | null = await env.MY_KV.get('my-key', { type: 'json' }); |
| 139 | |
| 140 | // Get as ArrayBuffer |
| 141 | const buffer: ArrayBuffer | null = await env.MY_KV.get('my-key', { type: 'arrayBuffer' }); |
| 142 | |
| 143 | // Get as ReadableStream |
| 144 | const stream: ReadableStream | null = await env.MY_KV.get('my-key', { type: 'stream' }); |
| 145 | |
| 146 | // Get with cache optimization |
| 147 | const value = await env.MY_KV.get('my-key', { |
| 148 | type: 'text', |
| 149 | cacheTtl: 300, // Cache at edge for 5 minutes (minimum 60 seconds) |
| 150 | }); |
| 151 | ``` |
| 152 | |
| 153 | #### `get()` - Read Multiple Keys (Bulk) |
| 154 | |
| 155 | ```typescript |
| 156 | // Read multiple keys at once (counts as 1 operation) |
| 157 | const keys = ['key1', 'key2', 'key3']; |
| 158 | const values: Map<string, string | null> = await env.MY_KV.get(keys); |
| 159 | |
| 160 | // Access values |
| 161 | const value1 = values.get('key1'); // string | null |
| 162 | const value2 = values.get('key2'); // string | null |
| 163 | |
| 164 | // Convert to object |
| 165 | const obj = Object.fromEntries(values); |
| 166 | ``` |
| 167 | |
| 168 | #### `getWithMetadata()` - Read with Metadata |
| 169 | |
| 170 | ```typescript |
| 171 | // Get single key with metadata |
| 172 | const { value, metadata } = await env.MY_KV.getWithMetadata('my-key'); |
| 173 | |
| 174 | // value: string | null |
| 175 | // metadata: any | null |
| 176 | |
| 177 | // Get as JSON with metadata |
| 178 | const { value, metadata } = await env.MY_KV.getWithMetadata<MyType>('my-key', { |
| 179 | type: 'json', |
| 180 | cacheTtl: 300, |
| 181 | }); |
| 182 | |
| 183 | // Get multiple keys with metadata |
| 184 | const keys = ['key1', 'key2']; |
| 185 | const result: Map<string, { value: string | null, metadata: any | null }> = |
| 186 | await env.MY_KV.getWithMetadata(keys) |