cf-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cloudflare CLI (
cf
)

Cloudflare CLI (
cf
)

cf
is Cloudflare's command-line interface for the public Cloudflare API (2,900+ commands, most generated from API schemas) and for Workers projects. It is in beta: commands, configuration, and Build Output can change before the stable release.
Prefer retrieval over pre-trained knowledge. Describe the task to
cf cli search
, inspect the match with
cf schema
or
--help
, and preview with
--dry-run
before running a command you have not verified.
cf
是 Cloudflare 面向 Cloudflare 公共 API(2,900 多个命令,大多数由 API schema 生成)和 Workers 项目的命令行界面。目前处于测试版:在正式版发布前,命令、配置和 Build Output 都可能发生变化。
优先检索,不要依赖预训练知识。 向
cf cli search
描述任务,使用
cf schema
或
--help
检查匹配项,并在运行尚未验证的命令前通过
--dry-run
进行预览。

Documentation

文档

FIRST: check installation and sign in

首先:检查安装和登录状态

bash
npm install --global cf   # installs two equivalent commands: `cf` and `cloudflare`
cf --version
cf auth whoami
  • Requires Node.js 22.18 or later. Bun is not supported.
  • If another tool named
    cf
    is on your
    PATH
    , use
    cloudflare
    instead — the package installs both commands.
  • cf auth login
    prints a link and a one-time code, then opens the browser. On a remote machine, over SSH, or in a container, add
    --no-browser
    so
    cf
    only prints the link; approve it from another device. Add
    --force
    to sign in again.
  • cf
    keeps its own credentials and does not reuse a Wrangler login.
  • Unattended agents and CI cannot complete
    cf auth login
    — set
    CLOUDFLARE_API_TOKEN
    instead (see https://developers.cloudflare.com/cf/ci/index.md).
To make agents prefer
cf
, add this to the user-level
AGENTS.md
,
CLAUDE.md
, or equivalent:
md
When interacting with Cloudflare, use the `cf` CLI unless the project has a
Wrangler configuration file.
bash
npm install --global cf   # installs two equivalent commands: `cf` and `cloudflare`
cf --version
cf auth whoami
  • 需要 Node.js 22.18 或更高版本。不支持 Bun。
  • 如果
    PATH
    中已有另一个名为
    cf
    的工具,请改用
    cloudflare
    ——该软件包会同时安装这两个命令。
  • cf auth login
    会打印一个链接和一次性代码,然后打开浏览器。在远程机器、SSH 会话或容器中运行时,请添加
    --no-browser
    ,使
    cf
    仅打印链接;然后在另一台设备上完成授权。添加
    --force
    可重新登录。
  • cf
    会保存自己的凭据,不会复用 Wrangler 登录状态。
  • 无人值守 Agent 和 CI 无法完成
    cf auth login
    ——请改用
    CLOUDFLARE_API_TOKEN
    (参见 https://developers.cloudflare.com/cf/ci/index.md)。
要让 Agent 优先使用
cf
,请将以下内容添加到用户级
AGENTS.md
、
CLAUDE.md
或同类文件中:
md
When interacting with Cloudflare, use the `cf` CLI unless the project has a
Wrangler configuration file.

Find commands

查找命令

bash
cf cli search "create a DNS record"   # local, no credentials; quote the whole task; up to 5 JSON matches
cf schema d1 create                   # API request shape: method, path, params, body fields
cf <command> --help                   # arguments and options (e.g. cf deploy --help)
cf cli search
takes the task as one argument — unquoted words after the first are parsed as unknown commands.
cf schema
covers generated API commands; for command arguments and options use
--help
.
bash
cf cli search "create a DNS record"   # local, no credentials; quote the whole task; up to 5 JSON matches
cf schema d1 create                   # API request shape: method, path, params, body fields
cf <command> --help                   # arguments and options (e.g. cf deploy --help)
cf cli search
将任务作为一个参数接收——第一个参数之后未加引号的词会被解析为未知命令。
cf schema
涵盖由 schema 生成的 API 命令;如需了解命令参数和选项,请使用
--help
。

Read output

读取输出

  • Results are JSON on standard output; status messages and errors go to standard error, so piping to
    jq
    or redirecting needs no extra flags.
  • Lists print one JSON array page; paging options differ per command — check
    --help
    .
  • A failed command exits non-zero and prints the error to standard error.
  • 结果以 JSON 形式输出到标准输出;状态消息和错误输出到标准错误,因此管道传给
    jq
    或重定向输出时无需额外标志。
  • 列表每次打印一个 JSON 数组页面;分页选项因命令而异——请查看
    --help
    。
  • 命令失败时退出码非零,并将错误输出到标准错误。

Run commands safely

安全运行命令

  • Preview first.
    --dry-run
    prints the request as JSON without sending it, and needs no credentials.
  • Aborted deletes still exit 0. In a non-interactive session, a destructive command without
    --force
    prints
    Aborted.
    to standard error and exits
    0
    — a zero exit does not mean the resource was deleted.
  • Review
    --force
    before allowing it.
    On some commands
    --force
    is also an API parameter (for example,
    cf workers delete --force
    also deletes a Worker that other Workers reference).
  • --local
    is limited.
    It works for KV keys, D1 databases (
    cf d1 raw
    ,
    cf d1 migrations list|apply
    ), and R2 objects; other commands (including
    cf d1 query
    ) return an error instead of calling the API.
  • 先预览。
    --dry-run
    会以 JSON 形式打印请求但不予发送,并且不需要凭据。
  • 中止的删除操作仍会以 0 退出。 在非交互式会话中,破坏性命令若未带
    --force
    ,会向标准错误打印
    Aborted.
    ,并以
    0
    退出——退出码为 0 并不意味着资源已被删除。
  • 允许使用
    --force
    前应先审查。
    在某些命令中,
    --force
    同时也是 API 参数(例如,
    cf workers delete --force
    还会删除被其他 Worker 引用的 Worker)。
  • --local
    的支持范围有限。
    它适用于 KV 键、D1 数据库(
    cf d1 raw
    、
    cf d1 migrations list|apply
    )和 R2 对象;其他命令(包括
    cf d1 query
    )会返回错误,而不会调用 API。

Choose the CLI for this project

为项目选择 CLI

  • cloudflare.config.ts
    exists: use
    cf
    .
  • wrangler.jsonc
    ,
    wrangler.json
    , or
    wrangler.toml
    exists, and no
    cloudflare.config.ts
    : keep Wrangler for
    dev
    ,
    build
    , and
    deploy
    . Do not run
    cf dev
    ,
    cf build
    , or
    cf deploy
    there — they can generate a new configuration that ignores the Wrangler file, or fail.
    cf
    resource commands (DNS, zones, storage) are fine alongside it. Run
    cf migrate
    only when the user asks, and follow Migrate a Wrangler project: preview with
    cf migrate --dry-run
    , run
    cf migrate
    , then resolve the
    TODO(@cloudflare)
    comments in the generated
    cloudflare.config.ts
    .
  • Neither exists: use
    cf
    , as Use cf with coding agents recommends. Do not install Wrangler or create a Wrangler configuration file.
For Wrangler equivalents, use the Wrangler to cf reference or
cf cli search
. Do not translate Wrangler commands from memory. For the full comparison, refer to
cf
for Wrangler users
.
  • 存在
    cloudflare.config.ts
    :使用
    cf
    。
  • 存在
    wrangler.jsonc
    、
    wrangler.json
    或
    wrangler.toml
    ,但不存在
    cloudflare.config.ts
    :继续使用 Wrangler 执行
    dev
    、
    build
    和
    deploy
    。不要在该项目中运行
    cf dev
    、
    cf build
    或
    cf deploy
    ——这些命令可能生成忽略 Wrangler 配置文件的新配置,或者直接失败。可以同时使用
    cf
    资源命令(DNS、zones、storage)。仅当用户提出要求时运行
    cf migrate
    ,并遵循迁移 Wrangler 项目中的说明:先使用
    cf migrate --dry-run
    预览,再运行
    cf migrate
    ,最后处理生成的
    cloudflare.config.ts
    中的
    TODO(@cloudflare)
    注释。
  • 两者都不存在:使用
    cf
    ,正如将
    cf
    与编程 Agent 配合使用
    所建议的那样。不要安装 Wrangler 或创建 Wrangler 配置文件。
如需查找 Wrangler 的等价命令,请使用 Wrangler 到
cf
参考
或
cf cli search
。不要凭记忆转换 Wrangler 命令。有关完整比较,请参阅面向 Wrangler 用户的
cf
。

Commands
cf
does not support yet

cf
尚不支持的命令

cf
does not support every Wrangler command yet. Run these with
npx wrangler
instead of adding Wrangler to the project. Wrangler does not read
cloudflare.config.ts
, so pass the Worker name:
  • Live logs:
    npx wrangler tail <WORKER_NAME>
    .
  • A single secret:
    npx wrangler secret put <SECRET_NAME> --name <WORKER_NAME>
    . Or upload secrets with a new Worker version by passing
    --secrets-file <PATH>
    to
    cf deploy
    or
    cf workers versions create
    .
For the current list, refer to "Commands not yet supported" in the Wrangler to cf reference.
cf
目前尚不支持所有 Wrangler 命令。请使用
npx wrangler
运行以下命令,不要将 Wrangler 添加到项目中。Wrangler 无法读取
cloudflare.config.ts
,因此请传入 Worker 名称:
  • 实时日志:
    npx wrangler tail <WORKER_NAME>
    。
  • 单个 secret:
    npx wrangler secret put <SECRET_NAME> --name <WORKER_NAME>
    。也可以通过向
    cf deploy
    或
    cf workers versions create
    传入
    --secrets-file <PATH>
    ,使用新的 Worker 版本上传 secrets。
有关当前列表,请参阅 Wrangler 到
cf
参考
中的“尚未支持的命令”。