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/workers/configuration.md
1# Workers Configuration23## wrangler.jsonc (Recommended)45```jsonc6{7"$schema": "./node_modules/wrangler/config-schema.json",8"name": "my-worker",9"main": "src/index.ts",10"compatibility_date": "2025-01-01", // Use current date for new projects1112// Bindings (non-inheritable)13"vars": { "ENVIRONMENT": "production" },14"kv_namespaces": [{ "binding": "MY_KV", "id": "abc123" }],15"r2_buckets": [{ "binding": "MY_BUCKET", "bucket_name": "my-bucket" }],16"d1_databases": [{ "binding": "DB", "database_name": "my-db", "database_id": "xyz789" }],1718// Environments19"env": {20"staging": {21"vars": { "ENVIRONMENT": "staging" },22"kv_namespaces": [{ "binding": "MY_KV", "id": "staging-id" }]23}24}25}26```2728## Configuration Rules2930**Inheritable**: `name`, `main`, `compatibility_date`, `routes`, `workers_dev`31**Non-inheritable**: All bindings (`vars`, `kv_namespaces`, `r2_buckets`, etc.)32**Top-level only**: `migrations`, `keep_vars`, `send_metrics`3334**ALWAYS set `compatibility_date` to current date for new projects**3536## Bindings3738```jsonc39{40// Environment variables - access via env.VAR_NAME41"vars": { "ENVIRONMENT": "production" },4243// KV (key-value storage)44"kv_namespaces": [{ "binding": "MY_KV", "id": "abc123" }],4546// R2 (object storage)47"r2_buckets": [{ "binding": "MY_BUCKET", "bucket_name": "my-bucket" }],4849// D1 (SQL database)50"d1_databases": [{ "binding": "DB", "database_name": "my-db", "database_id": "xyz789" }],5152// Durable Objects (stateful coordination)53"durable_objects": {54"bindings": [{ "name": "COUNTER", "class_name": "Counter" }]55},5657// Queues (message queues)58"queues": {59"producers": [{ "binding": "MY_QUEUE", "queue": "my-queue" }],60"consumers": [{ "queue": "my-queue", "max_batch_size": 10 }]61},6263// Service bindings (worker-to-worker RPC)64"services": [{ "binding": "SERVICE_B", "service": "service-b" }],6566// Analytics Engine67"analytics_engine_datasets": [{ "binding": "ANALYTICS" }]68}69```7071### Secrets7273Set via CLI (never in config):7475```bash76npx wrangler secret put API_KEY77```7879Access: `env.API_KEY`8081### Automatic Provisioning (Beta)8283Bindings without IDs are auto-created:8485```jsonc86{ "kv_namespaces": [{ "binding": "MY_KV" }] } // ID added on deploy87```8889## Routes & Triggers9091```jsonc92{93"routes": [94{ "pattern": "example.com/*", "zone_name": "example.com" }95],96"triggers": {97"crons": ["0 */6 * * *"] // Every 6 hours98}99}100```101102## TypeScript Setup103104### Automatic Type Generation (Recommended)105106```bash107npm install -D @cloudflare/workers-types108npx wrangler types # Generates .wrangler/types/runtime.d.ts from wrangler.jsonc109```110111`tsconfig.json`:112113```jsonc114{115"compilerOptions": {116"target": "ES2022",117"lib": ["ES2022"],118"types": ["@cloudflare/workers-types"]119},120"include": [".wrangler/types/**/*.ts", "src/**/*"]121}122```123124Import generated types:125126```typescript127import type { Env } from './.wrangler/types/runtime';128129export default {130async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {131await env.MY_KV.get('key'); // Fully typed, autocomplete works132return new Response('OK');133},134};135```136137Re-run `npx wrangler types` after changing bindings in wrangler.jsonc138139### Manual Type Definition (Legacy)140141```typescript142interface Env {143MY_KV: KVNamespace;144DB: D1Database;145API_KEY: string;146}147```148149## Advanced Options150151```jsonc152{153// Auto-locate compute near data sources154"placement": { "mode": "smart" },155156// Enable Node.js built-ins (Buffer, process, path, etc.)157"compatibility_flags": ["nodejs_compat"],158159// Observability (10% sampling)160"observability": { "enabled": true, "head_sampling_rate": 0.1 }161}162```163164### Node.js Compatibility165166`nodejs_compat` enables:167- `Buffer`, `process.env`, `path`, `stream`168- CommonJS `require()` for Node modules169- `node:` imports (e.g., `import { Buffer } from 'node:buffer'`)170171**Note:** Adds ~1-2ms cold start overhead. Use Workers APIs (R2, KV) when possible172173## Deployment Commands174175```bash176npx wrangler deploy # Production177npx wrangler deploy --env staging178npx wrangler deploy --dry-run # Validate only179```180181## See Also182183- [API](./api.md) - Runtime APIs and bindings usage184- [Patterns](./patterns.md) - Deployment strategies185- [Wrangler](../wrangler/README.md) - CLI reference186