mmendescortes

How to use Cloudflare DDNS on Mikrotik RouterOS

Prerequisites

  1. A Working Cloudflare account.
  2. API Key, Zone ID and Record ID.
  3. RouterBoard running RouterOS version 6.44 or higher.
  4. Time and patience to read through this post.

Create and setup your Cloudflare account

If you already have a Cloudflare Account working click here to skip account creation tutorial.

  1. Go to Cloudflare signup page.
  2. Type your email address and password. Click Create Account to proceed.
  3. Type your domain and then click Add Site.
  4. Select Free plan and click Confirm plan.
  5. A list of all the DNS records will be displayed. Click Continue to proceed.
  6. Set given Cloudflare nameservers as default for your domain on the registrar website. If you need help just google “Change nameserver YOUR_REGISTRAR”.
  7. You should now be able to see your website on Cloudflare’s Dashboard.

Getting the API Key, Zone ID and Record ID


Note: If you wish to use a subdomain you should create it’s record before following up these steps!

  1. Go to your profile clicking on the avatar located on page upper right, then click on My profile.
  2. Scroll down to API Keys and click on View at Global API Key option.
  3. Type your password and click View.
  4. Copy your key and paste on a safe place, you will need it later.
  5. Go back to dashboard home page and click on your website.
  6. Copy the Zone ID.
  7. Log in on WinBox.
  8. Click in New terminal.
  9. Copy the following script and paste it on the terminal window by right clicking over it, using the paste option.
  10. 
    global cfg do={\
    /tool fetch mode=https\
    http-method=get\
    url="https://api.cloudflare.com/client/v4/zones/$cfi/dns_records"\
    http-header-field="content-type:application/json,X-Auth-Email:$cfe,X-Auth-Key:$cfk"\
    }
    

  11. Do the same with the following script, but remember to change the arguments with the Cloudflare account email, API Key and Zone ID.
  12. 
    $cfg cfi="CHANGE IT TO YOUR ZONE ID" cfe="CHANGE IT TO YOUR ACCOUNT EMAIL" cfk="CHANGE IT TO YOUR GLOBAL KEY"
    
  13. After everything is done you should end up with something like this:
  14. Close the terminal and open Files located at the right menu.
  15. Drag and drop the dns_records file to some nice folder on your computer.
  16. Open the file and manually get the desired Record ID or use this tool to get it.
  17. Go back to WinBox and click in New terminal.
  18. Now just change the variables and run the following script.
  19. 
    :global cfu do={\
    :local cfi "THE_ZONE_ID";\
    :local cfr "THE_RECORD_ID";\
    :local cfe "ACCOUNT_EMAIL_ADDRESS";\
    :local cfk "API_GLOBAL_KEY";\
    :local cfd "THE_RECORD_DOMAIN";\
    :local cfa [/ip cloud get public-address];\
    :local cfp true;\
    /tool fetch mode=https\
    http-method=put\
    url="https://api.cloudflare.com/client/v4/zones/$cfi/dns_records/$cfr"\
    http-header-field="content-type:application/json,X-Auth-Email:$cfe,X-Auth-Key:$cfk"\
    http-data="{\"type\":\"A\",\"name\":\"$cfd\",\"content\":\"$cfa\",\"proxied\":$cfp}"\
    output=none\
    }
    
  20. Congratulations, everything should work by now! Just set up a cronjob to run $cfu and you are ready to go.