Skip to content
Tinbox

tutorials

Local-first email development: run the whole app on SQLite before you deploy

September 28, 2026

#tinbox#local-development#sqlite#cloudflare#d1
Conceptual textless workspace panels show a queue, reading pane, and context.

Evaluating an email stack usually means provisioning real cloud resources before you can click anything: a database, object storage, a queue, DNS records, secrets. That is a lot of setup to answer "is this even the shape I want?" Local-first development inverts the order: run the whole thing on your laptop first, then provision only once you have decided.

Most "try it" flows for email infrastructure put the cloud account first. Sign up, create a database, add a domain, paste an API key, then maybe see a screen. For Cloudflare-targeted stacks that order is doubly awkward, because the runtime is the edge and the natural assumption is that you cannot run it anywhere else.

You can. Cloudflare D1 is SQLite, and the local Workers runtime (miniflare, which wrangler dev uses) ships a local D1 that is a plain SQLite file on disk. That means the entire Tinbox app (the inbox UI, the transactional sender, FTS5 full-text search, the importers, and the AI chat agent) runs against local SQLite with no Cloudflare account, no domain, and no secrets. Below is how to do it, what is actually happening under the hood, and how the local setup maps one-to-one onto the deployed D1 version so you are not learning a throwaway environment.

Can I run a full email app locally on SQLite before deploying to Cloudflare?

Yes, and it is one command. The Tinbox companion app (the Worker plus React inbox SPA) has a demo script that applies migrations, seeds a populated inbox, builds the UI, and starts wrangler dev against miniflare's local D1.

pnpm install     # once, at the repo root
pnpm demo:app    # setup + build UI + start wrangler dev

Then open http://localhost:9401 and sign in with the seeded account:

demo@tinbox.local  /  demodemo

You land in a populated three-pane inbox: four seeded mailboxes (Support, Sales, Founder, Hiring), around ten threads mixing inbound mail and sent replies, unread badges, a "needs reply" flag, tenant custom-field badges, and an internal note. Full-text search works immediately. Try refund, enterprise, or dark mode and watch results come back from the same FTS5 index the production deploy uses.

No external keys are involved in any of that. The transactional send path, the importers, and search all run against local SQLite. The only feature that needs a key is the AI chat assistant, because it calls an LLM through the Vercel AI SDK. More on that below.

Why does local SQLite match production Cloudflare D1?

Because D1 is SQLite. There is no translation layer or compatibility shim between "local mode" and "deployed mode" at the query level: the schema, the migrations, and the SQL you run are identical, including the FTS5 virtual tables that power search. The runtime difference is operational, not behavioral.

When you run wrangler dev, miniflare creates a local SQLite database and binds it to the same binding name your deployed Worker uses. In Tinbox that binding is DB:

// apps/demo/wrangler.jsonc (abridged)
"d1_databases": [
  {
    "binding": "DB",
    "database_name": "tinbox",
    "database_id": "REPLACE_WITH_D1_DATABASE_ID",
    "migrations_dir": "migrations"
  }
]

Notice the database_id is a placeholder. In production that id points at a real D1 database in your Cloudflare account. Locally it is ignored entirely: miniflare keys on the binding name (DB), not the id, and writes to a SQLite file at .wrangler/state/v3/d1. So the REPLACE_WITH_* placeholders that would break a real deploy are irrelevant to local mode. Your Worker code calls env.DB.prepare(...).bind(...).all() and neither it nor you can tell whether the rows came from your laptop or from Cloudflare's replicated D1.

That is what makes local-first honest here rather than a demo trick. The code path you exercise on localhost:9401 is the same code path that ships. If a query works locally, it works deployed, with one class of exception worth knowing: D1's correlated-subquery and statement-count behavior at the edge has occasionally surfaced bugs that local SQLite does not, so treat local as "almost certainly correct" rather than "provably identical." For schema design and feature work, local D1 is the right loop.

What does the demo command actually do?

pnpm demo:app runs the @tinbox/demo demo script, which is three steps in sequence: setup:local, then build:ui, then wrangler dev. Each is runnable on its own once you start iterating:

# what demo:app runs, expanded
pnpm -F @tinbox/demo setup:local   # reset/seed local D1, write .dev.vars
pnpm -F @tinbox/demo build:ui      # build the React SPA into public/
pnpm -F @tinbox/demo dev           # wrangler dev only (assumes the above ran)

setup:local is the interesting one. On a clean checkout it:

  1. Writes apps/demo/.dev.vars with a freshly generated BETTER_AUTH_SECRET so sign-in works without you configuring anything. The file is gitignored.
  2. Applies migrations in the correct order against local D1: the engine migrations from packages/engine/migrations first (schema plus the FTS5 search tables), then the auth migration from the app's own migrations directory.
  3. Generates the demo seed SQL (the inboxes, threads, the directory of teammates, the demo login) and executes it.
  4. Builds the UI once if public/assets is missing.

Everything runs through wrangler d1 execute DB --local --file=..., which writes to the exact same SQLite file that wrangler dev later reads. There is no separate "dev database" to keep in sync.

One detail that saves confusion: setup:local is idempotent and preserves existing state by default. If .wrangler/state/v3/d1 already exists and you did not pass --reset, it skips migrations and seeding so your session and any data you created survive a restart.

[setup-local] .dev.vars already present, leaving it (BETTER_AUTH_SECRET preserved)
[setup-local] D1 state exists, skipping migrations + seed (sessions preserved)
[setup-local] run with --reset to wipe and re-seed

When you want a clean slate (after editing the schema, or to drop test data), force it:

pnpm demo:app:reset    # = demo:app with --reset passed through to setup

That wipes the local D1 state, re-applies all migrations, and re-seeds. This is your "factory reset" while developing a D1 email schema, and the things to know before you put email in Cloudflare D1 are worth a read while you are iterating on it locally.

How do I handle a port conflict or a missing build?

The demo serves on :9401, and a stray dev process can leave it in use. The repo ships a one-liner to free the dev ports:

$ pnpm demo:app
# ...
✘ [ERROR] Address already in use: could not start server on 127.0.0.1:9401
pnpm kill      # frees the dev ports (9401/9402/…), then re-run pnpm demo:app

A second common stumble is a blank page on first run. If you ever invoke dev directly before the UI has been built, the Worker serves an empty public/. The symptom is a 404 or blank document at the root while /health still returns OK:

$ curl -s http://localhost:9401/health
{"ok":true}
$ curl -s http://localhost:9401/ | head -c 80
# (empty or a not-found body, no SPA was built)

The fix is to build the UI, which demo:app does for you but dev does not:

pnpm -F @tinbox/demo build:ui

If you prefer hot-reload while editing the React client, use split mode: it runs the Vite SPA with HMR and the Worker side by side, with Vite proxying /api to the Worker.

pnpm demo:app:dev    # Vite SPA-HMR on :9401, wrangler worker on :9402

This is the loop you want when you are changing UI, not data.

Can I send and receive email in the local demo?

Partly, and the boundary is worth being precise about. The transactional send path and the inbox both operate against local SQLite, so composing and "sending" a reply writes the outbound message to your local database and threads it correctly. That is enough to test compose flows, templating, and the ledger write that precedes a real send.

What local mode does not do is hand a message to a real delivery transport: it intentionally binds a local fake. In production, Tinbox uses Cloudflare's native Email Service binding or an HTTPS provider path. Workers cannot deliver direct-to-MX SMTP because outbound port 25 is blocked; although connect() can reach allowed third-party submission services, that is not what the local demo is testing. If you want the full boundary, see [[cloudflare-workers-send-email|sending email from Cloudflare Workers]]. Inbound mail is similar: real inbound arrives via Cloudflare Email Routing, which is account-level configuration you wire after deploying. Locally, the seed stands in for "mail that already arrived" so the inbox is populated to work against.

So the honest summary: the local demo is for exercising the application logic (inbox, threading, search, templates, compose) end to end. The actual wire transport (real outbound delivery, real inbound routing) is a deploy-time concern. That split is by design, and it is exactly the part you cannot meaningfully test on a laptop anyway.

How do I turn on the AI chat agent locally?

The AI chat agent over your inbox is the one feature that needs an external key, because it calls a model. Everything else is keyless. The setup script writes a commented-out placeholder for you:

# apps/demo/.dev.vars (written by setup:local)
BETTER_AUTH_SECRET="…generated…"
# Optional: the chat assistant uses the Vercel AI SDK; paste a provider key to enable it.
# ANTHROPIC_API_KEY=""

Uncomment the line, paste a key, and restart wrangler dev. The provider is selectable: CHAT_PROVIDER defaults to anthropic (set ANTHROPIC_API_KEY) and also accepts openai (set OPENAI_API_KEY). The chat agent runs the same tool-calling loop locally as in production because it is built on the Vercel AI SDK and operates over the same D1-backed inbox. If you are curious how that agent reads your mail, running retrieval over your own email covers the retrieval side, and connecting an external AI assistant to your inbox covers the MCP path.

Leave the key out and the rest of the app is fully functional. Local-first does not mean "degraded"; it means the only thing you cannot do offline is the thing that fundamentally requires a remote model.

How does the local setup map onto a real deploy?

The mental model is: local-first is the inner loop, npx tinbox init is the outer loop, and the database engine is the same on both ends. Once you have decided the app is the shape you want, you move from a local SQLite file to a provisioned D1 database in your own Cloudflare account.

Here is the mapping, piece by piece:

Local demoDeployed
miniflare local D1 (SQLite file under .wrangler/state/v3/d1)Cloudflare D1 database in your account
DB binding, database_id ignoredDB binding, real database_id from provisioning
Seed SQL stands in for inbound mailCloudflare Email Routing catch-all to your Worker
Local "send" writes to SQLiteSend worker dispatches real MIME over HTTPS
.dev.vars with a generated secretSecrets pushed to the Worker during setup
pnpm demo (build + serve)wrangler deploy (Workers Assets serves the SPA)

The migrations do not change between the two. The seed you used locally is replaced by real traffic. To go from local to live you run the bootstrapper, which scaffolds a project, provisions the D1 database, R2 bucket, KV namespace, and queues, pushes secrets, applies the same migrations, and deploys:

npx tinbox init <your-domain>

The full npx tinbox init walkthrough covers that step in detail, including the one inbound email-routing rule you click in the dashboard, and the things to know before you run npx tinbox init flags the decisions worth making before you provision anything. And if you are coming from an existing system, the schema you validated locally is the same target you would aim a Postgres-to-D1 email migration at.

One honest caveat for planning: a single deploy serves one brand and one domain. You can run many tenants on one Cloudflare account via separate deploys, and multi-domain-per-instance is designed but not yet built. None of that changes the local loop, which is per-checkout and single-tenant by nature.

Why local-first is worth the discipline

The payoff of running the whole app on SQLite first is that your evaluation and your development share one fast, offline, deterministic loop. You can read the schema, run a query, break it, reset, and try again in seconds, with no cloud round-trip and no resources to clean up. Because local D1 and deployed D1 are the same SQLite engine, that loop is not a sandbox you graduate out of; it is the same code path you ship, minus the wire transport you could not test locally anyway.

When you are ready to put it on real infrastructure, the move is mechanical: provision once, point the same migrations at a real D1 database, wire one inbound routing rule. Everything you learned locally still applies.

Deploy this yourself: npx tinbox init <domain>. The local demo, the seed, and the setup scripts all live in apps/demo; the README there documents every sub-command, and the docs walk through provisioning when you are ready to go from a SQLite file to your own Cloudflare account.

Frequently asked questions

Can I run a full email app locally on SQLite before deploying to Cloudflare?

Yes. Tinbox's local demo runs the complete app (inbox, sender, full-text search, AI chat) against miniflare's local D1, which is SQLite on disk. One command, pnpm demo:app, applies migrations, seeds demo data, and serves the app at http://localhost:9401. No Cloudflare account, no domain, and no secrets are required.

Is the local SQLite database the same as Cloudflare D1?

D1 is SQLite. When you run wrangler dev, miniflare gives you a local SQLite file under .wrangler/state/v3/d1 that is bound by the same DB binding name your deployed Worker uses. The schema, the migrations, and the SQL (including FTS5 search) are identical. The main difference is that local D1 is a single file on your machine, while deployed D1 is replicated by Cloudflare.

How do I reset the local email database to a clean state?

Run pnpm demo:app:reset (which passes --reset through to the setup step). It wipes .wrangler/state/v3/d1, re-applies the engine migrations and then the auth migration, and re-seeds the demo data and login. Without --reset the setup script preserves existing state so your dev session and sign-in survive restarts.

Do I need an API key to try the inbox?

No. The inbox, transactional sender, importers, and FTS5 search all work with no external keys. Only the AI chat assistant needs a provider key (ANTHROPIC_API_KEY or OPENAI_API_KEY) because it calls an LLM through the Vercel AI SDK. The setup script writes a .dev.vars file with a generated BETTER_AUTH_SECRET so sign-in works out of the box.

What login does the local demo use?

The seed creates demo@tinbox.local with the password demodemo, granted admin on every seeded inbox. The hash is produced by better-auth's own scrypt hashing, so it verifies against the real email and password sign-in flow rather than a bypass.