Use Cloudflare List DNS Records API For Retrieving Record Identifier #27

Closed
opened 2025-11-20 04:18:59 -05:00 by saavagebueno · 1 comment
Owner

Originally created by @tools400 on GitHub (Apr 12, 2020).

Originally assigned to: @qdm12 on GitHub.

It was great if you could enhance your ddns-updater so that it calls the Cloudflare "List DNS Records" API for retrieving the record identifier, which is required for updating the record with the new IP address.
For sure I would create a pull request for that, if I was a more experienced Go developer. I played a bit with the following snippet, but was not able to see the response body:

func main() {
	token := "my-zone.dns-token"
	userServiceKey := ""
	email := ""
	key := ""
	zoneIdentifier := "1befa8a15f56bf0f04abbd3851fe95e0"
	URL := CloudflareURL + "/zones/" + zoneIdentifier + "/dns_records" + "?type=A&name=wiki.example.com"
	req, _ := http.NewRequest(http.MethodGet, URL, nil)
	switch {
	case len(token) > 0:
		req.Header.Set("Authorization", "Bearer "+token)
	case len(userServiceKey) > 0:
		req.Header.Set("X-Auth-User-Service-Key", userServiceKey)
	case len(email) > 0 && len(key) > 0:
		req.Header.Set("X-Auth-Email", email)
		req.Header.Set("X-Auth-Key", key)
	default:
		fmt.Println(fmt.Errorf("email and key are both unset and user service key is not set and no token was provided"))
		return
	}
	client := &http.Client{}
	resp, _ := client.Do(req)
	defer resp.Body.Close()
	fmt.Println(resp.Body)
	fmt.Println("** Executed **")
}

You could raise an error in case that more than one record is found.

Thanks,

Thomas.

Originally created by @tools400 on GitHub (Apr 12, 2020). Originally assigned to: @qdm12 on GitHub. It was great if you could enhance your ddns-updater so that it calls the Cloudflare "List DNS Records" API for retrieving the record identifier, which is required for updating the record with the new IP address. For sure I would create a pull request for that, if I was a more experienced Go developer. I played a bit with the following snippet, but was not able to see the response body: ```go func main() { token := "my-zone.dns-token" userServiceKey := "" email := "" key := "" zoneIdentifier := "1befa8a15f56bf0f04abbd3851fe95e0" URL := CloudflareURL + "/zones/" + zoneIdentifier + "/dns_records" + "?type=A&name=wiki.example.com" req, _ := http.NewRequest(http.MethodGet, URL, nil) switch { case len(token) > 0: req.Header.Set("Authorization", "Bearer "+token) case len(userServiceKey) > 0: req.Header.Set("X-Auth-User-Service-Key", userServiceKey) case len(email) > 0 && len(key) > 0: req.Header.Set("X-Auth-Email", email) req.Header.Set("X-Auth-Key", key) default: fmt.Println(fmt.Errorf("email and key are both unset and user service key is not set and no token was provided")) return } client := &http.Client{} resp, _ := client.Do(req) defer resp.Body.Close() fmt.Println(resp.Body) fmt.Println("** Executed **") } ``` You could raise an error in case that more than one record is found. Thanks, Thomas.
Author
Owner

@qdm12 commented on GitHub (May 30, 2020):

Hello Thomas,

Thanks for your suggestion and the piece of code 😉

I finally had the time to plug that in (the polished piece of code 😄) in this PR. So now it first gets the identifier using the fqdn + zone identifier, so no need to query manually, thanks for noticing, I didn't even see you could automate that.

In this PR, I also added support for ipv6 for Cloudflare, but I don't have a domain at theirs;

Would you mind trying with the image qmcgaw/ddns-updater:cloudflare-ipv6 to check it works? (It's building here)

Thanks!

@qdm12 commented on GitHub (May 30, 2020): Hello Thomas, Thanks for your suggestion and the piece of code 😉 I finally had the time to plug that in (the [polished piece of code](https://github.com/qdm12/ddns-updater/blob/cloudflare-ipv6/internal/settings/cloudflare.go#L138_L191) 😄) in [this PR](https://github.com/qdm12/ddns-updater/pull/71/files). So now it first gets the identifier using the fqdn + zone identifier, so no need to query manually, thanks for noticing, I didn't even see you could automate that. In this PR, I also added support for ipv6 for Cloudflare, but I don't have a domain at theirs; Would you mind trying with the image `qmcgaw/ddns-updater:cloudflare-ipv6` to check it works? (It's building [here](https://github.com/qdm12/ddns-updater/runs/723901838?check_suite_focus=true)) Thanks!
Sign in to join this conversation.