Skip to content

Cloudflare

Wrangler is the official CLI for Cloudflare. Manage Workers, Pages, DNS, WAF, and deploy applications at the edge.

Basic Commands

CommandDescription
wrangler --versionShow Wrangler CLI version
wrangler --helpDisplay help information
wrangler init [name]Initialize new Wrangler project
wrangler loginAuthenticate with Cloudflare account
wrangler logoutRemove Cloudflare authentication
wrangler whoamiDisplay current authenticated user
wrangler publishDeploy Worker to Cloudflare
wrangler devRun local development server
wrangler tailStream real-time logs
wrangler deleteDelete deployed Worker
wrangler list [secrets|kv|namespaces]List resources
wrangler statusDisplay deployment status

Installation

Linux/Ubuntu

# Using npm/Node.js
npm install -g wrangler

# Using yarn
yarn global add wrangler

# Using pnpm
pnpm add -g wrangler

# Verify installation
wrangler --version

macOS

# Using Homebrew
brew install wrangler

# Using npm
npm install -g wrangler

# Using MacPorts
sudo port install wrangler

# Verify installation
wrangler --version

Windows

# Using npm (recommended)
npm install -g wrangler

# Using Chocolatey
choco install wrangler

# Using Scoop
scoop install wrangler

# Verify installation
wrangler --version

Wrangler Configuration

CommandDescription
wrangler initCreate wrangler.toml configuration file
wrangler --env [env]Use specific environment config
wrangler publish --env [env]Deploy to specific environment
wrangler dev --localRun development server without Cloudflare
wrangler dev --remoteRemote development with live data
wrangler kv:namespace create [name]Create KV namespace
wrangler kv:key put [namespace] [key] [value]Set KV key-value
wrangler kv:key get [namespace] [key]Get KV value

Cloudflare Workers Commands

Deployment

# Deploy Worker to production
wrangler publish

# Deploy to specific route
wrangler publish --routes example.com/*

# Deploy to specific environment
wrangler publish --env staging

# Publish with tail logs
wrangler publish && wrangler tail

# Schedule deployment (at specific time)
wrangler publish --compatibility-date 2024-01-01

# Deploy without publishing
wrangler publish --dry-run

Development & Testing

# Start local development server
wrangler dev

# Local dev with custom port
wrangler dev --port 8787

# Remote development with live data
wrangler dev --remote

# Run with IP address binding
wrangler dev --ip 0.0.0.0

# Enable inspector/debugger
wrangler dev --inspect

# Watch mode (auto-reload)
wrangler dev --watch

Logging & Monitoring

# Stream live logs from deployed Worker
wrangler tail

# Stream logs from specific environment
wrangler tail --env production

# Stream with format options
wrangler tail --format pretty

# Follow logs (keep connection open)
wrangler tail --follow

# Get deployment history
wrangler deployments list

# Rollback to previous deployment
wrangler rollback

Secrets & Environment Variables

Managing Secrets

# Add secret to Worker
wrangler secret put SECRET_NAME

# Add secret from file
wrangler secret put SECRET_NAME < secret.txt

# List all secrets (names only)
wrangler secret list

# Delete secret
wrangler secret delete SECRET_NAME

# Add to specific environment
wrangler secret put API_KEY --env production

# Bulk import secrets from JSON
wrangler secret:bulk secrets.json

KV Storage (Cache/Data)

# Create KV namespace
wrangler kv:namespace create my-cache

# Create preview namespace
wrangler kv:namespace create my-cache --preview

# Set key-value pair
wrangler kv:key put my-cache mykey "myvalue"

# Get value by key
wrangler kv:key get my-cache mykey

# Delete key
wrangler kv:key delete my-cache mykey

# List all keys in namespace
wrangler kv:key list my-cache

# Bulk import JSON to KV
wrangler kv:key put-bulk my-cache data.json

# Export KV to JSON
wrangler kv:key list my-cache --prefix=user_

Monitoring and Logging

System Monitoring

# Monitor system resources
cloudflare monitor --system

# Monitor specific process
cloudflare monitor --pid <pid>

# Monitor network activity
cloudflare monitor --network

# Monitor file changes
cloudflare monitor --files <directory>

# Real-time monitoring
cloudflare monitor --real-time --interval 1

# Generate monitoring report
cloudflare report --type monitoring --output <file>

# Set monitoring alerts
cloudflare alert --threshold <value> --action <action>

# View monitoring history
cloudflare history --type monitoring

Logging

# View logs
cloudflare logs

# View logs with filter
cloudflare logs --filter <pattern>

# Follow logs in real-time
cloudflare logs --follow

# Set log level
cloudflare logs --level <level>

# Rotate logs
cloudflare logs --rotate

# Export logs
cloudflare logs --export <file>

# Clear logs
cloudflare logs --clear

# Archive logs
cloudflare logs --archive <archive_file>

Troubleshooting

Common Issues

Issue: Command not found

# Check if cloudflare is installed
which cloudflare
cloudflare --version

# Check PATH variable
echo $PATH

# Reinstall if necessary
sudo apt reinstall cloudflare
# or
brew reinstall cloudflare

Issue: Permission denied

# Run with elevated privileges
sudo cloudflare <command>

# Check file permissions
ls -la $(which cloudflare)

# Fix permissions
chmod +x /usr/local/bin/cloudflare

# Check ownership
sudo chown $USER:$USER /usr/local/bin/cloudflare

Issue: Configuration errors

# Validate configuration
cloudflare config validate

# Reset to default configuration
cloudflare config reset

# Check configuration file location
cloudflare config show --file

# Backup current configuration
cloudflare config export > backup.conf

# Restore from backup
cloudflare config import backup.conf

Issue: Service not starting

# Check service status
cloudflare status --detailed

# Check system logs
journalctl -u cloudflare

# Start in debug mode
cloudflare start --debug

# Check port availability
netstat -tulpn|grep <port>

# Kill conflicting processes
cloudflare killall --force

Debug Commands

CommandDescription
cloudflare --debugEnable debug output
cloudflare --verboseEnable verbose logging
cloudflare --traceEnable trace logging
cloudflare testRun built-in tests
cloudflare doctorRun system health check
cloudflare diagnoseGenerate diagnostic report
cloudflare benchmarkRun performance benchmarks
cloudflare validateValidate installation and configuration

Performance Optimization

Resource Management

# Set memory limit
cloudflare --max-memory 1G <command>

# Set CPU limit
cloudflare --max-cpu 2 <command>

# Enable caching
cloudflare --cache-enabled <command>

# Set cache size
cloudflare --cache-size 100M <command>

# Clear cache
cloudflare cache clear

# Show cache statistics
cloudflare cache stats

# Optimize performance
cloudflare optimize --profile <profile>

# Show performance metrics
cloudflare metrics

Parallel Processing

# Enable parallel processing
cloudflare --parallel <command>

# Set number of workers
cloudflare --workers 4 <command>

# Process in batches
cloudflare --batch-size 100 <command>

# Queue management
cloudflare queue add <item>
cloudflare queue process
cloudflare queue status
cloudflare queue clear

Integration

Scripting

#!/bin/bash
# Example script using cloudflare

set -euo pipefail

# Configuration
CONFIG_FILE="config.yaml"
LOG_FILE="cloudflare.log"

# Check if cloudflare is available
if ! command -v cloudflare &> /dev/null; then
    echo "Error: cloudflare is not installed" >&2
    exit 1
fi

# Function to log messages
log() \\\\{
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"|tee -a "$LOG_FILE"
\\\\}

# Main operation
main() \\\\{
    log "Starting cloudflare operation"

    if cloudflare --config "$CONFIG_FILE" run; then
        log "Operation completed successfully"
        exit 0
    else
        log "Operation failed with exit code $?"
        exit 1
    fi
\\\\}

# Cleanup function
cleanup() \\\\{
    log "Cleaning up"
    cloudflare cleanup
\\\\}

# Set trap for cleanup
trap cleanup EXIT

# Run main function
main "$@"

API Integration

Environment Variables

VariableDescriptionDefault
CLOUDFLARE_CONFIGConfiguration file path~/.cloudflare/config.yaml
CLOUDFLARE_HOMEHome directory~/.cloudflare
CLOUDFLARE_LOG_LEVELLogging levelINFO
CLOUDFLARE_LOG_FILELog file path~/.cloudflare/logs/cloudflare.log
CLOUDFLARE_CACHE_DIRCache directory~/.cloudflare/cache
CLOUDFLARE_DATA_DIRData directory~/.cloudflare/data
CLOUDFLARE_TIMEOUTDefault timeout30s
CLOUDFLARE_MAX_WORKERSMaximum workers4

Configuration File

# ~/.cloudflare/config.yaml
version: "1.0"

# General settings
settings:
  debug: false
  verbose: false
  log_level: "INFO"
  log_file: "~/.cloudflare/logs/cloudflare.log"
  timeout: 30
  max_workers: 4

# Network configuration
network:
  host: "localhost"
  port: 8080
  ssl: true
  timeout: 30
  retries: 3

# Security settings
security:
  auth_required: true
  api_key: ""
  encryption: "AES256"
  verify_ssl: true

# Performance settings
performance:
  cache_enabled: true
  cache_size: "100M"
  cache_dir: "~/.cloudflare/cache"
  max_memory: "1G"

# Monitoring settings
monitoring:
  enabled: true
  interval: 60
  metrics_enabled: true
  alerts_enabled: true

Examples

Basic Workflow

# 1. Initialize cloudflare
cloudflare init

# 2. Configure basic settings
cloudflare config set port 8080

# 3. Start service
cloudflare start

# 4. Check status
cloudflare status

# 5. Perform operations
cloudflare run --target example.com

# 6. View results
cloudflare results

# 7. Stop service
cloudflare stop

Advanced Workflow

# Comprehensive operation with monitoring
cloudflare run \
  --config production.yaml \
  --parallel \
  --workers 8 \
  --verbose \
  --timeout 300 \
  --output json \
  --log-file operation.log

# Monitor in real-time
cloudflare monitor --real-time --interval 5

# Generate report
cloudflare report --type comprehensive --output report.html

Automation Example

#!/bin/bash
# Automated cloudflare workflow

# Configuration
TARGETS_FILE="targets.txt"
RESULTS_DIR="results/$(date +%Y-%m-%d)"
CONFIG_FILE="automation.yaml"

# Create results directory
mkdir -p "$RESULTS_DIR"

# Process each target
while IFS= read -r target; do
    echo "Processing $target..."

    cloudflare \
        --config "$CONFIG_FILE" \
        --output json \
        --output-file "$RESULTS_DIR/$\\\\{target\\\\}.json" \
        run "$target"

done < "$TARGETS_FILE"

# Generate summary report
cloudflare report summary \
    --input "$RESULTS_DIR/*.json" \
    --output "$RESULTS_DIR/summary.html"

Best Practices

Security

  • Always verify checksums when downloading binaries
  • Use strong authentication methods (API keys, certificates)
  • Regularly update to the latest version
  • Follow principle of least privilege
  • Enable audit logging for compliance
  • Use encrypted connections when possible
  • Validate all inputs and configurations
  • Implement proper access controls

Performance

  • Use appropriate resource limits for your environment
  • Monitor system performance regularly
  • Optimize configuration for your use case
  • Use parallel processing when beneficial
  • Implement proper caching strategies
  • Regular maintenance and cleanup
  • Profile performance bottlenecks
  • Use efficient algorithms and data structures

Operational

  • Maintain comprehensive documentation
  • Implement proper backup strategies
  • Use version control for configurations
  • Monitor and alert on critical metrics
  • Implement proper error handling
  • Use automation for repetitive tasks
  • Regular security audits and updates
  • Plan for disaster recovery

Development

  • Follow coding standards and conventions
  • Write comprehensive tests
  • Use continuous integration/deployment
  • Implement proper logging and monitoring
  • Document APIs and interfaces
  • Use version control effectively
  • Review code regularly
  • Maintain backward compatibility

Resources

Official Documentation

Community Resources

Learning Resources

  • Git - Complementary functionality
  • Docker - Alternative solution
  • Kubernetes - Integration partner

Last updated: 2025-07-06|Edit on GitHub