{"id":8448,"date":"2024-05-06T11:56:59","date_gmt":"2024-05-06T11:56:59","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8448"},"modified":"2024-05-06T11:57:00","modified_gmt":"2024-05-06T11:57:00","slug":"easy-guide-cloudflare-subdomain-to-digital-ocean-droplet","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/easy-guide-cloudflare-subdomain-to-digital-ocean-droplet\/","title":{"rendered":"Easy Guide: Cloudflare Subdomain to Digital Ocean Droplet"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 2,511<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>Certainly! To automate the process of configuring Cloudflare subdomains to point to a DigitalOcean droplet, you can use Cloudflare&#8217;s API along with the DigitalOcean API. Below is a high-level outline of the steps involved:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>DigitalOcean<\/strong>: Obtain the IP address of your droplet using the DigitalOcean API.<\/li>\n\n\n\n<li><strong>Cloudflare<\/strong>: Use the Cloudflare API to add or update DNS records for your subdomain.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s an example using Python with the <code>requests<\/code> library to interact with both APIs:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-2bc2c14c051a5fc5e5aef1bc3fb53468\"><code><code>import requests\n\n# DigitalOcean API credentials\ndo_token = \"YOUR_DIGITALOCEAN_API_TOKEN\"\ndroplet_id = \"YOUR_DROPLET_ID\"\n\n# Cloudflare API credentials\ncf_email = \"YOUR_CLOUDFLARE_EMAIL\"\ncf_token = \"YOUR_CLOUDFLARE_API_TOKEN\"\ncf_zone_id = \"YOUR_CLOUDFLARE_ZONE_ID\"\nsubdomain = \"sub.example.com\"\n\n# Get Droplet's IP address from DigitalOcean\ndef get_droplet_ip():\n    url = f\"https:\/\/api.digitalocean.com\/v2\/droplets\/{droplet_id}\"\n    headers = {'Authorization': f'Bearer {do_token}'}\n    response = requests.get(url, headers=headers)\n    data = response.json()\n    if response.status_code == 200:\n        return data&#91;'droplet']&#91;'networks']&#91;'v4']&#91;0]&#91;'ip_address']\n    else:\n        print(\"Failed to get droplet IP:\", data)\n        return None\n\n# Update DNS record on Cloudflare\ndef update_dns_record(ip_address):\n    url = f\"https:\/\/api.cloudflare.com\/client\/v4\/zones\/{cf_zone_id}\/dns_records\"\n    headers = {\n        'Content-Type': 'application\/json',\n        'Authorization': f'Bearer {cf_token}'\n    }\n    payload = {\n        'type': 'A',\n        'name': subdomain,\n        'content': ip_address,\n        'ttl': 1,\n        'proxied': True  # Set to True if you want Cloudflare proxy\n    }\n    response = requests.post(url, headers=headers, json=payload)\n    data = response.json()\n    if response.status_code == 200:\n        print(\"DNS record updated successfully:\", data)\n    else:\n        print(\"Failed to update DNS record:\", data)\n\n# Main function\ndef main():\n    droplet_ip = get_droplet_ip()\n    if droplet_ip:\n        update_dns_record(droplet_ip)\n\nif __name__ == \"__main__\":\n    main()<\/code><\/code><\/pre>\n\n\n\n<p>Remember to replace placeholders like <code>YOUR_DIGITALOCEAN_API_TOKEN<\/code>, <code>YOUR_DROPLET_ID<\/code>, <code>YOUR_CLOUDFLARE_EMAIL<\/code>, <code>YOUR_CLOUDFLARE_API_TOKEN<\/code>, and <code>YOUR_CLOUDFLARE_ZONE_ID<\/code> with your actual credentials. Also, adjust the <code>subdomain<\/code> variable to match your desired subdomain.<\/p>\n\n\n\n<p>This script will fetch the IP address of your DigitalOcean droplet and then update the DNS record for the specified subdomain on Cloudflare to point to that IP address. Make sure you have appropriate permissions and API tokens set up for both DigitalOcean and Cloudflare.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2,511 Views Certainly! To automate the process of configuring Cloudflare subdomains to point to a DigitalOcean droplet, you can use Cloudflare&#8217;s API along with the DigitalOcean API. Below is a high-level outline of the steps involved: Here&#8217;s an example using Python with the requests library to interact with both APIs: Remember to replace placeholders like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[67,69],"tags":[],"class_list":["post-8448","post","type-post","status-publish","format-standard","hentry","category-cloud","category-digital-ocean"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=8448"}],"version-history":[{"count":1,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8448\/revisions"}],"predecessor-version":[{"id":8449,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8448\/revisions\/8449"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}