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

March 22, 2016 870 views
API DigitalOcean DNS Networking Linux 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 Answer

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

by Shaun Lewis
Learn how to use cron to automate tasks on a VPS.
  • 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}"'"}' ""
Have another answer? Share your knowledge.