Wrangler is the CLI for Cloudflare Workers — deploy JavaScript to 300+ edge locations in seconds. Plus access to KV, R2, D1, Queues, and Durable Objects.
Getting Started
npm create cloudflare@latest my-worker
cd my-worker
npx wrangler dev # Local development
npx wrangler deploy # Deploy globally
Worker Code
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 the edge!' });
}
if (url.pathname === '/api/geo') {
return Response.json({
country: request.cf?.country,
city: request.cf?.city,
timezone: request.cf?.timezone
});
}
return new Response('Not Found', { status: 404 });
}
};
KV Storage
// wrangler.toml: [[kv_namespaces]] binding = "CACHE" id = "abc123"
export default {
async fetch(request: Request, env: Env) {
// Read
const cached = await env.CACHE.get('my-key');
if (cached) return new Response(cached);
// Write with TTL
await env.CACHE.put('my-key', 'value', { expirationTtl: 3600 });
return new Response('Stored');
}
};
D1 Database (SQLite at the Edge)
export default {
async fetch(request: Request, env: Env) {
const { results } = await env.DB.prepare(
'SELECT * FROM users WHERE active = ?'
).bind(1).all();
return Response.json(results);
}
};
R2 Object Storage
export default {
async fetch(request: Request, env: Env) {
if (request.method === 'PUT') {
const body = await request.arrayBuffer();
await env.BUCKET.put('myfile.pdf', body);
return new Response('Uploaded');
}
const object = await env.BUCKET.get('myfile.pdf');
return new Response(object?.body);
}
};
Wrangler CLI Commands
wrangler dev # Local dev server
wrangler deploy # Deploy to production
wrangler tail # Stream live logs
wrangler d1 execute DB --command "SELECT * FROM users"
wrangler kv:key list --binding CACHE
wrangler r2 object get bucket/key
Why This Matters
- Global by default: Code runs in 300+ locations
- Free tier: 100K requests/day
- Full platform: KV + D1 + R2 + Queues + Durable Objects
- Sub-millisecond cold start: Fastest serverless runtime
Need custom edge computing or Cloudflare Workers tooling? I build developer tools. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.
Top comments (0)