$npx -y skills add jezweb/claude-skills --skill cloudflare-apiHit the Cloudflare REST API directly for operations that wrangler and MCP can't handle well. Bulk DNS, custom hostnames, email routing, cache purge, WAF rules, redirect rules, zone settings, Worker routes, D1 cross-database queries, R2 bulk operations, KV bulk read/write, Vectori
| 1 | # Cloudflare API |
| 2 | |
| 3 | Hit the Cloudflare REST API directly when wrangler CLI or MCP servers aren't the right tool. For bulk operations, fleet-wide changes, and features that wrangler doesn't expose. |
| 4 | |
| 5 | ## When to Use This Instead of Wrangler or MCP |
| 6 | |
| 7 | | Use case | Wrangler | MCP | This skill | |
| 8 | |----------|---------|-----|-----------| |
| 9 | | Deploy a Worker | Yes | Yes | No | |
| 10 | | Create a D1 database | Yes | Yes | No | |
| 11 | | Bulk update 50 DNS records | Slow (one at a time) | Slow (one tool call each) | Yes — batch script | |
| 12 | | Custom hostnames for white-label | No | Partial | Yes | |
| 13 | | Email routing rules | No | Partial | Yes | |
| 14 | | WAF/firewall rules | No | Yes but verbose | Yes — direct API | |
| 15 | | Redirect rules in bulk | No | One at a time | Yes — batch script | |
| 16 | | Zone settings across 20 zones | No | 20 separate calls | Yes — fleet script | |
| 17 | | Cache purge by tag/prefix | No | Yes | Yes (when scripting) | |
| 18 | | Worker route management | Limited | Yes | Yes (when bulk) | |
| 19 | | Analytics/logs query | No | Partial | Yes — GraphQL | |
| 20 | | D1 query/export across databases | One DB at a time | One DB at a time | Yes — cross-DB scripts | |
| 21 | | R2 bulk object operations | No | One at a time | Yes — S3 API + batch | |
| 22 | | KV bulk read/write/delete | One at a time | One at a time | Yes — bulk endpoints | |
| 23 | | Vectorize query/delete | No | Via Worker only | Yes — direct API | |
| 24 | | Queue message injection | No | Via Worker only | Yes — direct API | |
| 25 | | Audit all resources in account | No | Tedious | Yes — inventory script | |
| 26 | |
| 27 | **Rule of thumb**: Single operations → MCP or wrangler. Bulk/fleet/scripted → API directly. |
| 28 | |
| 29 | ## Auth Setup |
| 30 | |
| 31 | ### API Token (recommended) |
| 32 | |
| 33 | Create a scoped token at: Dashboard → My Profile → API Tokens → Create Token |
| 34 | |
| 35 | ```bash |
| 36 | # Store it |
| 37 | export CLOUDFLARE_API_TOKEN="your-token-here" |
| 38 | |
| 39 | # Test it |
| 40 | curl -s "https://api.cloudflare.com/client/v4/user/tokens/verify" \ |
| 41 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq '.success' |
| 42 | ``` |
| 43 | |
| 44 | **Token scopes**: Always use minimal permissions. Common presets: |
| 45 | - "Edit zone DNS" — for DNS operations |
| 46 | - "Edit zone settings" — for zone config changes |
| 47 | - "Edit Cloudflare Workers" — for Worker route management |
| 48 | - "Read analytics" — for GraphQL analytics |
| 49 | |
| 50 | ### Account and Zone IDs |
| 51 | |
| 52 | ```bash |
| 53 | # List your zones (find zone IDs) |
| 54 | curl -s "https://api.cloudflare.com/client/v4/zones?per_page=50" \ |
| 55 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq '.result[] | {name, id}' |
| 56 | |
| 57 | # Get zone ID by domain name |
| 58 | ZONE_ID=$(curl -s "https://api.cloudflare.com/client/v4/zones?name=example.com" \ |
| 59 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id') |
| 60 | ``` |
| 61 | |
| 62 | Store IDs in environment or a config file — don't hardcode them in scripts. |
| 63 | |
| 64 | ## Workflows |
| 65 | |
| 66 | ### Bulk DNS Operations |
| 67 | |
| 68 | **Add/update many records at once** (e.g. migrating a domain, setting up a new client): |
| 69 | |
| 70 | ```bash |
| 71 | # Pattern: read records from a file, create in batch |
| 72 | while IFS=',' read -r type name content proxied; do |
| 73 | curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ |
| 74 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 75 | -H "Content-Type: application/json" \ |
| 76 | -d "{\"type\":\"$type\",\"name\":\"$name\",\"content\":\"$content\",\"proxied\":$proxied,\"ttl\":1}" \ |
| 77 | | jq '{name: .result.name, id: .result.id, success: .success}' |
| 78 | sleep 0.25 # Rate limit: 1200 req/5min |
| 79 | done < dns-records.csv |
| 80 | ``` |
| 81 | |
| 82 | **Export all records from a zone** (backup or migration): |
| 83 | |
| 84 | ```bash |
| 85 | curl -s "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?per_page=100" \ |
| 86 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 87 | | jq -r '.result[] | [.type, .name, .content, .proxied] | @csv' > dns-export.csv |
| 88 | ``` |
| 89 | |
| 90 | **Find and replace across records** (e.g. IP migration): |
| 91 | |
| 92 | ```bash |
| 93 | OLD_IP="203.0.113.1" |
| 94 | NEW_IP="198.51.100.1" |
| 95 | |
| 96 | # Find records pointing to old IP |
| 97 | RECORDS=$(curl -s "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?content=$OLD_IP" \ |
| 98 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[].id') |
| 99 | |
| 100 | # Update each one |
| 101 | for RECORD_ID in $RECORDS; do |
| 102 | curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ |
| 103 | -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ |
| 104 | -H "Content-Type: application/json" \ |
| 105 | -d "{\"content\":\"$NEW_IP\"}" | jq '.success' |
| 106 | done |
| 107 | ``` |
| 108 | |
| 109 | ### Custom Hostnames (White-Label Client Do |