Skip to content

Instantly share code, notes, and snippets.

@Starttoaster
Last active April 7, 2021 07:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Starttoaster/07d568c2a99ad7631dd776688c988326 to your computer and use it in GitHub Desktop.
Save Starttoaster/07d568c2a99ad7631dd776688c988326 to your computer and use it in GitHub Desktop.
Cloudflare API Calls (Ignore !'s. Used for emphasis/easy spotting)
*X-Auth-Email:* This is the email you use to log into your Cloudflare account. Found in "My Profile"
*X-Auth-Key:* This is found in the Cloudflare website under "My Profile > API Keys > Global API Key".
[See this link on finding the Global API Key](https://support.cloudflare.com/hc/en-us/articles/200167836-Where-do-I-find-my-CloudFlare-API-key-)
*Zone Identifier:* This is an ID specific to your domain. Found in the Cloudflare website on your domain's "Overview" page written as "Zone ID"
*Identifier:* This is an ID specific to a singular DNS record under your domain. This one is a little tricker to find.
It would appear that you need to find this by making an API call, which Gets info about all DNS records on your domain and outputs an "id" attribute.
#
# You NEED to use this to find out the "ID" or "IDENTIFIER" of your DNS Records with Cloudflare, it seems.
# This API call assumes you have python and curl installed in your linux distro. They usually are by default.
# You will need to fill in the Zone Identifier, Account Email, and Global API Key sections of this command.
#
curl -X GET "https://api.cloudflare.com/client/v4/zones/!!ZONE_IDENTIFIER!!/dns_records" \
-H "X-Auth-Email: !!ACCOUNT_EMAIL!!" \
-H "X-Auth-Key: !!GLOBAL_API_KEY!!" \
-H "Content-Type: application/json" \
| python -m json.tool \
| grep "{\|}\|content\|id\|name\|type\|zone_id\|zone_name"
#
# If you have created an A Record on Cloudflare's website, it will show up in a JSON formatted list with a "type" of "A"
# Find the "id" attribute for this A Record, as you will need it in the script.
#
#
# This is what the DDNS container will be doing for you to update your A Records.
#
curl -X PUT "https://api.cloudflare.com/client/v4/zones/!!ZONE_IDENTIFIER!!/dns_records/!!IDENTIFIER!!" \
-H "X-Auth-Email: !!ACCOUNT_EMAIL!!" \
-H "X-Auth-Key: !!GLOBAL_API_KEY!!" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"*","content":"!!IP_ADDRESS!!"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment