Coding agents can use cf to work with your Cloudflare account. Commands
return JSON and describe their own inputs, so an agent can find and run the
right command without prior knowledge of cf.
-
Install
cfglobally so every agent session can run it:npm install --global cfyarn global add cfpnpm add --global cfbun add --global cfInside a project that installs
cfas a dependency, the global command runs the project's copy. -
Sign in:
cf auth loginSign-in opens a browser page where you approve access, so complete this step yourself. For agents that run without a person present, refer to Authenticate unattended agents.
-
Tell your agent to prefer
cf. Add this instruction to your user-levelAGENTS.md,CLAUDE.md, or equivalent instructions file:AGENTS.mdmd When interacting with Cloudflare, use the `cf` CLI unless the project has a Wrangler configuration file.Projects that still use
wrangler.jsoncorwrangler.tomlkeep working with Wrangler, while every other Cloudflare task goes throughcf.
You can also ask your agent to do this setup for you:
Install the Cloudflare CLI with `npm install -g cf`. Then add this line to my
user-level AGENTS.md or CLAUDE.md file: "When interacting with Cloudflare, use
the cf CLI unless the project has a Wrangler configuration file."cf has more than 2,900 commands. Instead of reading help for every product,
an agent can describe the task, inspect the best match, and then run it.
-
Search for a command by describing the task:
cf cli search "create D1 database"The search runs locally and needs no credentials. Put the task in quotes.
cf cli searchtakes the whole task as one argument, and unquoted words after the first fail as unknown commands. It returns up to five matches as a JSON array, best match first. Each match has the command and a short summary. These are the first two matches for this search:[ { "command": "cf d1 create", "summary": "Create D1 Database" }, { "command": "cf d1 update", "summary": "Update D1 Database" } ] -
Inspect the API request of the chosen command:
cf schema d1 createThe JSON result describes the API request that the command sends:
operationId,httpMethod,path,pathParams,queryParams,hasRequestBody, andrequestBodyFields. Each parameter and body field lists its name, its type, and whether it is required. Body fields also include a description.cf schemacovers generated API commands. For a command's arguments and options, or for a command such ascf deploy, run the command with--help.Path and query parameters use their API names, such as
account_id.requestBodyFieldslists the body fields that the command also accepts as options, under the option names. A field inside an object gets a combined name, such asread-replication-modeforread_replication.mode, and array fields show the typestring. For some bodies, the list is incomplete or empty. For these commands, pass the complete body with--body, and check the API reference for its shape. -
Preview the request with
--dry-run, then run the command without it:cf d1 create --name my-database --dry-run
Root and group --help output starts with a reminder for agents to use
cf cli search first. A command's own --help shows its usage as plain text.
If an agent types a command that does not exist, cf lists the closest
matches.
You and your agent can use cf docs search to find guides, tutorials, and
examples across Cloudflare products. The search uses live Cloudflare
documentation and requires an Internet connection, but no Cloudflare login.
-
Describe the task or ask a question:
cf docs search "proxy a DNS record"Put the query in quotes. The command returns up to five ranked JSON results, each with a
title,description, andurl. -
Choose a result and fetch its URL with the
Accept: text/markdownheader to read the complete page as Markdown:curl -H "Accept: text/markdown" \ https://developers.cloudflare.com/learning-paths/prevent-ddos-attacks/baseline/proxy-dns-records/
Use cf cli search to find commands and cf docs search to find
documentation. Keep both kinds of search generic: do not include account IDs,
domains, email addresses, tokens, or other identifying information.
Use cf feedback send to report Cloudflare problems your agent encounters. To
ask your agent to look for blockers and workarounds across a repository, refer to
Review an existing project.
Results go to standard output as JSON. Messages, such as the selected zone,
pagination metadata, and errors go to standard error. When standard output is
not a terminal, it contains only the result, so an agent can parse it or filter
it with jq without extra flags.
- Lists print the page items as JSON on standard output (usually a JSON
array, or an object wrapping the items for some endpoints) and return one
page per call. Paging options differ between commands, so check the command's
--helpoutput. - Changes that return no data print nothing to standard output.
- Raw content, such as an R2 object or an image from a Workers AI model, goes to standard output unchanged. Redirect it to a file.
- JSON output is indented, whether or not standard output is a terminal. Colors are added only in a terminal.
A failed command exits with a non-zero status and prints the error to standard error.
To keep standard output clean JSON for jq and scripts, paginated cf list
commands print page items on standard output (stdout) and write API
pagination metadata to standard error (stderr) as a single line prefixed with
result_info::
result_info: {"page":1,"per_page":20,"count":20,"total_count":45,"total_pages":3}Omit --quiet (-q) when reading pagination metadata, because --quiet
suppresses the result_info: line. Do not merge stderr into stdout with
2>&1 when piping to jq or a JSON parser — capture stdout and stderr
separately so the result_info: line and other messages do not break JSON
parsing.
Cloudflare API endpoints do not all return the same result_info fields. Some
endpoints include total_pages, while others (such as cf d1 list) return
total_count and per_page — or only count or a cursor — without
total_pages, and some omit result_info altogether. When scripting
pagination, determine when to stop defensively:
- Use
total_pageswhen it is present inresult_info. - When
total_pagesis absent buttotal_countandper_pageare present, calculate the page count with ceiling division (Math.ceil(total_count / per_page)), or injq:(.total_pages // (((.total_count + .per_page - 1) / .per_page) | floor)). - Stop if a page returns an empty list (
[]), empty output, or fewer items than the page size (result_info.per_page, or the--per-pagevalue you requested). - For cursor-based commands, pass the
cursorvalue fromresult_infoon the next call, and stop whencursoris absent or empty.
The following example captures stdout and stderr separately and paginates
cf d1 list without assuming total_pages is present:
page=1
per_page=50
while :; do
items_file=$(mktemp)
err_file=$(mktemp)
if ! cf d1 list --page "$page" --per-page "$per_page" >"$items_file" 2>"$err_file"; then
cat "$err_file" >&2
rm -f "$items_file" "$err_file"
exit 1
fi
# Process page items from standard output
jq -r '.[].name' "$items_file"
count=$(jq 'length' "$items_file")
count=${count:-0}
result_info=$(sed -n 's/^result_info: //p' "$err_file" | tail -n 1)
rm -f "$items_file" "$err_file"
# Use total_pages when present, otherwise derive it from total_count and per_page
total_pages=$(printf '%s' "$result_info" | jq -r '
.total_pages // (
if (.total_count != null and (.per_page // 0) > 0)
then (((.total_count + .per_page - 1) / .per_page) | floor)
else empty
end
)
')
page_size=$(printf '%s' "$result_info" | jq -r '.per_page // empty')
page_size=${page_size:-$per_page}
if [ -n "$total_pages" ] && [ "$page" -ge "$total_pages" ]; then
break
fi
if [ "$count" -eq 0 ] || [ "$count" -lt "$page_size" ]; then
break
fi
page=$((page + 1))
done- Preview changes. Add
--dry-runto print the request as JSON without sending it. Dry runs need no credentials. - Check for aborted deletes. In a non-interactive session, a destructive
command without
--forceprintsAborted.to standard error, makes no change, and exits with status1. - Review
--forcebefore you allow it. On some commands,--forceis also an API parameter. For example,cf workers delete --forcealso deletes a Worker that other Workers still reference. - Use local data where supported.
--localworks only for a few resources that local development provides, mainly KV keys, D1 databases throughcf d1 raw,cf d1 migrations list, andcf d1 migrations apply, and R2 objects. Commands without a local equivalent, includingcf d1 query, return an error instead of calling the Cloudflare API.
For local state, refer to Local resource data.
Before your agent runs workloads on Cloudflare, consider setting spending limits or email alerts for Workers and Durable Objects. You can configure them with the cf CLI.
Each limit applies to the product across your account, not to an individual agent or project. Reaching a limit can interrupt other applications on the same account. Choose an email alert if you want notification without blocking usage.
To migrate a project with an agent, ask it to:
-
Preview the migration without writing files:
cf migrate --dry-run -
Run the migration:
cf migrate -
Resolve the
TODO(@cloudflare)comments in the generatedcloudflare.config.ts, and ask you about any choice it cannot make on its own. The build fails until every required item is resolved.
For the full process, refer to Migrate a Wrangler project.
Agents that run without a person present, such as in continuous integration
(CI), cannot complete cf auth login. Set CLOUDFLARE_API_TOKEN instead. The
token takes priority over any stored login. For the variables, token
permissions, and non-interactive behavior, refer to Use cf in CI.
To give an agent separate credentials for one project on your own machine, bind a named profile to the project directory. Refer to Use named profiles.
When cf dev runs inside a supported coding agent, the development server also
prints the URL and main routes of the
Local Explorer API.
The agent can use this API to read and change the data in your Worker's local
bindings, and to query local traces and logs.
- Agent setup configures Cloudflare Skills and MCP servers for popular coding agents.
- Use cf in CI covers API tokens and non-interactive behavior.