Cloudflare DNS for a new app, record by record

Put a new domain on Cloudflare DNS: zone states, the nameserver switch, the records a SaaS needs, proxy status, API calls and how to debug.

Simon, Founder of Shipyard

By · Founder of Shipyard

7 minute read

From added to answering

Putting a domain on Cloudflare DNS takes three moves: add the domain to your Cloudflare account, which creates a zone and assigns it two nameservers; replace the nameservers at your registrar with those two; then create the records for your host and your email. The zone only becomes Active once Cloudflare sees its nameservers at the registry, which Cloudflare says can take up to 24 hours. Your registration, renewal and bill stay where they are.

A zone is Cloudflare's word for one domain and all of its records. What you can do with it depends on its status, and two of the statuses come with a deletion clock that is easy to miss.

StatusWhat it meansClock
InitializingYou started adding the domain and never picked a plan.Deleted after 28 days.
Pending Nameserver UpdateThe zone exists and answers on its assigned nameservers, but the registrar still points elsewhere. Nothing can be proxied.On the free plan, deleted if not activated within 28 days.
ActiveCloudflare has seen its nameservers on the domain.None.
MovedThe nameservers were changed away from Cloudflare.On the free plan, deleted 7 days later.

Adding the domain and switching nameservers

A domain registered at Cloudflare Registrar needs none of these steps: its zone comes with the purchase and is on Cloudflare nameservers from the first minute.

  1. In the Cloudflare dashboard go to Domains and select Onboard a domain. Enter the bare domain, such as example.com, not www.example.com.
  2. Let Cloudflare scan for existing records, or choose to add them by hand. Select Continue and pick a plan; Free is enough for DNS.
  3. Review what the scan imported. On a domain you bought yesterday these may be the registrar's parking records, which you can delete. On a domain that already carries email, check that every MX and TXT record came across, because the scan is not guaranteed to find everything.
  4. If DNSSEC is switched on at the current registrar, switch it off there first. Changing nameservers with DNSSEC active can make the domain unreachable.
  5. Copy the two assigned nameservers. They also appear later on the zone's Overview page. You cannot choose them.
  6. At the registrar, remove the existing nameservers and enter the two from Cloudflare, exactly.
  7. Wait for the email saying the domain is active. You can re-enable DNSSEC from Cloudflare afterwards.

The records a typical SaaS ends up with

This is roughly what a zone holds after a Shipyard launch on Railway with Resend for email. The exact targets come from your host and your email provider; never copy them from an article, including this one.

Two details in the table are specific to Cloudflare. A CNAME on the bare domain is not allowed in plain DNS, but Cloudflare accepts it and answers with the target's IP address, a behaviour it calls CNAME flattening. That matters for hosts such as Railway that give you a host name and publish no fixed IP. And in the dashboard the name @ stands for the bare domain.

TypeNameContentProxyFor
CNAME@the target your host showsDNS onlyThe bare domain
CNAMEwwwthe target your host showsDNS onlyThe www address
TXTas shown by the hosta verification tokenn/aProves to the host that the domain is yours
TXTresend._domainkeyp=...n/aDKIM for outgoing mail
MXsendfeedback-smtp.<region>.amazonses.com, priority 10n/aBounce handling for outgoing mail
TXTsendv=spf1 include:amazonses.com ~alln/aSPF for outgoing mail
TXT_dmarcv=DMARC1; p=nonen/aDMARC policy
TXT@the token Google issuesn/aSearch Console ownership

Proxy status: start grey

Only A, AAAA and CNAME records can be proxied. Everything else, MX and TXT included, is always DNS only. A proxied record (orange cloud) answers DNS queries with Cloudflare's own addresses and passes web traffic through Cloudflare. A DNS-only record (grey cloud) returns your host's real value.

For the records that point at your host, the two settings are different setups, not a cosmetic switch. With DNS only there is one certificate, issued and served by your host, and one place to look when HTTPS fails. With the proxy on, Cloudflare serves its own certificate to visitors and makes a second connection to your host. Railway's documentation describes the proxied setup and is specific about it: SSL/TLS mode must be Full, Full (Strict) does not work as intended, and Railway may not be able to issue its own certificate for a proxied domain. Shipyard starts every launch on DNS only, so the host's certificate is the only moving part on day one, and leaves turning the proxy on to you.

Be aware of the defaults. Records created while onboarding a domain in the dashboard are proxied by default. When you create a record through the API, say what you want: Shipyard sends "proxied": false on every A, AAAA and CNAME it writes.

TTL is how long resolvers may cache an answer. Proxied records are fixed at Auto, which is 300 seconds. DNS-only records can be set anywhere from 60 seconds to a day, and Auto also means 300 seconds.

The same thing through the API

Create a token under My Profile, API Tokens, Create Token. The Edit zone DNS template is enough to manage records in an existing zone; creating zones as well needs the Zone permission at Edit level next to DNS. The secret is shown once.

Three things about the record call differ from the dashboard. name is the full name, www.example.com, not www. ttl set to 1 means automatic. And comment is free text stored with the record that never appears in a DNS answer; Shipyard writes "Created by Shipyard" there so you can tell its records from yours at a glance.

Terminal
# is the token alive?
curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

# find the zone and read its status and nameservers
curl "https://api.cloudflare.com/client/v4/zones?name=example.com" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"

# create a DNS-only CNAME for www
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"type":"CNAME","name":"www.example.com","content":"<target from your host>","ttl":1,"proxied":false,"comment":"web"}'

Three API behaviours worth knowing before you script it

Records that cannot share a name

Most failures after the nameserver switch are collisions, not propagation.

What Shipyard does with the zone

Shipyard looks for the zone first and creates it only if it is missing. If the domain sits at a registrar it can reach, it sets the nameservers there, asking for confirmation when the domain already existed, because records at the old provider stop being used. If the domain is somewhere else, it stops and prints the two nameservers for you to enter by hand. It then checks every 20 seconds, for up to 30 minutes, until the zone is active before it writes anything that depends on it.

Every record goes through one rule. An identical record that already exists is left alone and never treated as Shipyard's. A different record where only one may exist (A, AAAA or CNAME on the same name, a second SPF, a second DMARC) stops the run with a message naming both values. Otherwise the record is created and remembered. Teardown deletes only remembered records and never the zone, since the zone may hold records that are not Shipyard's. The token stays in your computer's keychain.

Checking the delegation and the records

When a zone stays in Pending Nameserver Update, ask the registry what it has, instead of your laptop's resolver. The first command below walks down from the root and prints the nameservers the parent zone hands out. If they are not the two Cloudflare assigned, the change at the registrar was not saved, was typed with a mistake, or is blocked by a registrar state such as a transfer in progress. The second looks for a leftover DNSSEC record from the previous provider, which breaks the delegation even when the nameservers are right.

Terminal
dig +trace example.com NS +noall +authority +nodnssec
dig DS example.com

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

See how it works

Read next