Using the v2 API and my Home Computer to Automatically Update a Domain Name A Record (Like Dynamic DNS)

Posted March 22, 2016 9.9k views
DigitalOceanAPINetworkingDNSLinux Commands

I want to have a script run automatically on my Mac at home, once an hour, to update the A record of a domain name which lives on DigitalOcean’s name servers.

I want the A record to be updated with whatever my external home IP address is (in effect, a dynamic DNS service so I can always get back to my home computer over the internet). My home computer sits behind a router supplied by my ISP.

I have been looking at the DigitalOcean API (v2) and using Terminal on my Mac I can successfully, manually, update the A record in question with the following command:

curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer MY_API_ACCESS_TOKEN" -d '{"data":"IP_ADDRESS"}' ""

What I’d like some help with, please, is how do I add to the above so that:

  • My home external IP address gets ascertained when the script runs,
  • Once ascertained, the IP address is automatically used (in place of ‘IP_ADDRESS’) in the above command, and,
  • The command runs on its own, once an hour.

I’m quite new to all this so I’m sorry if I’m missing anything obvious.

Thanks, any help appreciated.

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

Just to expand the script above and prevent on each cron trigger to call DO API if IP wasn’t changed:


SAVED_IPV4=`cat current_ip.txt`

if [ "$PUBLIC_IPV4" != "$SAVED_IPV4" ]
echo ${PUBLIC_IPV4} > "current_ip.txt"
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer MY_API_ACCESS_TOKEN" -d '{"data":"'"${PUBLIC_IPV4}"'"}' ""

Hey there,

You’ll need to do some basic bash scripting. There are a few sites that will return just the requesting IP address from a curl command.

Here’s a quick example of a script:



curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer MY_API_ACCESS_TOKEN" -d '{"data":"${PUBLIC_IPV4}"}' ""

You’d want to make a file that contains the code from the gist link. The file will also need to be executable (chmod o+x

Technically you can run a cronjob on that script file to run once an hour. Here’s a tutorial that talks about how to use cron.

However, “cron” is not the modern way to schedule events on OS X. Technically you want to use launchd, however that is quite a bit more complex. Here is a link on how you would do that and a link to a tool that can generate launchd plist files.

Happy coding,

Jon Schwenn
Platform Support Specialist

  • Hi,

    Thanks very much for your help.

    I’ve followed your steps, but whenever I run the script (in Terminal on my Mac) I keep getting the following response (I assume, from the API):

    {"id":"unprocessable_entity","message":"IP address did not match IPv4 format (e.g."}

    If I manually type an IP address into the command (rather than trying to use the one determined through the curl part) then this doesn’t happen; the IP address gets set as it should do.

    Grateful for any further help.


    • You may need to try different quote marks around the IP variable. For example:

      curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer MY_API_ACCESS_TOKEN" -d '{"data":"'"${PUBLIC_IPV4}"'"}' ""

Here’s an alternative I wrote, which I think is much more secure.

In this case, the DO api key only exists on a single central API server with this script, and every other host can connect to this api server to inform it of it’s IP. This means your DO keys (which provide complete access to your DO account) are not spread around on multiple hosts.