DEV Community

Alex Spinov
Alex Spinov

Posted on

Cloudflare Workers Has a Free API — Deploy Code to 300+ Edge Locations

Cloudflare Workers lets you run JavaScript/TypeScript at the edge — 300+ locations worldwide, 0ms cold starts, and a generous free tier (100K requests/day). Deploy APIs, full-stack apps, or middleware that runs closer to your users than any server.

Why Workers?

  • 0ms cold start — V8 isolates, not containers
  • 300+ locations — code runs near every user
  • Free tier — 100K requests/day, 10ms CPU/request
  • Built-in storage — KV, D1 (SQLite), R2 (S3-compatible), Durable Objects

Quick Start

npm create cloudflare@latest myworker
cd myworker
npm run dev   # Local development
npm run deploy # Deploy to edge
Enter fullscreen mode Exit fullscreen mode

Basic Worker

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/users' && request.method === 'POST') {
      const body = await request.json();
      return Response.json({ created: body }, { status: 201 });
    }

    return new Response('Not Found', { status: 404 });
  },
};
Enter fullscreen mode Exit fullscreen mode

KV Storage (Key-Value)

// wrangler.toml
// [[kv_namespaces]]
// binding = "CACHE"
// id = "abc123"

export default {
  async fetch(request: Request, env: Env) {
    // Write
    await env.CACHE.put('user:1', JSON.stringify({ name: 'Alice' }), {
      expirationTtl: 3600,
    });

    // Read
    const user = await env.CACHE.get('user:1', 'json');

    // List keys
    const keys = await env.CACHE.list({ prefix: 'user:' });

    return Response.json(user);
  },
};
Enter fullscreen mode Exit fullscreen mode

D1 Database (SQLite at the Edge)

export default {
  async fetch(request: Request, env: Env) {
    // Query
    const { results } = await env.DB.prepare(
      'SELECT * FROM users WHERE age > ?'
    ).bind(18).all();

    // Insert
    await env.DB.prepare(
      'INSERT INTO users (name, email) VALUES (?, ?)'
    ).bind('Alice', 'alice@example.com').run();

    return Response.json(results);
  },
};
Enter fullscreen mode Exit fullscreen mode

R2 Storage (S3-Compatible)

export default {
  async fetch(request: Request, env: Env) {
    if (request.method === 'PUT') {
      const body = await request.arrayBuffer();
      await env.BUCKET.put('uploads/file.pdf', body);
      return new Response('Uploaded');
    }

    const object = await env.BUCKET.get('uploads/file.pdf');
    if (!object) return new Response('Not Found', { status: 404 });

    return new Response(object.body, {
      headers: { 'Content-Type': object.httpMetadata?.contentType ?? '' },
    });
  },
};
Enter fullscreen mode Exit fullscreen mode

Hono on Workers

import { Hono } from 'hono';

const app = new Hono();

app.get('/api/users', async (c) => {
  const { results } = await c.env.DB.prepare('SELECT * FROM users').all();
  return c.json(results);
});

app.post('/api/users', async (c) => {
  const { name, email } = await c.req.json();
  await c.env.DB.prepare('INSERT INTO users (name, email) VALUES (?, ?)').bind(name, email).run();
  return c.json({ success: true }, 201);
});

export default app;
Enter fullscreen mode Exit fullscreen mode

Need edge computing for your scraping pipeline? Check out my Apify actors for web scraping, or email spinov001@gmail.com for custom edge solutions.

Workers, Lambda, or Deno Deploy — which edge platform do you use? Share below!

Top comments (0)