VARIABLE | VALUE |
---|---|
The email address associated with your Cloudflare account. | |
KEY | The global API key associated with your Cloudflare account. |
DOMAIN | The name of the domain to create a zone record for. |
JUMP_START | If true, automatically attempts to fetch existing DNS records when creating a domain’s zone record |
ZONE_ID | The unique ID of the domain’s zone record. Assigned by Cloudflare. Required when managing an existing zone record and its DNS records. |
DNS_ID | The unique ID given to each of the domain’s individual DNS records. Assigned by Cloudflare. Required when updating or deleting an existing DNS record. |
TYPE | The DNS record type including A, CNAME, MX and TCXT ecords. This equates to the Type column on the Cloudflare dashboard. |
NAME | The DNS record name. This equates to the Name column on the Cloudflare dashboard. |
CONTENT | The DNS record content. This equates to the Value column on the Cloudflare dashboard. |
PROXIED | If true, a DNS record will pass through Cloudflare’s servers. Un-proxied records will not and are for DNS resolution only. Applicable to A and CNAME records only. This equates to the Status column on the Cloudflare dashboard. |
TTL | Valid TTL. Must be between 120 and 2,147,483,647 seconds, or 1 for automatic |
PRIORITY | The order in which servers should be contacted. Applicable to MX records only. |
ALL If true, JSON output will be pretty-printed using Python’s json.tool module. Otherwise, output will be limited to specified data.
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
ZONE_ID="8b717207bcee4047af2e9dff95832996"; \
TYPE="A"; \
NAME="subdomain."; \
CONTENT="<IP>"; \
PROXIED="true"; \
TTL="1"; \
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
--data '{"type":"'"$TYPE"'","name":"'"$NAME"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}' \
| python -m json.tool;
Some requests to the Cloudflare API produce a lot of JSON data:
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
ZONE_ID="8b717207bcee4047af2e9dff95832996"; \
TYPE="A"; \
NAME="example.com"; \
CONTENT="203.0.113.50"; \
PROXIED="true"; \
TTL="1"; \
ALL="false"; \
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
--data '{"type":"'"$TYPE"'","name":"'"$NAME"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}' \
| if $ALL; then python -m json.tool; else python -c "import sys,json;data=json.loads(sys.stdin.read()); print('Type: ' + data['result']['type'] + '\n' + 'DNS ID: ' + data['result']['id'] if data['success'] else 'ERROR: ' + data['errors'][0]['message'])"; fi
In order to create DNS records for a domain, we first need to create a unique zone record for that domain to which we’ll later add these DNS records. To create a zone record for example.com, use the following code:
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
DOMAIN="example.com"; \
JUMP_START="false"; \
curl -X POST "https://api.cloudflare.com/client/v4/zones/" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
--data '{"name":"'"$DOMAIN"'","jump_start":'"$JUMP_START"'}' \
| python -m json.tool;
To display the existing zone record for example.com, use the following code:
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
DOMAIN="example.com"; \
curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
| python -m json.tool;
To delete the existing zone record for example.com and all its related DNS records, use the following code. Note that we need to provide the unique ID of the domain’s existing zone record:
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
ZONE_ID="8b717207bcee4047af2e9dff95832996"; \
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
| python -m json.tool;
To create a DNS record that points example.com to the IP address 203.0.113.50, use the following code:
EMAIL="[email protected]"; \
KEY="08n46q4ofo0v5pc3u3g3eu517o69axu8s6ml4"; \
ZONE_ID="8b717207bcee4047af2e9dff95832996"; \
TYPE="A"; \
NAME="example.com"; \
#NAME="sub-domain"; \
CONTENT="203.0.113.50"; \
PROXIED="true"; \
TTL="1"; \
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $KEY" \
-H "Content-Type: application/json" \
--data '{"type":"'"$TYPE"'","name":"'"$NAME"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}' \
| python -m json.tool;
The "delete existing record" url should include the record id.