Cloudflare workers

From TerminalSkills/skills · View interactive version

Cloudflare workers is an AI agent skill from the GitHub repository TerminalSkills/skills. Cloudflare Workers is an edge computing platform for running JavaScript, TypeScript, and WebAssembly close to users worldwide with sub-millisecond cold starts. Use when working with the Workers runtime, the Wrangler CLI, KV, D1, R2, Durable Objects, Queues, or Hyperdrive, or when a user asks to buil

How to use this skill

Skill file

Cloudflare Workers

Overview

Cloudflare Workers runs code at the edge in V8 isolates, with cold starts measured in low milliseconds. Workers pairs with storage and compute services: KV (eventually-consistent key-value cache), D1 (serverless SQLite), R2 (S3-compatible object storage with no egress fees), Durable Objects (strongly consistent, single-instance state), and Queues (message queues for background work). The CLI is Wrangler; configuration lives in wrangler.jsonc (or the older wrangler.toml).

Instructions

Examples

Example 1: Edge API with KV caching

User request: "Set up a Cloudflare Worker that serves cached API responses from KV."

npm create cloudflare@latest -- product-cache-api
cd product-cache-api
npx wrangler kv namespace create PRODUCT_CACHE
// wrangler.jsonc
{
  "name": "product-cache-api",
  "main": "src/index.ts",
  "compatibility_date": "2026-09-23",
  "kv_namespaces": [
    { "binding": "PRODUCT_CACHE", "id": "a1b2c3d4e5f64a7b8c9d0e1f2a3b4c5d" }
  ]
}
// src/index.ts
export interface Env {
  PRODUCT_CACHE: KVNamespace;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);
    const sku = url.searchParams.get("sku");
    if (!sku) return new Response("Missing sku", { status: 400 });

    const cached = await env.PRODUCT_CACHE.get(sku);
    if (cached) return new Response(cached, { headers: { "Cache-Control": "public, max-age=60" } });

    const upstream = await fetch(`https://api.internal.example/products/${sku}`);
    const body = await upstream.text();
    await env.PRODUCT_CACHE.put(sku, body, { expirationTtl: 300 });
    return new Response(body, { headers: { "Cache-Control": "public, max-age=60" } });
  },
};
npx wrangler dev        # local, simulated KV
npx wrangler deploy

Output: a Worker that checks KV for a cached product, falls back to the origin API, and caches the result for five minutes.

Example 2: Scheduled sync into D1

User request: "Build a Worker that runs on a schedule to sync inventory counts from an external API into D1."

npx wrangler d1 create inventory-db
npx wrangler d1 migrations create inventory-db create_inventory_table
# edit the generated migration SQL, then:
npx wrangler d1 migrations apply inventory-db --remote
{
  "d1_databases": [
    { "binding": "DB", "database_name": "inventory-db", "database_id": "7f3e9c21-4b8a-4d6e-9f12-5a6b7c8d9e0f" }
  ],
  "triggers": { "crons": ["0 * * * *"] }
}
export default {
  async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
    const res = await fetch("https://api.internal.example/inventory/snapshot");
    const items: { sku: string; count: number }[] = await res.json();
    const stmt = env.DB.prepare("INSERT OR REPLACE INTO inventory (sku, count) VALUES (?, ?)");
    await env.DB.batch(items.map((i) => stmt.bind(i.sku, i.count)));
    ctx.waitUntil(fetch("https://api.internal.example/sync/ack", { method: "POST" }));
  },
};

Output: a cron-triggered Worker (hourly) that refreshes a D1 inventory table from an upstream API.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.