Skip to content

Instantly share code, notes, and snippets.

@Suzhou65
Last active July 24, 2024 13:35
Show Gist options
  • Select an option

  • Save Suzhou65/8b9e5e5360f9c0a363e82038bb0d29b8 to your computer and use it in GitHub Desktop.

Select an option

Save Suzhou65/8b9e5e5360f9c0a363e82038bb0d29b8 to your computer and use it in GitHub Desktop.
Get DNS record from CloudFlare API

Let's try to ask DNS record for CloudFlare API.

Replace the "API Token" at auth_key by the API Token you get, the "Zone ID" can be found in Cloudflare Dashboard, logged in the Cloudflare Dashboard, check the Domain page, "Zone ID" information will appearance at API block, right hand side.

import json
import requests

cloudflare_api = "https://api.cloudflare.com/client/v4/"
zone_id = "278035ad7a9d983bc54a990b43ef7eb0"
auth_key = "API Token"
headers = {'Authorization': auth_key, 'Content-Type':'application/json'}

cloudflare_dns = cloudflare_api + "zones/" + zone_id + "/dns_records"  
cloudflare_dns_respon = requests.get(cloudflare_dns, headers=headers)

if cloudflare_dns_respon.status_code == 200:
    print("Ok")
else:
    print(cloudflare_dns_respon.status_code)    

dns_data = json.loads(cloudflare_dns_respon.text)

If the request send successfully, the result will print.

Ok

Then, the JSON data(dns_data) it respon will be like this:

{'result': [{'id': '3f7a9d18e117a65860dc5e2f2abdd191',
   'zone_id': '278035ad7a9d983bc54a990b43ef7eb0',
   'zone_name': 'lewd.dream',
   'name': 'lewd.dream',
   'type': 'A',
   'content': '114.514.19.19',
   'proxiable': True,
   'proxied': True,
   'ttl': 1,
   'locked': False,
   'meta': {'auto_added': False,
    'managed_by_apps': False,
    'managed_by_argo_tunnel': False,
    'source': 'primary'},
   'created_on': '2020-08-24T03:30:28.114514Z',
   'modified_on': '2020-08-24T03:30:28.114514Z'},
  {'id': 'ec8b33016fccf46dc8969316578974d7',
   'zone_id': '278035ad7a9d983bc54a990b43ef7eb0',
   'zone_name': 'lewd.dream',
   'name': 'midsummer-s.lewd.dream',
   'type': 'AAAA',
   'content': '8930:8100:1145:141:919:36:114:514',
   'proxiable': True,
   'proxied': False,
   'ttl': 1,
   'locked': False,
   'meta': {'auto_added': False,
    'managed_by_apps': False,
    'managed_by_argo_tunnel': False,
    'source': 'primary'},
   'created_on': '2020-08-24T03:43:46.114514Z',
   'modified_on': '2020-08-24T03:43:46.114514Z'}],
 'success': True,
 'errors': [],
 'messages': [],
 'result_info': {'page': 1,
  'per_page': 20,
  'count': 2,
  'total_count': 2,
  'total_pages': 1}}
@erbanku
Copy link

erbanku commented Jul 6, 2024

image

@Suncatcher
Copy link

Suncatcher commented Jul 7, 2024

$date = Get-Date -Format "MMdd-HHmmss"; curl --request GET --url https://api.cloudflare.com/client/v4/zones/YOUR_ZONE_ID/dns_records --header "Content-Type: application/json" --header "X-Auth-Email: XXX@gmail.com" --header "X-Auth-Key: XXX" --output "CF-DNS-Records-Fetch-on-$date.json"

this works in PS Core 7.4.2
image

and doesn't work in classic PS 5.0, shipped with Windows
image

Seems like another bit in long list of differences between the two, just to the attention of the folks who will read the thread.

Anyway, thanks a lot @erbanku, I really appreciate your effort in troubleshooting this. Due to your help I received another error and found out I used the wrong token (API token instead of API key), and got a better understand of CF. Bless you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment