API connection for changing DNS records

Hi, i am writing a script to connect to my cloudflare account via API token and change my dns records and i have problem detecting a zone id and a domain, can you help me plz?

You can get the zone ids from this endpoint…

2 Likes

If your question was just how you can get them one time:

Otherwise the API endpoint zones-get linked above by sjr is what you would want to find one programmatically, you can use the various query parameters (ex: name) to search for a specific one

1 Like

Thank you for your kind replies, i need a script which can log in to my cloudflare account using API key and change my DNS records for my subdomains. I have a script in python3 but it is not working. I will be really apreciated for your help.

1 Like

Hi , I need a script that can change my dns records, can you help me please??

I can’t make a script for you. But you can use the Cloudflare API to do exactly this, the relevant documentation for that is here

Here’s a quick ‘n’ dirty bash script thrown together to get you started. This will get your zones and show the DNS records for each in turn. Use the appropriate API endpoints to add/delete/change records as required. Requires curl and jq. Obviously take care not to run changes on all zones until tested. Use at your own risk, etc, etc.

Uses a global API key, you should really generate a token with just the required permissions and use that instead.

#!/bin/bash

CF_AUTH_EMAIL="[email protected]"
CF_AUTH_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxx"

ZONE_IDS=`
curl -s --request GET \
  --url https://api.cloudflare.com/client/v4/zones \
  --header "Content-Type: application/json" \
  --header "X-Auth-Email: ${CF_AUTH_EMAIL}" \
  --header "X-Auth-Key: ${CF_AUTH_KEY}" \
 | jq -r '.result[] | "\(.id)"'
`

for zone_id in ${ZONE_IDS}; do 
	dns_data=`
curl -s --request GET \
  --url https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records \
  --header "Content-Type: application/json" \
  --header "X-Auth-Email: ${CF_AUTH_EMAIL}" \
  --header "X-Auth-Key: ${CF_AUTH_KEY}" \
| jq .
`
	echo -E "${dns_data}"
done

Duplicate post, see here…

https://community.cloudflare.com/t/api-connection-for-changing-dns-records/573706/7

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.