---
name: forkbase-setup
description: Set up Forkbase (the backend for AI agents) with the user, step by step - install it on this machine or a server, or connect to one already running, then build a new app on it or wire it into the app in this folder. Use when the user asks to set up, install, connect or integrate Forkbase, or pastes a prompt pointing at this file.
---

# Set up Forkbase

You are helping a developer set up **Forkbase**: a self-hosted backend for apps where AI agents write data. Postgres
tables over REST, auth, storage, realtime and jobs (like Supabase), plus a fork per agent run: an agent's changes
reach the data only when its merge policy allows them or a person approves them, and any merge can be undone.

Docs: every Forkbase server serves them, all in one file at `<FORKBASE_URL>/llms-full.txt`, and as skills at
`<FORKBASE_URL>/skills/` (Step 2.5 installs those).

## How to run this conversation

- **Ask one question at a time.** Wait for the answer before the next one. Use your question tool if you have one
  (Claude Code: AskUserQuestion), with 2 to 4 options and your recommendation first. Otherwise ask in one short line
  with numbered options.
- **Look before you ask.** If the folder, the shell or the prompt already answers a question (a `package.json`, a
  running server, a URL in the prompt), say what you found and skip it.
- **Say what you're about to do before anything that installs software, uses `sudo`, touches a server over SSH, or
  writes outside this folder.** Wait for a yes.
- **Never show a key or a password in the chat**, and never put the service_role key in code a browser gets. Write
  keys to `.env` (or `.env.local`), and make sure that file is git-ignored.
- After each step, say in one line what now works.

## Step 1. Where is Forkbase?

If the prompt names a Forkbase URL (for example "connect this project to my Forkbase at https://..."), check it:
`curl -s <url>/health` should answer `{"status": "ok", ...}`. If it does, go to Step 2c (keys into the env file), then Step 2.5 (skills), then Step 3.

Otherwise ask:

> Where should Forkbase run?
> 1. On this machine (recommended to start)
> 2. On a server I have (SSH)
> 3. It's already running: I have its URL

## Step 2. Install it

### 2a. On this machine

1. Check for it: `forkbase --version`. If it's missing, install (tell the user first):
   ```bash
   curl -fsSL https://github.com/2090-labs/forkbase/releases/latest/download/install.sh | FORKBASE_ENGINE=1 sh
   ```
   If that fails (no release for this platform yet), offer Docker (`deploy/` in the repository) or building from
   source (docs/getting-started.md).
2. Ask:
   > Which database should hold the data?
   > 1. The bundled Chronos engine (recommended: nothing else to run)
   > 2. A Postgres 16+ I already have
3. Make a `forkbase/` folder for its config (or use the one the user names) and run there:
   - Chronos: `forkbase init --db chronos`
   - Postgres: `forkbase init --db postgres`. Ask the user to write the database URL into `forkbase/database-url`
     themselves (it has a password; `chmod 600` it, and git-ignore it). Forkbase doesn't read `.env` files: it reads
     `forkbase.toml` and its own environment.
4. Postgres only: `export FORKBASE_DATABASE_URL_FILE="$PWD/database-url"` in the shell you use for the next steps:
   every `forkbase` command (`start`, `keys`, `status`) loads the config and needs it.
5. Start it in the background, logs to `forkbase/forkbase.log`: `forkbase start`.

   Wait for `curl -s http://127.0.0.1:8000/health` to say ok.
6. `forkbase keys` prints the keys as JSON. Write them into the app's env file without echoing them:
   `FORKBASE_URL=http://127.0.0.1:8000`, `FORKBASE_ANON_KEY=...`, `FORKBASE_SERVICE_KEY=...`.
7. Tell the user the dashboard is at http://127.0.0.1:8000/dashboard/ and they sign in with the service key from the
   env file.

### 2b. On a server

1. Ask for the SSH host (`user@host`) and the domain the API will be reached at (for example `api.myapp.com`).
2. Show the plan and wait for a yes: install Forkbase with the engine, write `/etc/forkbase/forkbase.toml`, a
   systemd service running `forkbase start` as its own user, and Caddy for HTTPS on the domain.
3. Ask which database (as in 2a). For Postgres, have the user write the URL to `/etc/forkbase/database-url` on the
   server themselves; point `FORKBASE_DATABASE_URL_FILE` at it.
4. Set `auth.external_url = "https://<domain>"` and `auth.site_url` to the app's address. Ask how email should go out;
   until SMTP is set, sign-in emails go to the server log.
5. Start it; check `https://<domain>/health`.
6. Keys: run `forkbase keys` on the server and copy them into the local env file over SSH without printing them. If
   you can't, ask the user to copy the anon and service keys from the dashboard's Settings page into the env file.

### 2c. Already running

Ask for the URL (skip this if the prompt gave it). Check `/health`. Ask the user to put the anon and service keys (dashboard, Settings) into the env
file: `FORKBASE_URL`, `FORKBASE_ANON_KEY`, `FORKBASE_SERVICE_KEY`. Don't ask them to paste keys into the chat.

## Step 2.5. Install the Forkbase skills (always, no question)

Forkbase is reachable now. Before writing any code, give this project the whole Forkbase manual, so you (and every
agent that opens this folder later) know every API, rule and recipe:

```bash
curl -fsSL "$FORKBASE_URL/skills/install.sh" | sh
```

It downloads three skills into `.claude/skills/`, served by the user's own server, so they match its version:

- `forkbase`: building apps on Forkbase. Its `SKILL.md` routes each task to a reference: every docs page plus
  `references/recipes.md` (sign-in, RLS, uploads, realtime, agents, jobs, webhooks, MCP, moving from Supabase).
- `forkbase-agent`: how an agent works in the data (run, check, propose, stop when held).
- `forkbase-setup`: this file.

Claude Code picks them up by itself. For Codex, Cursor and other agents, add this to `AGENTS.md` at the project root
(make the file if needed; keep what's there):

```markdown
## Forkbase
This app's backend is Forkbase. Before changing anything that touches it (tables, auth, storage, realtime, agents,
jobs), read `.claude/skills/forkbase/SKILL.md` and the reference it points to. Agents that change data follow
`.claude/skills/forkbase-agent/SKILL.md`.
```

From here on, **read the matching reference before each part you wire in** (`references/auth.md` before sign-in,
`references/agents.md` before agents, and so on), and follow the recipes rather than guessing. If `curl` can't reach
the server, fetch `$FORKBASE_URL/llms-full.txt` instead and say so.

Say in one line: "Forkbase skills installed in .claude/skills (forkbase, forkbase-agent, forkbase-setup)."

## Step 3. The app

Look at the folder first. Then ask:

> What are we doing?
> 1. Add Forkbase to the app in this folder (recommended when there is one)
> 2. Start a new app

### New app

Ask, one at a time:

1. "What are you building, in one sentence?" (Use the answer to name tables and the agent.)
2. "Which stack?" Next.js (recommended), a Node server, Python (FastAPI), or something else.
3. "Which parts do you need?" (multi-select) Sign-in, file storage, live updates, AI agents that change data,
   background jobs.

Scaffold the smallest app of that stack, then go on as below.

### Existing app

Read its `package.json` / `pyproject.toml`, framework, current database and auth. If it already uses Supabase,
Forkbase is a drop-in: point `SUPABASE_URL` / `SUPABASE_ANON_KEY` (or the app's names for them) at Forkbase and keep
the code. Ask which parts to add (as above), one question.

## Step 4. Wire it in

Do only the parts the user picked.

- **Clients.** Tables, auth, storage and realtime use supabase-js / supabase-py:
  `createClient(process.env.FORKBASE_URL, process.env.FORKBASE_ANON_KEY, { auth: { flowType: "pkce" } })`.
  Agents and jobs use the `forkbase` SDK (`npm i @chronosdb/forkbase` / `pip install forkbase`). Server code only gets the service key.
- **Tables.** Write the schema as a SQL migration file in the app (`db/001_init.sql` or the framework's place) and run
  it with the dashboard's SQL editor, `psql`, or `POST /platform/v1/sql` with the service key. For every table people
  reach: `grant` it to `authenticated` (and `anon` if public), `enable row level security`, and a policy using
  `auth.uid()` (it returns text).
- **Sign-in.** supabase-js auth with PKCE. Set `auth.site_url` (and `redirect_urls`) in `forkbase.toml` to the app's
  address; restart Forkbase.
- **Storage.** Make the bucket (`POST /storage/v1/bucket` with the service key) and a policy on `storage.objects`.
- **Live updates.** Add the table to `[realtime] tables` (or `POST /realtime/v1/tables`) and subscribe with
  `supabase.channel(...).on("postgres_changes", ...)`.
- **AI agents.** Ask: "What will the agent change, and what should always need a person?" Then:
  1. write `forkbase/policies/<name>.toml` from the answer (for example `tables = [...]`, `max_rows = 50`,
     `max_deletes = 0`, `review_tables = [...]`) and restart Forkbase;
  2. make the agent: `POST /agents/v1/agents {"name": "<name>", "policy": "<name>"}` with the service key; write its
     `fba_` token to the env file as `FORKBASE_AGENT_TOKEN`;
  3. write the agent's code with `fb.agents.withRun(intent, async (run) => { ... })`, and handle all three outcomes:
     `merged`, `held` (tell a person; don't retry), `conflict` (start again on fresh data).
- **Background jobs.** Put functions in `functions/<name>/index.ts` (or `main.py`); `import { step, sleep } from
  "@chronosdb/forkbase"`; deploy with `forkbase deploy functions/<name>`, add `--cron` or `--agent` as needed. No npm or pip
  dependencies are installed on the server: bundle with esbuild or use the standard library.

## Step 5. Connect your own tools (optional)

Ask: "Should I connect this coding agent to Forkbase over MCP, so it can work in the data through runs?" If yes:

- Claude Code: `claude mcp add --transport http forkbase <FORKBASE_URL>/mcp`
- Codex: add `[mcp_servers.forkbase]` with `url = "<FORKBASE_URL>/mcp"` to `~/.codex/config.toml`
- Cursor: `.cursor/mcp.json` with `{"mcpServers": {"forkbase": {"url": "<FORKBASE_URL>/mcp"}}}`

The browser opens Forkbase's consent page; the user picks which agent the client works as. The `forkbase-agent`
skill installed in Step 2.5 teaches the run, propose, stop-when-held loop.

## Step 6. Prove it works

Run each and show the result (never the keys):

1. `curl -s $FORKBASE_URL/health`
2. a read through the app's client (or `GET /rest/v1/<table>` with the anon key)
3. if agents were set up: one run that changes a row and proposes; show whether it `merged` or was `held`, and where
   to approve it (dashboard, Agents, Approvals)
4. the app's own dev server, if there is one

Then sum up in a few lines: where Forkbase runs, where its config and keys are, what was added to the app, that the
skills are in `.claude/skills` (and `AGENTS.md` points at them), the dashboard URL, and the one thing to do next.

## Rules

- One question per message. Never a list of questions at once.
- Never print, log or commit keys, tokens or database passwords.
- The service_role key never reaches a browser. Browsers get the anon key.
- Don't approve held runs for the user, and don't loosen a policy to make a run merge. Those are a person's calls.
- Forkbase is free to self-host under FSL-1.1-ALv2. Don't call it open source.
