Create Cloudflare Workers Edge Functions

backend
TypeScript
scaffolding
playful
Remix

Build serverless edge functions with routing, KV storage, caching, and CORS support.

12/8/2025

Prompt

Create Cloudflare Workers edge functions for [APPLICATION_NAME] with the following specifications:

Requirements

  • Application name: [APPLICATION_NAME]
  • API endpoints: [ENDPOINT_1], [ENDPOINT_2], [ENDPOINT_3]
  • HTTP methods: [GET/POST/PUT/DELETE]
  • KV storage needed: [YES/NO]
  • KV namespaces: [NAMESPACE_1], [NAMESPACE_2]
  • Caching strategy: [Cache everything/Selective/None]
  • Cache TTL: [SECONDS]
  • CORS configuration: [Allow all origins/Specific origins]
  • Allowed origins: [ORIGIN_1], [ORIGIN_2]
  • Environment variables: [VAR_1], [VAR_2], [VAR_3]
  • External APIs to proxy: [API_1], [API_2]
  • Rate limiting: [YES/NO]

Deliverables

Generate complete Cloudflare Workers code with:

Main worker file (index.ts):

  • Default export with fetch() handler
  • Request/Response handling
  • Env interface with all bindings

Router setup using itty-router:

  • Router instance creation
  • Route definitions for all [ENDPOINTS]
  • HTTP method handlers (GET, POST, PUT, DELETE)
  • Request parsing (JSON, FormData, text)
  • Response formatting with proper headers
  • Error handling middleware
  • 404 handler for unknown routes

KV storage operations if needed:

  • Env interface with KV namespace bindings
  • GET operations: read from [NAMESPACES]
  • PUT operations: write with optional TTL
  • DELETE operations: remove keys
  • LIST operations: query keys by prefix
  • JSON value handling (stringify/parse)
  • Error handling for KV operations

Caching implementation if [CACHING_STRATEGY]:

  • Cache API usage with caches.default
  • Cache key generation
  • Cache matching logic
  • Cache-Control headers with [CACHE_TTL]
  • Cache invalidation strategy
  • Conditional caching based on request/response

CORS configuration:

  • CORS headers object with [ALLOWED_ORIGINS]
  • OPTIONS method handler for preflight
  • Apply CORS headers to all responses
  • Credentials support if needed
  • Custom allowed methods and headers

Environment variable handling:

  • Env interface with all [ENV_VARS]
  • Type-safe access to environment variables
  • Validation of required variables
  • Usage in API calls and logic

External API proxying for [EXTERNAL_APIS]:

  • Fetch calls to external services
  • Request header forwarding
  • Response streaming
  • Error handling and fallbacks
  • Timeout handling

Rate limiting if needed:

  • KV-based rate limiting per IP/user
  • Sliding window algorithm
  • Rate limit headers (X-RateLimit-*)
  • 429 response when exceeded

TypeScript types:

  • Env interface for all bindings
  • Request/Response types
  • Custom types for data structures

wrangler.toml configuration:

  • Worker name
  • KV namespace bindings
  • Environment variables
  • Compatibility settings
  • Routes configuration

Output production-ready Cloudflare Workers code ready to deploy with wrangler.

Tags

cloudflare
workers
edge
serverless

Tested Models

gpt-4
claude-3-5-sonnet

Comments (0)

Sign in to leave a comment

Sign In