Skip to main content
GrN.dk

Main navigation

  • Articles
  • Cases
  • Contact
  • Your Digital Project Manager
  • About Greg Nowak
  • Services
  • Portfolio
  • Container
    • Excel Freelancer
    • Kubuntu - tips and tricks
    • Linux Apache MySQL and PHP
    • News
    • Image Gallery
User account menu
  • Log in

Breadcrumb

  1. Home

Cloudflare API for Safer DNS Automation

By Greg Nowak. Last updated 2026-07-09.

Cloudflare's dashboard is fine for an occasional DNS edit. The API becomes useful when DNS is part of a launch checklist, migration plan, client onboarding process, or agency handover. In those cases, the goal is not clever automation. The goal is a repeatable workflow that can be reviewed before traffic moves and understood by someone else later.

As of July 9, 2026, Cloudflare's current API documentation still supports a simple curl-and-bash approach for DNS automation. The important baseline is permissions: use scoped API tokens rather than building scripts around a broad Global API key.

Use the API when DNS needs a paper trail

Automation is worth the setup when the risk of manual clicking is higher than the cost of writing a small script. That usually means launches, repeatable environment setup, bulk client work, or a migration where the same decision may need to be checked by a developer, project manager, and site owner.

Situation Best approach Why it helps
One small record change Dashboard Fastest path when there is no repeatable process.
Launch or migration Scripted read, review, then write Reduces wrong-zone and wrong-record mistakes.
Agency handover Versioned bash script with comments Makes ownership and intent visible after the project.
Bulk zone move Export, clean, import Creates a rollback reference and catches old DNS debt.
CI preflight check Read-only token Verifies required records without granting write access.
A practical decision matrix for choosing dashboard work, API scripts, or import/export workflows.

Start with the smallest useful token

Create a token for the job, not for the person who happens to be doing the job today. For inventory and preflight checks, DNS Read is normally enough. For changes and imports, use DNS Write and restrict the token to the relevant zone wherever possible. Cloudflare also supports Account API tokens for compatible endpoints, which can be cleaner for service automation that should not be tied to one employee login.

One practical detail catches teams out: looking up a zone through GET /zones requires Zone Zone Read. If the working script only needs DNS permissions, fetch the zone ID once from the dashboard or a separate admin step, then store it in your secret manager or CI variables.

export CLOUDFLARE_API_TOKEN='...'
export ZONE_ID='...'
CF_API='https://api.cloudflare.com/client/v4'

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

A bash workflow another person can run

A dependable DNS script should do four things in order: verify the token, read the current state, make the narrowest possible change, and leave context behind. Do not start with a write call. List the relevant records first, preferably with exact filters instead of broad search behavior.

curl --fail --silent --show-error "$CF_API/zones/$ZONE_ID/dns_records?type=A&name=app.example.com" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

curl --fail --silent --show-error "$CF_API/zones/$ZONE_ID/dns_records" \
  --request POST \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"type":"A","name":"app.example.com","content":"198.51.100.4","ttl":3600,"proxied":true,"comment":"App origin - OPS-142"}'

Comments are useful because they carry business context inside Cloudflare without changing DNS responses. A ticket number, migration note, client owner, or expiry reminder is often enough. Tags can help larger teams group records, but comments are the low-friction habit to establish first.

Keep Cloudflare's record rules close to the script. A and AAAA records cannot share a name with a CNAME. NS records cannot share a name with any other record type. For TTL, 1 means automatic; otherwise the normal range is 60 to 86400 seconds, with 30 seconds available on Enterprise zones.

Treat imports like migrations, not uploads

For a real DNS migration, import and export endpoints are often more valuable than individual record calls. Export first, keep the file with the change request, clean the source zone file, and import only after someone has reviewed the records that will become active.

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

curl --fail --silent --show-error "$CF_API/zones/$ZONE_ID/dns_records/import" \
  --request POST \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --form "file=@your_formatted_file.txt"

Cloudflare's current limits matter in production planning: zone files are capped at 256 KiB, and the import/export API limit is three requests per minute per user. If an import fails, read the parsing error instead of retrying blindly.

Zone-file formatting deserves a quiet review. Targets for records such as CNAME, MX, NS, PTR, and SRV should be fully qualified names with a trailing period. Cloudflare supports $ORIGIN, $TTL, and $GENERATE, but not $INCLUDE. If proxy state matters, use Cloudflare's reserved cf-proxied:true or cf-proxied:false tags in the zone file so the import does not guess.

What maintainable DNS automation looks like

For a single site, a few well-named curl commands may be enough. For a business or agency handling multiple zones, make the workflow boring on purpose: obvious inputs, scoped tokens, readable output, and no secrets in the script body.

  1. Keep token and zone IDs in a proper secret store.
  2. Use read-only tokens for checks and write tokens only for approved changes.
  3. Version the script and sample input files.
  4. Put ticket or owner context in record comments.
  5. Export before imports so rollback has a known starting point.

If DNS changes keep showing up during launches, migrations, or client handovers, a small Cloudflare API workflow can remove a lot of avoidable risk. For help turning that into a clean operating process, talk with Greg about a safer DNS automation setup.

Related on GrN.dk

  • AI automations need a spend dashboard before the first runaway bill
  • Cloudflare Service Keys: Old Automation Needs a Token Audit
  • Bulk Delete Cloudflare DNS Records Without Browser Console JavaScript

Need help with this kind of work?

Talk with Greg about DNS automation Get in touch with Greg.

Sources

  • Create API token - Cloudflare Fundamentals docs
  • List Zones - Cloudflare API
  • List DNS Records - Cloudflare API
  • Create DNS Record - Cloudflare API
  • Import and export records - Cloudflare DNS docs
Last modified
2026-07-09

Tags

  • Cloudflare
  • DNS
  • API
  • Automation
  • bash

Review Greg on Google

Greg Nowak Google Reviews

 

Illustrated infographic summarizing: Copilot Has Repo-Level Metrics Now. What Should Teams Measure?
Copilot Has Repo-Level Metrics Now. What Should Teams Measure?
2026-07-29

GitHub’s repo-level Copilot metrics show where AI is active, but not whether it adds value. This scorecard connects usage with delivery, quality, and cost.

Illustrated infographic summarizing: Not Every AI Job Needs an Instant Answer: Batch the Backlog
Not Every AI Job Needs an Instant Answer: Batch the Backlog
2026-07-28

Move delay-tolerant AI work into dependable batch queues to cut processing costs without compromising quality, data controls, or urgent workflows.

Illustrated infographic summarizing: A stray Set-Cookie can waste your CDN: audit the cache at the edge
A stray Set-Cookie can waste your CDN: audit the cache at the edge
2026-07-27

Cloudflare Cache Response Rules can recover wasted CDN capacity, but first you need a route-level audit of public, personal and authenticated responses.

Illustrated infographic summarizing: Shorter TLS certificates expose every renewal you never automated
Shorter TLS certificates expose every renewal you never automated
2026-07-26

Shorter TLS lifetimes leave less room for manual handoffs and faulty deploy hooks. Build a renewal path that protects service availability.

Illustrated infographic summarizing: One Timeout, Two Orders: Make AI Actions Safe to Retry
One Timeout, Two Orders: Make AI Actions Safe to Retry
2026-07-25

A timed-out AI action may already have succeeded. Stable keys, durable ledgers, queues and stored results prevent a routine retry from duplicating real work.

Illustrated infographic summarizing: Your AI Visibility Dashboard Needs a Methodology, Not More Charts
Your AI Visibility Dashboard Needs a Methodology, Not More Charts
2026-07-24

A practical framework for measuring AI-search visibility with fixed prompts, repeated tests, separate metrics, retained evidence, and honest reporting.

Illustrated infographic summarizing: AI Admin APIs Are Here—But Your Directory Is Still the Source of Truth
AI Admin APIs Are Here—But Your Directory Is Still the Source of Truth
2026-07-23

New AI admin APIs can automate access and spend controls, but reliable governance still starts with authoritative directory data and clear ownership.

Illustrated infographic summarizing: OpenAI Presence Arrived—But Is Your Workflow Ready for an Agent?
OpenAI Presence Arrived—But Is Your Workflow Ready for an Agent?
2026-07-22

Before an AI agent can take on real work, its workflow needs clear scope, permissions, handoffs, evaluation cases, and production monitoring.

Illustrated infographic summarizing: Chatbot Transcripts Quietly Became a Retention and Redaction Problem
Chatbot Transcripts Quietly Became a Retention and Redaction Problem
2026-07-21

Chatbot transcripts spread across providers, logs and support tools. Here is how to map each copy, redact sensitive data and test deletion properly.

Illustrated infographic summarizing: Cloudflare Service Keys Stop in September: Find Every Caller
Cloudflare Service Keys Stop in September: Find Every Caller
2026-07-20

Cloudflare Service Keys stop working on September 30, 2026. Here is how to find every caller, move to scoped API tokens and avoid a late outage.

More articles
RSS feed

GrN.dk web platforms, web optimization, data analysis, data handling and logistics.