CLI
Codegen, SQL and diagnostics for better-supabase projects.
pnpm better-supabase <command> [options]| Command | What it does |
|---|---|
init | Writes the config, lib/supabase/index.ts and glue for the frameworks it finds |
add | Installs blocks, applies a preset (--preset), maps blocks onto existing tables (--adopt) or adds integration glue |
remove | Takes blocks out of the config and lib/blocks.ts and deletes their module files |
upgrade | Writes the upgrade steps and the module files again after a library or config change |
blocks | Lists, shows and searches the blocks you can add, the installed ones, and a preset's |
gen | Generates types, metadata and validators, or saves a schema snapshot with --save-snapshot |
dev | Writes env files, runs a command with the stack's keys, creates signing keys, and lists or prunes stacks |
db | Runs SQL files, renders typed seeds and diffs the schema into a migration |
spec | Writes, checks and validates the OpenAPI, AsyncAPI and Arazzo documents of a defineApi module |
config | Prints the resolved config, with every default filled in, or how it differs from its preset |
codemod | Rewrites imports, calls, config keys and CLI calls for renamed better-supabase APIs |
dev | Regenerates the project on every change, and works with the local stack |
cache | Deletes what the CLI caches between runs (cache purge) |
doctor | Checks RLS, indexes, drift, auth config and env files; text, JSON, SARIF or GitHub annotations |
mcp | Serves the CLI to an agent over MCP on stdio |
skills | Installs the Agent Skills that ship with the package |
completion | Prints the shell completion script for bash, zsh or fish |
login | Logs in to the Better Supabase cloud, with logout and whoami |
The package also installs the short bin bsb, so pnpm bsb gen runs the same
CLI as pnpm better-supabase gen.
Options you would pass on every run belong in the
config: a flag wins for one run, then
the active environment's block, then the config, then the default. With the
config filled in, better-supabase gen is the whole build step.
Every command takes --help. Commands that write files take --dry-run and
--check (writing files).
In a terminal, output is colored (pass --no-color or set NO_COLOR to turn
it off), init and add ask what they need, and database work shows a
spinner on stderr. Outside a terminal the spinner is one plain line, such as
Reading the schema....
A mistyped command gets a suggestion: Did you mean "gen"?.
Global options
| Option | Effect |
|---|---|
--cwd <dir> | Project directory. Defaults to the current one. supabase/config.toml is read from here or the nearest parent that has one (monorepos). |
--config <file> | Config file, relative to --cwd. Defaults to the nearest better-supabase.config.* in --cwd or a parent directory. |
--env <name> | Applies the config's environment block with that name. Defaults to $BETTER_SUPABASE_ENV, then ci when $CI is set. |
--json | Prints one result envelope on stdout, with the data, Problem Details for an error, and the next commands. Never prompts. |
--agent <mode> | Agent mode: auto (the default) turns it on when a coding agent runs the CLI, yes and no decide. Defaults to $BETTER_SUPABASE_AGENT. In agent mode, --json is on unless you pass --no-json, prompts are off, and destructive work waits for consent (agent mode). |
--effects-digest <digest> | Together with --yes, confirms the destructive work that a confirmation_required result described. The digest covers that command line only. |
--yes, -y | Answers yes to every confirmation and never prompts. In agent mode it also needs --effects-digest. |
--non-interactive | Never prompts, and fails with input_required when a value is missing. Already on in CI and agent mode. |
--color, --no-color | Prints no ANSI colors on either stream, like NO_COLOR. --color is the default; it colors a terminal only, never a pipe. |
--debug | Prints, on stderr, the stack trace of an error, each child command the CLI runs with its exit code, and how long the run and each step took. |
--help, -h | Prints the usage of the CLI, or of one command. |
--version | Prints the version. |
Prompts only run in a terminal. In CI ($CI is set), in agent mode, and
under --json, --yes or --non-interactive, commands use their flags and
defaults instead of asking.
Interaction, format, color and confirmation are four separate settings. A
pipe turns color off without turning prompts off, --json turns prompts off
without turning color on, and --yes answers confirmations without changing
the format. The CLI reads NO_COLOR, FORCE_COLOR and $CI in one place,
so every command follows the same rules.
Writing files
Every command that writes files prints one line per file:
Created src/lib/supabase/generated.ts
Updated supabase/schemas/900_better_supabase_audit.sql (audit)
Unchanged src/lib/supabase/database.types.ts
Skipped src/proxy.ts (exists; pass --force to overwrite)
Removed src/lib/supabase/old.ts| Flag | Effect |
|---|---|
--dry-run | Prints a unified diff of each file that would change, then the lines (Would create), and writes nothing |
--check | Writes nothing, and exits 1 with the diffs when a file would change (check_failed) |
--force | Overwrites files the CLI would skip |
The CLI writes a command's files together: it writes each one next to its
target first, then renames them all, so a failed run leaves no file half
written. It refuses a path outside the project root (outside_root). Files
that hold secrets, such as .env.local and the signing keys, are named in a
diff but never shown.
Two kinds of file are skipped. A file the CLI creates once, like
lib/supabase/index.ts, belongs to the app after that, so init and add
skip it when it exists. A file the CLI regenerates carries a
Generated by better-supabase header; when the file on disk has no header,
the app wrote it, so the command skips it and fails with file_conflict.
--force overwrites both. Under --json, data.files lists every file
with its outcome (create, update, unchanged, skip or remove).
Exit codes and JSON output
The CLI exits with 0 on success, 1 when a command fails or finds a problem
(--check drift, doctor errors), and 2 when the command or its input needs a
change: a usage error, a missing value (input_required), a confirmation the
user still owes (confirmation_required), or a login (auth_required). A
rerun fails the same way until someone changes the command. Interrupted runs
exit 130, a SIGTERM exits 143, and gen --metadata exits 65 when it rejects
its input. Every error has a reason, listed on the
errors page.
Stdout is the answer and stderr is everything else: the receipt, progress, warnings, errors and the next steps. A script can pipe stdout without filtering.
With --json, a run that succeeds prints one result document on stdout,
described by
cli-result-v2.json:
{
"$schema": "https://unpkg.com/better-supabase/schemas/cli-result-v2.json",
"status": "success",
"data": {
"files": [{ "path": "src/lib/supabase/generated.ts", "outcome": "update" }]
},
"next": [
{
"command": "better-supabase doctor",
"argv": ["better-supabase", "doctor"],
"when": "to check the result"
}
]
}| Field | Holds |
|---|---|
status | success for exit code 0, error for a report that exits 1 |
data | The command's result: gen gives { "tables", "written" }, doctor its report, blocks list --installed the modules |
warnings | Problems that did not stop the run |
next | The commands to run next, each as { "command", "argv", "when" } |
A run that fails prints the error document as one line on stderr and nothing on stdout, so a caller that parses stdout never reads half an answer:
{
"$schema": "https://unpkg.com/better-supabase/schemas/cli-result-v2.json",
"status": "error",
"reason": "unknown_command",
"message": "Unknown command \"genn\". Did you mean \"gen\"?",
"type": "https://bettersupabase.com/docs/cli/errors#unknown_command",
"next": [],
"data": { "suggestion": "gen" },
"userActionRequired": false,
"retryable": false
}reason is a stable name (errors), userActionRequired
tells an agent to ask a person, and retryable says that running the same
command again later can work. A report is not an error: doctor and
gen --check print their findings on stdout, exit 1 and write no error
document, so better-supabase gen --check --json | jq .data.stale stays
parseable. In a terminal the next commands print on stderr under
Next steps:, and each warning starts with !.
--help --json prints the command tree instead, described by
cli-commands-v2.json,
so an agent can learn the CLI without parsing help text. The document has the
CLI's name, version, the result schema and global options, and one
entry per command with its path, description, docs link, examples,
effects (the files, database and network access a run may have),
arguments and options. An option that takes a secret is marked secret.
After a command name it lists that command, or a group's subcommands:
better-supabase gen --help --json | jq '.commands[0].effects'A removed command or flag exits 2 and names its replacement, for example
--confirm was removed. Use --yes --effects-digest <digest> instead. The CLI
keeps no aliases.
Stdout only ever carries the command's output: its text, its envelope, or
the one file gen --metadata prints. Prompts, spinners, warnings and
--debug lines go to stderr, and each stream is colored only when it is a
terminal.
dev and spec emit --watch run until stopped. With --json they
print one result per run on its own line (NDJSON), so a script can read
the stream line by line:
better-supabase dev --json | while read -r line; do
echo "$line" | jq -r .status
doneThe same failure in a row prints once, and the watch retries every interval.
Environment
| Variable | Used by |
|---|---|
DATABASE_URL | gen, doctor and db seed --yes, when nothing else is set |
SUPABASE_DB_URL | The same commands, when DATABASE_URL is unset too |
POSTGRES_URL_NON_POOLING | The same commands, when both are unset: the direct connection the Vercel Marketplace integration sets |
POSTGRES_URL | The same commands, after POSTGRES_URL_NON_POOLING: the pooled connection from the integration |
SUPABASE_ACCESS_TOKEN | --project-ref and database.source.projectRef |
SUPABASE_API_URL | Another Management API host |
SUPABASE_BIN | The Supabase CLI dev env runs, instead of node_modules/.bin/supabase or supabase on the PATH |
CI | Turns prompts off, and applies the config's ci environment |
BETTER_SUPABASE_ENV | The config environment to apply when --env is not passed |
BETTER_SUPABASE_AGENT | Agent mode when --agent is not passed: auto, yes or no |
The CLI checks these once per run; a malformed URL stops it with
env_invalid. Connection strings hold the
database password, so no command takes one as an argument. Set
$DATABASE_URL, or $SUPABASE_DB_URL (the name the Supabase CLI and
better-supabase dev env use), or pipe one in with --db-url-stdin. When both
are set, $DATABASE_URL wins. After vercel env pull or under
vercel env run, the integration's $POSTGRES_URL_NON_POOLING and
$POSTGRES_URL are read last, so a shell that exports them never beats a
DATABASE_URL you set on purpose. The CLI never loads .env files itself.
Programmatic use
The CLI never calls process.exit. run returns the exit code and output,
which is how the tests drive it:
import { run } from "better-supabase/cli";
const { code, stdout, stderr } = await run(["gen", "--check"], {
env: process.env,
});run reads no process globals: it sees only the env you pass (none by
default), and --cwd resolves against the cwd option. Pass io to
receive the output as it is written; io.color and io.stderrColor color
each stream, and io.spinner shows progress. help() returns the
usage text, and help(command) the text for one command.
Add your own commands with defineCliCommand and pass them to createCli,
which returns a CLI with them next to the built-in ones. Commands are
citty definitions: args declares the
options, and run gets the parsed arguments and the context (cwd,
config, io, env, json, signal, log, paint and emit). Throw a
CliError to report a coded error, and return data to give --json a
document. Write messages with log.info, log.warn and log.debug, and
wrap slow work in log.progress(message, task): all of it goes to stderr, so
stdout keeps only the result. paint colors text for stdout and is plain
under --no-color. A command that runs until stopped prints each result
with emit and returns { code: 0, streamed: true }. Options listed in
lists may repeat or take commas, and list() splits them. Set runsWithInvalidConfig: true on a
command that repairs the config: when the config file fails to load, it runs
with the default config rooted at that file's directory instead of reporting
config_invalid.
defineCliCommand marks each command with apiVersion: 1, the command
contract this release supports. createCli refuses a command without it, a
name a built-in command already has, and the names kept free for account
commands (login, logout, whoami, link, projects, deploy and
cloud).
import { createCli, defineCliCommand, list } from "better-supabase/cli";
const cli = createCli({
commands: {
schemas: defineCliCommand({
meta: {
name: "schemas",
description: "Prints the schemas codegen reads",
},
args: { only: { type: "string", description: "Schemas to keep" } },
lists: ["only"],
run: async (args, { config }) => {
const only = list(args.only);
const schemas = config.schemas.filter(
(name) => only.length === 0 || only.includes(name),
);
return { code: 0, output: schemas.join("\n") };
},
}),
},
});
const { code } = await cli.run(process.argv.slice(2), {
cwd: process.cwd(),
env: process.env,
});
process.exitCode = code;Last updated on