# Self-host the backend

Technical users can run the FlareMail Worker on their own Cloudflare account with a single provisioning script.

5 min read. HTML: https://sendwithflare.com/help/self-host-the-backend/

FlareMail is open source, and its backend is a single Cloudflare Worker backed by D1 (metadata) and R2 (message bodies). If you'd rather run it on your own Cloudflare account, the repo includes a one-shot, idempotent provisioning script.

## One-shot setup

First authenticate Wrangler (this opens a browser), then run the setup script from the repo root:

```
npx wrangler login
node scripts/setup-cloudflare.mjs
```

The script verifies auth, creates (or reuses) the `flaremail` D1 database and `flaremail-mail` R2 bucket, applies migrations to the remote database, sets an `APP_SESSION_SECRET`, and deploys the Worker. Re-running it is safe — it reuses existing resources.

## Prefer to do it by hand?

```
wrangler d1 create flaremail          # paste database_id into wrangler.toml
wrangler r2 bucket create flaremail-mail
wrangler secret put APP_SESSION_SECRET
npm run migrate:remote
npm run deploy:api
```

When it's deployed, put your Worker's URL into FlareMail under **Settings → Connection**, then sign up or sign in from the app. For inbound mail, add the [Email Routing rule](/help/connect-cloudflare-email-routing/) pointing at your Worker.
