Skip to main content
Home
GrN.dk

Main navigation

  • Articles
  • Cases
  • Services
  • Your Digital Project Manager
  • About Greg Nowak
  • Image Gallery
  • Contact
User account menu
  • Log in

Join my community / free newsletter — sign up here

Breadcrumb

  1. Home

Cloudflare API: Safer DNS Automation for Launches and Migrations

Illustrated infographic summarizing: Cloudflare API: Safer DNS Automation for Launches and Migrations

By Greg Nowak. Updated August 9, 2026.

Cloudflare’s dashboard is perfectly adequate for an occasional DNS edit. The API earns its place when DNS becomes part of a launch checklist, migration, client onboarding process, or agency handover.

The business case is not automation for its own sake. It is having a change that can be inspected before traffic moves, repeated without relying on memory, and understood by the next person responsible for the domain. A short, well-documented shell script is often enough.

Choose the smallest workflow that controls the risk

Manual work is not automatically unsafe, and scripts are not automatically reliable. Match the method to the change, its consequences, and whether somebody will need to repeat or review it.

Situation Recommended approach Control that matters
One low-risk edit Cloudflare dashboard Second-person check for important records
Launch or migration Read, review, then write through the API Exact zone, name, type, content, and proxy status
Agency handover Versioned script and sample configuration Clear ownership and record comments
Bulk zone move Export, clean, review, then import Saved pre-change zone file
CI preflight Read-only API token No write permission in the checking job
A practical decision matrix for DNS work: use the lightest method that still leaves the change reviewable and recoverable.

Scope the token to the job and the zone

Use an API token instead of Cloudflare’s broad Global API key. Inventory and preflight jobs normally need DNS Read; record changes and imports need DNS Write. Restrict the token to the relevant zone and consider an expiry or client-IP restriction when the operating environment supports it.

A user token suits ad hoc work performed by a named operator. For durable CI/CD or service integrations, an Account API token can avoid tying an important process to an employee account, provided the required endpoint supports it. Keep the token in a secret store. The zone ID is configuration rather than a secret and can live in an approved CI variable.

: "${CLOUDFLARE_API_TOKEN:?set by your secret store}"
: "${ZONE_ID:?set as deployment configuration}"
CF_API='https://api.cloudflare.com/client/v4'

curl --fail-with-body --silent --show-error \
  "$CF_API/user/tokens/verify" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

If a script discovers the zone with GET /zones, its token also needs Zone Read. For a tightly scoped DNS job, copying the zone ID from the dashboard during setup avoids adding that permission merely for discovery.

Read the exact record before writing

The dangerous pattern is a script that immediately sends a create or update request. Start by filtering on both the fully qualified name and record type. Review the returned ID, content, TTL, and proxy status before deciding what should happen.

curl --fail-with-body --silent --show-error --get \
  "$CF_API/zones/$ZONE_ID/dns_records" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --data-urlencode "type=A" \
  --data-urlencode "name=app.example.com"

Make the script stop if the result is ambiguous. Zero matches can lead to a reviewed POST; one match can lead to a PATCH using that record’s ID; multiple matches should require a human decision. This makes reruns safer and prevents a creation script from quietly accumulating unintended records.

curl --fail-with-body --silent --show-error \
  "$CF_API/zones/$ZONE_ID/dns_records/$DNS_RECORD_ID" \
  --request PATCH \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"content":"198.51.100.4","ttl":3600,"proxied":true,"comment":"App origin - OPS-142"}'

Cloudflare’s current record rules still matter: A and AAAA records cannot share a name with a CNAME, while an NS record cannot share its name with another record type. A TTL of 1 means automatic; otherwise the documented range is 60–86,400 seconds, with a 30-second minimum available on Enterprise zones.

Leave business context beside the record

Comments do not affect DNS responses, but they can explain why a record exists. Add a ticket number, system owner, migration reference, or planned removal date. Comments are available on every plan; tags are useful for grouping records but are limited to paid plans. Keep the authoritative detail in your project system and use the DNS comment as a concise pointer.

Treat imports as migrations, not uploads

For bulk work, export the current zone before changing it. Store that file with the approved change request, review the proposed zone file separately, and import only after checking mail, verification, delegation, and origin records—not just the website’s A and CNAME records.

curl --fail-with-body --silent --show-error \
  "$CF_API/zones/$ZONE_ID/dns_records/export" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --output zone-before-change.txt

curl --fail-with-body --silent --show-error \
  "$CF_API/zones/$ZONE_ID/dns_records/import" \
  --request POST \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --form "[email protected]"

Cloudflare currently limits zone files to 256 KiB and import/export API traffic to three requests per minute per user. Targets for records such as CNAME, MX, NS, PTR, and SRV should be fully qualified names ending with a period. Cloudflare supports $ORIGIN, $TTL, and $GENERATE, but not $INCLUDE. Reserved cf-proxied:true and cf-proxied:false tags preserve intentional proxy state during import.

Build the handover into the automation

For coordinated multi-record work, Cloudflare’s batch endpoint can apply deletes, patches, puts, and posts in one database transaction. Network propagation is still not atomic, so do not assume every resolver will observe every change simultaneously.

  1. Version the script and reviewed sample inputs.
  2. Keep tokens out of source code and shell-history examples.
  3. Record who approves and who executes production changes.
  4. Export before bulk operations and document the rollback decision.
  5. Verify the public DNS result after the change, from outside the Cloudflare account.

If DNS keeps becoming a fragile part of launches or client handovers, Greg can help turn the commands into a dependable operating process that your team can review, run, and maintain.

Related on GrN.dk

  • AI automations need a spend dashboard before the first runaway bill
  • Google’s August 18, 2026 Content API Cutoff: Feed Cleanup Before Merchant API Migration
  • How to Bulk Delete Cloudflare DNS Records Without Browser Console JavaScript

Need help with this kind of work?

Discuss a safer DNS workflow with Greg Get in touch with Greg.

Sources

  • Create API token — Cloudflare Fundamentals
  • Cloudflare DNS Records API
  • Import and export records — Cloudflare DNS
  • Batch record changes — Cloudflare DNS
  • Account API tokens — Cloudflare Fundamentals
Last modified
2026-08-11

Tags

  • Cloudflare
  • DNS
  • API
  • Automation
  • Operations

Review Greg on Google

Greg Nowak Google Reviews

 

Written recommendations from Trafik og Veje, Aarhus Municipality (2011) and AgroTech (2010) — read them on LinkedIn.

Illustrated infographic summarizing: Google’s AI Search Toggle Is a Publishing Decision, Not an SEO Setting
Google’s AI Search Toggle Is a Publishing Decision, Not an SEO Setting
2026-08-19

Google’s AI Search toggle forces a commercial choice about visibility, attribution and content use. Here’s how to make that choice responsibly.

Illustrated infographic summarizing: From Supplier Invoice to Bookkeeping: AI with a Control Checkpoint
From Supplier Invoice to Bookkeeping: AI with a Control Checkpoint
2026-08-18

AI can reduce the work involved in processing supplier invoices, but reliable bookkeeping requires validation, duplicate checks, approval and a clear audit trail.

Illustrated infographic summarizing: Nginx 1.30 Changed the Upstream Defaults—Test Before You Upgrade
Nginx 1.30 Changed the Upstream Defaults—Test Before You Upgrade
2026-08-17

Nginx 1.30 defaults upstream proxying to HTTP/1.1 with keepalive enabled. Here is what to inspect, model and test before upgrading.

Illustrated infographic summarizing: OpenAI’s Assistants API Shuts Down in Ten Days. Is Your App Ready?
OpenAI’s Assistants API Shuts Down in Ten Days. Is Your App Ready?
2026-08-16

OpenAI’s Assistants API shuts down on August 26, 2026. Learn what to inventory, how to preserve state and how to cut over without breaking the product.

Illustrated infographic summarizing: WordPress 7.1 Forces the Editor Into an iframe—Test Your Custom Blocks
WordPress 7.1 Forces the Editor Into an iframe—Test Your Custom Blocks
2026-08-15

WordPress 7.1 removes the non-iframe editor fallback. Learn how to audit custom blocks, test real workflows and fix compatibility issues before launch.

Illustrated infographic summarizing: GitHub will stop sending jobs to stale self-hosted runners
GitHub will stop sending jobs to stale self-hosted runners
2026-08-14

GitHub starts enforcing runner versions on August 24, 2026. Audit and upgrade self-hosted runners before builds and deployments start stalling.

Illustrated infographic summarizing: Your AI Agent Has Shell Access. What Can It Reach?
Your AI Agent Has Shell Access. What Can It Reach?
2026-08-13

A practical guide to mapping what a shell-enabled AI agent can reach, then containing its access to files, credentials, networks, tools, and high-impact actions.

Illustrated infographic summarizing: Cloudflare Changed DoH JSON. What Else Is Parsing DNS as Text?
Cloudflare Changed DoH JSON. What Else Is Parsing DNS as Text?
2026-08-12

Cloudflare’s DoH JSON change exposes brittle DNS parsing. Find affected scripts, test both formats, and choose a safer integration contract.

Illustrated infographic summarizing: Your Website Can Answer Questions Now. Should It?
Your Website Can Answer Questions Now. Should It?
2026-08-11

NLWeb makes conversational website search practical to deploy. The real question is whether your content, users and team are ready to support it.

Illustrated infographic summarizing: AI Search Finally Has Reports. Now Connect Visibility to Revenue
AI Search Finally Has Reports. Now Connect Visibility to Revenue
2026-08-11

Google and Bing now expose first-party AI search data. The real task is connecting citations and impressions to analytics, CRM outcomes, and revenue.

More articles

Built by AI — available for your business. The daily articles on this site are researched, written and illustrated by an autonomous AI pipeline. At nowa.dk I install the same kind of AI automation in businesses at fixed prices — site in Danish, English version here, and web/marketing agencies have a dedicated page.

RSS feed

Footer

  • All articles
  • Contact

GrN.dk — AI automation, web platforms, web optimization, data handling and logistics.

© 2026 GrN.dk · LinkedIn · Contact · AI automation in Danish: nowa.dk

Behind GrN.dk: Individual Entrepreneur Codecrafter · Tax ID 305669096 · Bakhtrioni St. 22, 0194 Tbilisi, Georgia · official business register