First App.
Your learning space A little progress, every day
EXTRA LESSONWhen your app needs online data

Add a Cloudflare database and server code

Keep the existing website on Pages and add a small function that reads Cloudflare’s D1 database. Start with public sample tips that only you can edit.

In Cloudflare and your AI helper18 min read & do
By the end of this lesson

Show records from a real D1 database through a server endpoint, with separate local and live data.

Before you begin

Choose this route for shared Cloudflare data. If you mainly need user logins and private records, skip to the Supabase lesson. You do not need to set up both.

Let’s do this

  1. 1
    In Cloudflare

    Create a free database

    In Cloudflare, open Storage & databases, then D1 SQL Database and Create Database. Name it little-garden-live. Create little-garden-preview separately for testing. Stay on the free plan. D1 currently allows 500 MB per free database and 5 GB total, with daily read and write limits.

    Cloudflare D1: getting started (opens a new tab)
  2. 2
    In your AI helper

    Ask the AI to prepare one tiny feature

    Use the prompt below. Ask for a tips table with an ID and a tip, plus two sample rows. The AI should save the table setup as a numbered migration, which is a record of a database structure change. Have it explain the SQL before it is run.

    Cloudflare D1: database migrations (opens a new tab)
  3. 3
    In your AI helper

    Add server code to the existing Pages project

    For this course’s Vite + Pages route, ask for a GET /api/tips endpoint in functions/api/tips.ts at the project root, outside dist. It should read through a binding named DB and return only public tips. A binding is the named connection from server code to the database. Do not add public edit or delete endpoints.

    Cloudflare Pages: D1 bindings (opens a new tab)
  4. 4
    In your AI helper

    Test with a local database first

    Have the AI install and configure Cloudflare’s free Wrangler tool for this project. It should apply the migration to local D1, build the Vite site, then start a Pages preview that includes functions and the DB binding. A plain npm run dev preview does not run Pages Functions by itself. Ask for one project command to repeat this safely.

    Cloudflare Pages: local function development (opens a new tab)
  5. 5
    In your AI helper

    Set up the hosted copies deliberately

    Once local tests pass, have the AI identify the live and preview databases and apply the same reviewed migration to each intended remote database. “Local” and “remote” are different stores; local sample rows do not appear online by themselves. Keep the two sample tips public and non-personal.

  6. 6
    In Cloudflare

    Connect each Pages environment and redeploy

    Open the Pages project, then Settings, Bindings, Add and D1 database. Name the binding DB. For Production select little-garden-live; for Preview select little-garden-preview. If the project uses a Wrangler configuration file as the source of these settings, have the AI update that consistently. Commit and push the checked code, then redeploy so the binding takes effect.

    Cloudflare Pages: D1 bindings (opens a new tab)
  7. 7
    In your browser

    Prove the live page reads the database

    Open the published page and /api/tips. In the live D1 dashboard, use the Console with a reviewed SQL statement from the AI to change a sample tip. Refresh the public page and check it changes without rebuilding the site. The dashboard login protects your editing; the reading endpoint is intentionally public.

A message for your AI helper

Add a small Cloudflare D1 feature to the existing React/Vite Pages project for my first website. Start with public sample plant-care tips, edited only through my Cloudflare dashboard. Create a reviewed numbered SQL migration and a GET /api/tips Pages Function using a DB binding. No public write endpoints. Use prepared queries, bounded results and clear loading/error states. Set up a repeatable local Pages+Functions+D1 preview. Explain local versus remote migrations, separate preview/live databases, binding setup and redeployment. Keep secrets on the server and check current Free limits. Guide me through each hosted step and verify a changed database tip appears on the live page.

What you should see

The published page shows real D1 records, a dashboard edit appears after refresh, and the preview uses its own test database.

If you get stuck

Go at your own pace. There is no test to pass.