Skip to content

Manage resources from the command line

Last updated View as MarkdownAgent setup

cf manages Cloudflare resources directly, without a Workers project. This guide uses DNS records to show a pattern that most resource commands follow: find a resource, list what it contains, preview a change, make the change, and clean up.

Before you begin

  • Install cf and sign in. If you use an API token, it needs permission to edit DNS records in the zone.
  • Add a domain to your Cloudflare account. This guide uses example.com.
  • (Optional) Install jq ↗︎ to filter JSON output.

1. Find a zone

List the zones you can access:

cf zones list

The result is a JSON array. To look up one domain, filter by name:

cf zones list --name example.com

Note the id of the zone. Zone-scoped commands accept the zone ID or the domain name. For details, refer to Select a zone.

2. List DNS records

List the DNS records in the zone:

cf dns records list --zone example.com

Options narrow the results. For example, list only A records:

cf dns records list --zone example.com --type A

cf dns records list returns one page of results. To get more, use --page and --per-page.

3. Preview a change

Add --dry-run to see the request a command would send. A dry run prints the method, URL, and body as JSON, and exits without sending anything. It does not need credentials.

cf dns records create --zone <ZONE_ID> --body '{"type":"A","name":"test","content":"192.0.2.1","proxied":true}' --dry-run
{
	"command": "cf dns records create",
	"method": "POST",
	"url": "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/dns_records",
	"pathParams": {
		"zone-id": "<ZONE_ID>"
	},
	"query": {},
	"bodyKind": "json",
	"body": {
		"type": "A",
		"name": "test",
		"content": "192.0.2.1",
		"proxied": true
	}
}

Use the zone ID in a dry run. A dry run does not look up domain names, so the preview would show the domain name where the ID belongs.

--body takes the JSON request body. Some operations, including this one, accept their input only through --body. For the fields the body accepts, refer to the Create DNS Record API reference.

4. Create the record

Run the same command without --dry-run:

cf dns records create --zone example.com --body '{"type":"A","name":"test","content":"192.0.2.1","proxied":true}'

cf prints the new record as JSON, including its id.

5. Filter the output

Because results are JSON on standard output, you can pipe them to jq. Print the name and content of each A record:

cf dns records list --zone example.com --type A | jq -r '.[] | "\(.name) \(.content)"'

Get the ID of the record you created:

cf dns records list --zone example.com --name test.example.com | jq -r '.[0].id'

6. Delete the record

Delete the record by its ID:

cf dns records delete <RECORD_ID> --zone example.com

cf asks you to confirm before it deletes anything. The default answer is no.

In a non-interactive session, such as a script or CI job, cf cannot ask. It prints the question and Aborted., makes no change, and exits with status 0. To delete without confirmation, pass --force:

cf dns records delete <RECORD_ID> --zone example.com --force

7. Find other commands

Describe a task to find the command for it:

cf cli search "purge cached files for a URL"

cf cli search prints up to five matching commands as JSON, best match first. Each match includes the command and a short summary. It runs locally and does not need credentials.

To see the arguments and options a command accepts, add --help:

cf cache purge --help

To see the API request a generated command sends, including its method, path, parameters, and body fields, pass it to cf schema:

cf schema zones create

To browse commands by product, add --help to cf, to a product such as cf dns, or to any command.

Next steps

Was this helpful?