A command-line tool for managing Cloudflare DNS records, built with Go using the Cobra framework.
# Global install (requires sudo)
curl -fsSL https://raw.githubusercontent.com/coollabsio/cloudflare-cli/main/scripts/install.sh | bash
# User install (no sudo required)
curl -fsSL https://raw.githubusercontent.com/coollabsio/cloudflare-cli/main/scripts/install.sh | bash -s -- --usergo install github.com/coollabsio/cloudflare-cli@latestThis will install the cf binary in your $GOPATH/bin directory (usually ~/go/bin). Make sure this directory is in your $PATH.
git clone https://github.com/coollabsio/cloudflare-cli.git
cd cloudflare-cli
go build -o cf .Create an API token in the Cloudflare Dashboard with the following permissions:
- Zone:Read - For listing zones and zone details
- DNS:Read - For listing/viewing DNS records
- DNS:Edit - For creating/updating/deleting records
cf auth save <your-api-token>This saves the token to ~/.cloudflare/config.yaml.
export CLOUDFLARE_API_TOKEN=your-api-token
# or
export CF_API_TOKEN=your-api-tokenexport CLOUDFLARE_API_KEY=your-api-key
export CLOUDFLARE_API_EMAIL=your-emailcf auth verifycf auth verify- Verify API credentialscf auth save <token>- Save API token to config file
cf config set <key> <value>- Set a config valuecf config get <key>- Get a config valuecf config list- List all config values
Available config keys:
output_format- Default output format (tableorjson)
cf zones list- List all zonescf zones get <zone-name-or-id>- Get zone details
cf dns list <zone>- List DNS records--type, -t- Filter by record type (A, AAAA, CNAME, TXT, MX, etc.)--name, -n- Filter by record name--search, -s- Search in name, content, and comment (case-insensitive)
cf dns get <zone> <record-id>- Get DNS record detailscf dns create <zone>- Create a DNS record--type, -t- Record type (required)--name, -n- Record name (required)--content, -c- Record content (required)--ttl- TTL in seconds (1 = auto, default: 1)--proxied- Proxy through Cloudflare (true|false)--priority- Record priority (for MX, SRV)--comment- Comment for the record
cf dns update <zone> <record-id>- Update a DNS record- Only specify fields you want to change
--type, -t- New record type--name, -n- New record name--content, -c- New record content--ttl- TTL in seconds--proxied- Set proxy status (true|false)--priority- Record priority--comment- Comment for the record (use empty string to clear)
cf dns delete <zone> <record-id>- Delete a DNS recordcf dns find <zone>- Find DNS records by name and/or type--type, -t- Record type to find--name, -n- Record name to find
All commands support these global flags:
--config- Config file path (default:~/.cloudflare/config.yaml)--output, -o- Output format:table(default) orjson
# List all zones
cf zones list
# Get zone details
cf zones get example.com
# Get zone by ID (useful for zone-specific tokens)
cf zones get 023e105f4ecef8ad9ca31a8372d0c353# List all DNS records for a zone
cf dns list example.com
# List only A records
cf dns list example.com --type A
# List records matching a name
cf dns list example.com --name www
# Search records by name, content, or comment
cf dns list example.com --search "production"
# Create an A record
cf dns create example.com --name www --type A --content 192.0.2.1
# Create a proxied CNAME record
cf dns create example.com --name blog --type CNAME --content example.com --proxied
# Create an MX record with priority
cf dns create example.com --name mail --type MX --content mail.example.com --priority 10
# Create a record with a comment
cf dns create example.com --name api --type A --content 192.0.2.10 --comment "Production API server"
# Update only the content of a record
cf dns update example.com abc123def456 --content 192.0.2.2
# Enable proxying on an existing record
cf dns update example.com abc123def456 --proxied
# Disable proxying
cf dns update example.com abc123def456 --proxied=false
# Update the comment on a record
cf dns update example.com abc123def456 --comment "Updated comment"
# Clear the comment on a record
cf dns update example.com abc123def456 --comment ""
# Delete a record
cf dns delete example.com abc123def456
# Find record ID by name and type
cf dns find example.com --name www --type A# Get JSON output for scripting
cf dns list example.com --output json
# Set JSON as default output format
cf config set output_format jsonIf you create an API token scoped to specific zones (not "All zones"), you cannot list zones or look up zone IDs by name. The API returns a permission error.
Workarounds:
-
Use the zone ID directly instead of the zone name:
cf dns list 023e105f4ecef8ad9ca31a8372d0c353
-
Grant your token "All zones" read permission for zone listing
The CLI accepts both zone names and zone IDs for all commands.
The config file is stored at ~/.cloudflare/config.yaml:
api_token: your-api-token-here
output_format: tableEnvironment variables take precedence over config file values.
# Build
go build -o cf .
# Run tests
go test ./...
# Run with coverage
go test -cover ./...
# Install locally
go install .cf/
├── main.go # Entry point
├── cmd/
│ ├── root.go # CLI setup, global flags
│ ├── auth.go # auth verify/save commands
│ ├── config.go # config set/get/list commands
│ ├── zones.go # zones list/get commands
│ └── dns.go # dns list/get/create/update/delete/find commands
├── internal/
│ ├── client/
│ │ └── client.go # Cloudflare API client wrapper
│ ├── config/
│ │ └── config.go # Configuration management
│ └── output/
│ └── output.go # Table/JSON output formatting
├── go.mod
└── go.sum
- cloudflare-go - Official Cloudflare Go library
- cobra - CLI framework
- yaml.v3 - YAML configuration
This CLI currently focuses on DNS management, but additional Cloudflare API features are planned:
- DNS - Record management (current)
- Zones - Zone settings, purge cache, development mode
- SSL/TLS - Certificate management, SSL settings
- Firewall - WAF rules, IP access rules, rate limiting
- Page Rules - URL-based settings
- Workers - Deploy and manage Workers scripts
- R2 - Object storage management
- D1 - Database management
- KV - Key-Value storage
- Pages - Cloudflare Pages deployments
- Load Balancing - Pool and monitor management
- Access - Zero Trust access policies
Contributions welcome!
MIT