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.
Show records from a real D1 database through a server endpoint, with separate local and live data.
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
- 1In 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) - 2In 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) - 3In 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) - 4In 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) - 5In 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.
- 6In 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) - 7In 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
Checked 7 September 2026. Screens can change; the official guides below have the latest steps.
Cloudflare D1: getting startedCloudflare D1: storage and recovery limitsCloudflare D1: pricingCloudflare Pages: D1 bindingsCloudflare D1: database migrationsCloudflare Pages: local function developmentCloudflare Workers: Git buildsCloudflare: Next.js on Workers