Self-host MemFree on Cloudflare
MemFree is open source, and the whole app runs as one Cloudflare Worker. This guide shows how to deploy your own copy from the MemFree repository.
Architecture
| Piece | What it uses |
|---|---|
| App | One Cloudflare Worker (Next.js App Router on vinext) |
| Database | PostgreSQL with the pgvector extension (for example Elefork), reached through Cloudflare Hyperdrive |
| Embeddings | Workers AI @cf/baai/bge-m3 (1024 dimensions, multilingual) |
| Files | R2 (chat attachments and generated images) |
| LLM | Agnes: agnes-3.0-flash for answers, agnes-image-2.5-flash for images |
| Search and page reading | TinyFish Search and Fetch; DeepSeek Fetch as an optional second page reader |
| Image and video results | Serper (optional) |
| Sign-in | Google and GitHub OAuth |
All configuration lives in cloudflare.config.ts. The database schema is in db/migrations/, and database scripts are in scripts/db/.
Prerequisites
- A Cloudflare account (Workers, Hyperdrive, R2 and Workers AI).
- A PostgreSQL database with
pgvector, for example Elefork. - Agnes API access (one or more API keys).
- A TinyFish API key.
- Optional: a DeepSeek API key (page reading fallback) and a Serper key (image and video results).
- A Google OAuth client and a GitHub OAuth app. Set their callback URLs to
https://<your-domain>/api/auth/callback/googleandhttps://<your-domain>/api/auth/callback/github. - Node.js, pnpm and Docker (Docker only for local development).
1. Install
git clone https://github.com/memfreeme/memfree.git
cd memfree
pnpm install
2. Create the database schema
Run the migrations against your Postgres. They enable pgvector and create all tables; running them again is safe.
DATABASE_URL="postgresql://<user>:<password>@<host>:5432/<database>" pnpm db:migrate
3. Create a Hyperdrive config
The Worker connects to Postgres through Hyperdrive. Create a Hyperdrive config for your database in the Cloudflare dashboard (Storage & databases → Hyperdrive), or with the CLI:
npx cf hyperdrive create --name memfree-db \
--origin-host <host> --origin-port 5432 --origin-database <database> \
--origin-user <user> --origin-password <password>
Turning query caching off is recommended, so users always see their latest history. Copy the Hyperdrive id and set it in cloudflare.config.ts:
HYPERDRIVE: bindings.hyperdrive({
id: "<your-hyperdrive-id>",
dev: { connectionString: "postgresql://postgres:dev@127.0.0.1:55440/postgres" },
}),
4. About the Agnes proxy
cloudflare.config.ts contains an AGNES_PROXY binding (a Workers VPC service). It is specific to the hosted memfree.me, which reaches Agnes through its own proxy. You don't need it. Remove the AGNES_PROXY line (it points to a VPC service that only exists in the memfree.me account) and leave AGNES_PROXY_SECRET empty. Without AGNES_PROXY_SECRET, the Worker calls Agnes directly at AGNES_BASE_URL with AGNES_API_KEYS.
If Agnes rejects requests from Cloudflare's network in your setup, you can run your own HTTPS proxy that forwards /v1/chat/completions and /v1/images/generations, and set AGNES_BASE_URL to it.
5. Secrets
The Worker reads these secrets (the bindings.secret() entries in cloudflare.config.ts):
| Secret | Required | Purpose |
|---|---|---|
AGNES_API_KEYS | Yes, unless you use the proxy | Agnes API keys, comma-separated; requests are spread across them |
AGNES_PROXY_SECRET | No | Shared secret for the Agnes proxy; leave empty to call Agnes directly |
AUTH_SECRET | Yes | Signs session cookies; generate with openssl rand -base64 32 |
AUTH_GOOGLE_ID | Yes | Google OAuth client id |
AUTH_GOOGLE_SECRET | Yes | Google OAuth client secret |
AUTH_GITHUB_ID | Yes | GitHub OAuth app client id |
AUTH_GITHUB_SECRET | Yes | GitHub OAuth app client secret |
TINYFISH_API_KEY | Yes | Web search and page reading |
DEEPSEEK_API_KEY | No | DeepSeek Fetch, used as a fallback page reader |
SERPER_API_KEY | No | Image and video results; leave empty to turn them off |
REINDEX_TOKEN | No | Admin token for scripts/db/reindex-kb.mjs; without it /api/kb/reindex returns 404 |
Plain settings such as the model names, AGNES_BASE_URL and the daily limits (DAILY_LIMIT_ANON = 50, DAILY_LIMIT_USER = 300) are set as text bindings in cloudflare.config.ts; change them there if you like.
Put the secrets in a JSON file outside the repository (or in a gitignored file) for the first deploy:
{
"AGNES_API_KEYS": "<key1>,<key2>",
"AGNES_PROXY_SECRET": "",
"AUTH_SECRET": "<random string>",
"AUTH_GOOGLE_ID": "<google client id>",
"AUTH_GOOGLE_SECRET": "<google client secret>",
"AUTH_GITHUB_ID": "<github client id>",
"AUTH_GITHUB_SECRET": "<github client secret>",
"TINYFISH_API_KEY": "<tinyfish key>",
"DEEPSEEK_API_KEY": "",
"SERPER_API_KEY": "",
"REINDEX_TOKEN": ""
}
Never commit this file.
6. Deploy
Sign in to Cloudflare with the CLI, then build and deploy. The first deploy uploads the secrets and provisions the R2 bucket:
pnpm build && npx cf deploy --prebuilt --secrets-file <secrets.json>
Later deploys keep the existing secrets:
pnpm build && npx cf deploy --prebuilt
To change a single secret afterwards, use npx cf workers secrets update <NAME>. To serve the Worker on your own domain, add it under domains in cloudflare.config.ts (or in the Cloudflare dashboard) and make sure your OAuth callback URLs use that domain.
Local development
-
Start Postgres with pgvector in Docker, on the port the
devconnection string incloudflare.config.tsexpects:docker run -d --name memfree-pg -e POSTGRES_PASSWORD=dev -p 55440:5432 pgvector/pgvector:pg18 DATABASE_URL="postgresql://postgres:dev@127.0.0.1:55440/postgres" pnpm db:migrate -
Create a
.dev.varsfile in the repository root with the same secret names, oneNAME=valueper line (.dev.varsis gitignored):AGNES_API_KEYS=<key> AUTH_SECRET=<random string> AUTH_GOOGLE_ID=<google client id> AUTH_GOOGLE_SECRET=<google client secret> AUTH_GITHUB_ID=<github client id> AUTH_GITHUB_SECRET=<github client secret> TINYFISH_API_KEY=<tinyfish key>For local sign-in, add
http://localhost:<port>/api/auth/callback/googleto your Google OAuth client. A GitHub OAuth app has only one callback URL, so create a separate GitHub app for local development. -
Start the dev server:
pnpm dev
Workers AI has no local emulation, so embeddings for the knowledge base call Cloudflare even in local development. Stay signed in to the Cloudflare CLI.
Help
If you get stuck, ask in Discord, open an issue on GitHub, or email support@memfree.me.
Get accurate answers, faster
Search