// Rapid development with Cloudflare Workers - build and deploy serverless applications on Cloudflare's global network. Use when building APIs, full-stack web apps, edge functions, background jobs, or real-time applications. Triggers on phrases like "cloudflare workers", "wrangler", "edge computing", "serverless cloudflare", "workers bindings", or files like wrangler.toml, worker.ts, worker.js.
| name | cloudflare-workers |
| description | Rapid development with Cloudflare Workers - build and deploy serverless applications on Cloudflare's global network. Use when building APIs, full-stack web apps, edge functions, background jobs, or real-time applications. Triggers on phrases like "cloudflare workers", "wrangler", "edge computing", "serverless cloudflare", "workers bindings", or files like wrangler.toml, worker.ts, worker.js. |
Cloudflare Workers is a serverless execution environment that runs JavaScript, TypeScript, Python, and Rust code on Cloudflare's global network. Workers execute in milliseconds, scale automatically, and integrate with Cloudflare's storage and compute products through bindings.
Key Benefits:
Use Cloudflare Workers for:
npm install -g wrangler
# Login to Cloudflare
wrangler login
# Using C3 (create-cloudflare) - recommended
npm create cloudflare@latest my-worker
# Or create manually
wrangler init my-worker
cd my-worker
Basic HTTP API (TypeScript):
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === "/api/hello") {
return Response.json({ message: "Hello from Workers!" });
}
return new Response("Not found", { status: 404 });
},
};
With environment variables and KV:
interface Env {
MY_VAR: string;
MY_KV: KVNamespace;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Access environment variable
const greeting = env.MY_VAR;
// Read from KV
const value = await env.MY_KV.get("my-key");
return Response.json({ greeting, value });
},
};
# Start local development server with hot reload
wrangler dev
# Access at http://localhost:8787
# Deploy to workers.dev subdomain
wrangler deploy
# Deploy to custom domain (configure in wrangler.toml)
wrangler deploy
Workers use the V8 JavaScript engine with Web Standard APIs:
Supported APIs:
Workers respond to events through handlers:
Fetch Handler (HTTP requests):
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
return new Response("Hello!");
},
};
Scheduled Handler (cron jobs):
export default {
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
// Runs on schedule defined in wrangler.toml
await env.MY_KV.put("last-run", new Date().toISOString());
},
};
Queue Handler (message processing):
export default {
async queue(batch: MessageBatch<any>, env: Env, ctx: ExecutionContext) {
for (const message of batch.messages) {
await processMessage(message.body);
message.ack();
}
},
};
Bindings connect your Worker to Cloudflare resources. Configure in wrangler.toml:
KV (Key-Value Storage):
[[kv_namespaces]]
binding = "MY_KV"
id = "your-kv-namespace-id"
// Usage
await env.MY_KV.put("key", "value");
const value = await env.MY_KV.get("key");
D1 (SQL Database):
[[d1_databases]]
binding = "DB"
database_name = "my-database"
database_id = "your-database-id"
// Usage
const result = await env.DB.prepare(
"SELECT * FROM users WHERE id = ?"
).bind(userId).all();
R2 (Object Storage):
[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "my-bucket"
// Usage
await env.MY_BUCKET.put("file.txt", "contents");
const object = await env.MY_BUCKET.get("file.txt");
const text = await object?.text();
Environment Variables:
[vars]
API_KEY = "development-key" # pragma: allowlist secret
Secrets (sensitive data):
# Set via CLI (not in wrangler.toml)
wrangler secret put API_KEY
The ctx parameter provides control over request lifecycle:
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
// Run tasks after response is sent
ctx.waitUntil(
env.MY_KV.put("request-count", String(Date.now()))
);
// Pass through to origin on exception
ctx.passThroughOnException();
return new Response("OK");
},
};
Essential wrangler.toml:
name = "my-worker"
main = "src/index.ts"
compatibility_date = "2025-01-01"
# Custom domain
routes = [
{ pattern = "api.example.com/*", zone_name = "example.com" }
]
# Or workers.dev subdomain
workers_dev = true
# Environment variables
[vars]
ENVIRONMENT = "production"
# Bindings
[[kv_namespaces]]
binding = "CACHE"
id = "your-kv-id"
[[d1_databases]]
binding = "DB"
database_name = "production-db"
database_id = "your-db-id"
[[r2_buckets]]
binding = "ASSETS"
bucket_name = "my-assets"
# Cron triggers
[triggers]
crons = ["0 0 * * *"] # Daily at midnight
Use environments for staging/production:
[env.staging]
vars = { ENVIRONMENT = "staging" }
[env.staging.d1_databases]
binding = "DB"
database_name = "staging-db"
database_id = "staging-db-id"
[env.production]
vars = { ENVIRONMENT = "production" }
[env.production.d1_databases]
binding = "DB"
database_name = "production-db"
database_id = "production-db-id"
# Deploy to staging
wrangler deploy --env staging
# Deploy to production
wrangler deploy --env production
JSON API with Error Handling:
export default {
async fetch(request: Request, env: Env): Promise<Response> {
try {
const url = new URL(request.url);
if (url.pathname === "/api/users" && request.method === "GET") {
const users = await env.DB.prepare("SELECT * FROM users").all();
return Response.json(users.results);
}
if (url.pathname === "/api/users" && request.method === "POST") {
const body = await request.json();
await env.DB.prepare(
"INSERT INTO users (name, email) VALUES (?, ?)"
).bind(body.name, body.email).run();
return Response.json({ success: true }, { status: 201 });
}
return Response.json({ error: "Not found" }, { status: 404 });
} catch (error) {
return Response.json(
{ error: error.message },
{ status: 500 }
);
}
},
};
Authentication Middleware:
async function authenticate(request: Request, env: Env): Promise<string | null> {
const authHeader = request.headers.get("Authorization");
if (!authHeader?.startsWith("Bearer ")) {
return null;
}
const token = authHeader.substring(7);
const userId = await env.SESSIONS.get(token);
return userId;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const userId = await authenticate(request, env);
if (!userId) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
// Proceed with authenticated request
return Response.json({ userId });
},
};
CORS Headers:
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export default {
async fetch(request: Request): Promise<Response> {
if (request.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
const response = await handleRequest(request);
// Add CORS headers to response
Object.entries(corsHeaders).forEach(([key, value]) => {
response.headers.set(key, value);
});
return response;
},
};
Serve static files alongside your Worker code:
[assets]
directory = "./public"
binding = "ASSETS"
import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// API routes
if (url.pathname.startsWith("/api/")) {
return handleAPI(request, env);
}
// Serve static assets
try {
return await getAssetFromKV(
{ request, waitUntil: () => {} },
{ ASSET_NAMESPACE: env.ASSETS }
);
} catch {
return new Response("Not found", { status: 404 });
}
},
};
Using Vitest:
import { env, createExecutionContext } from "cloudflare:test";
import { describe, it, expect } from "vitest";
import worker from "./index";
describe("Worker", () => {
it("responds with JSON", async () => {
const request = new Request("http://example.com/api/hello");
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
expect(response.status).toBe(200);
expect(await response.json()).toEqual({ message: "Hello!" });
});
});
Workers supports major frameworks with adapters:
Example with Hono:
import { Hono } from "hono";
const app = new Hono();
app.get("/", (c) => c.text("Hello!"));
app.get("/api/users/:id", async (c) => {
const id = c.req.param("id");
const user = await c.env.DB.prepare(
"SELECT * FROM users WHERE id = ?"
).bind(id).first();
return c.json(user);
});
export default app;
For detailed information on advanced features, see the reference files:
references/bindings-complete-guide.md - Deep dive into all binding types (Durable Objects, Queues, Vectorize, AI, Hyperdrive, etc.)references/wrangler-and-deployment.md - Wrangler commands, GitHub Actions, GitLab CI/CD, gradual rollouts, versionsreferences/development-patterns.md - Testing strategies, debugging, error handling, performance optimizationreferences/advanced-features.md - Workers for Platforms, Smart Placement, WebSockets, streaming, custom domainsreferences/observability.md - Logging (tail, Logpush, Workers Logs), metrics, traces, debuggingOfficial Documentation:
Templates & Quick Starts:
Community: