DEV Community

Alex Spinov
Alex Spinov

Posted on

Cloudflare D1 Has a Free API You Should Know About

Cloudflare D1 is a serverless SQLite database that runs at the edge. Your data lives close to your users — globally distributed with automatic replication.

Why D1 Changes Edge Computing

A developer had a Cloudflare Worker that needed a database. Options were external DB calls adding 100ms+ latency. D1 runs alongside Workers at the edge — database queries in under 1ms.

Key Features:

  • SQLite at the Edge — Familiar SQL, global distribution
  • Zero Cold Starts — Always warm, always fast
  • Automatic Replication — Read replicas near users automatically
  • Time Travel — Restore to any point in time (30 days)
  • Free Tier — 5GB storage, 5M reads/day, 100K writes/day

Quick Start

npx wrangler d1 create my-db
Enter fullscreen mode Exit fullscreen mode
// Cloudflare Worker
export default {
  async fetch(request, env) {
    const { results } = await env.DB.prepare(
      "SELECT * FROM users WHERE id = ?"
    ).bind(1).all()

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

Migrations

npx wrangler d1 migrations create my-db init
Enter fullscreen mode Exit fullscreen mode
-- migrations/0001_init.sql
CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT UNIQUE
);
Enter fullscreen mode Exit fullscreen mode
npx wrangler d1 migrations apply my-db
Enter fullscreen mode Exit fullscreen mode

Why Choose D1

  1. Edge-native — database at every Cloudflare location
  2. SQLite — no new query language to learn
  3. Generous free tier — 5M reads/day for free

Check out D1 docs to get started.


Need edge solutions? Check out my Apify actors or email spinov001@gmail.com for custom solutions.

Top comments (0)