Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/kv/configuration.md
1# KV Configuration23## Create Namespace45```bash6wrangler kv namespace create MY_NAMESPACE7# Output: { binding = "MY_NAMESPACE", id = "abc123..." }89wrangler kv namespace create MY_NAMESPACE --preview # For local dev10```1112## Workers Binding1314**wrangler.jsonc:**15```jsonc16{17"kv_namespaces": [18{19"binding": "MY_KV",20"id": "abc123xyz789"21},22// Optional: Different namespace for preview/development23{24"binding": "MY_KV",25"preview_id": "preview-abc123"26}27]28}29```3031## TypeScript Types3233**env.d.ts:**34```typescript35interface Env {36MY_KV: KVNamespace;37SESSIONS: KVNamespace;38CACHE: KVNamespace;39}40```4142**worker.ts:**43```typescript44export default {45async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {46// env.MY_KV is now typed as KVNamespace47const value = await env.MY_KV.get("key");48return new Response(value || "Not found");49}50} satisfies ExportedHandler<Env>;51```5253**Type-safe JSON operations:**54```typescript55interface UserProfile {56name: string;57email: string;58role: "admin" | "user";59}6061const profile = await env.USERS.get<UserProfile>("user:123", "json");62// profile: UserProfile | null (type-safe!)63if (profile) {64console.log(profile.name); // TypeScript knows this is a string65}66```6768## CLI Operations6970```bash71# Put72wrangler kv key put --binding=MY_KV "key" "value"73wrangler kv key put --binding=MY_KV "key" --path=./file.json --ttl=36007475# Get76wrangler kv key get --binding=MY_KV "key"7778# Delete79wrangler kv key delete --binding=MY_KV "key"8081# List82wrangler kv key list --binding=MY_KV --prefix="user:"8384# Bulk operations (max 10,000 keys per file)85wrangler kv bulk put data.json --binding=MY_KV86wrangler kv bulk get keys.json --binding=MY_KV87wrangler kv bulk delete keys.json --binding=MY_KV --force88```8990## Local Development9192```bash93wrangler dev # Local KV (isolated)94wrangler dev --remote # Remote KV (production)9596# Or in wrangler.jsonc:97# "kv_namespaces": [{ "binding": "MY_KV", "id": "...", "remote": true }]98```99100## REST API101102### Single Operations103104```typescript105import Cloudflare from 'cloudflare';106107const client = new Cloudflare({108apiEmail: process.env.CLOUDFLARE_EMAIL,109apiKey: process.env.CLOUDFLARE_API_KEY110});111112// Single key operations113await client.kv.namespaces.values.update(namespaceId, 'key', {114account_id: accountId,115value: 'value',116expiration_ttl: 3600117});118```119120### Bulk Operations121122```typescript123// Bulk update (up to 10,000 keys, max 100MB total)124await client.kv.namespaces.bulkUpdate(namespaceId, {125account_id: accountId,126body: [127{ key: "key1", value: "value1", expiration_ttl: 3600 },128{ key: "key2", value: "value2", metadata: { version: 1 } },129{ key: "key3", value: "value3" }130]131});132133// Bulk get (up to 100 keys)134const results = await client.kv.namespaces.bulkGet(namespaceId, {135account_id: accountId,136keys: ["key1", "key2", "key3"]137});138139// Bulk delete (up to 10,000 keys)140await client.kv.namespaces.bulkDelete(namespaceId, {141account_id: accountId,142keys: ["key1", "key2", "key3"]143});144```145