DEV Community

Eli
Eli

Posted on • Updated on

Dynamic DNS with Netlify

I purchased a domain for remote access to my home a while ago, but it was just sitting there for about a year. So I decided to do something about it.

Looking at the different dynamic DNS providers, I didn't really like what they offered, or it cost $$. And I'm sort of a cheap-skate. I had used Netlify for a side project and liked what they offer. So I looked into housing my DNS with them. Turns out, most, if not everything they do is available over API. Unfortunately their API spec (https://open-api.netlify.com) didn't have what I wanted. So I went digging

I opened up the Chrome debugger and looked at the network requests that the page was doing to update DNS records, turns out it's pretty simple. The End point for DNS's is

https://api.netlify.com/api/v1/dns_zones/example_com/dns_records

At this end point you can add new records following this convention

{
    dns_zone_id: "11111111111111111",
    
errors: [],
    
flag: null,
    
hostname: "example.com",
    
id: "1111111111111111111",
    
managed: false
    
port: null
    
priority: null
    
site_id: null
    
tag: null
    
ttl: 3600
    
type: "A"
    
value: "1.1.1.1"
}
Enter fullscreen mode Exit fullscreen mode

And to delete a record all you have to do is to send a DELETE request to

https://api.netlify.com/api/v1/dns_zones/example_com/dns_records/<id>

So with this new found knowledge, I wrote some python running as a cron job on my raspberrypi to update my DNS with my home IP. If you want you can check out the code here.

GitHub logo eli-xciv / netlify-dns-client

Netlify DNS Updater

netlify-dns-client

Netlify DNS updater

This is a simple script that you can use to update the 'A' record on Netlify's DNS service.

The primary use for this is running on a raspberry pi (Debian Stretch). It's also only really used for dynamic home IP's. I may try to expand it into a full-fledged client.

Usage

  • export NETLIFY_API_TOKEN="<YOUR_TOKEN>"
  • export NETLIFY_URL="www.example.com"
  • Run the script. You can also run it as cron job.

Future

  • Expand the client to support more domains other than .com
  • Add ability for CNAME, MX records etc.
  • Better usability

Contributions

  • Fork it
  • Make changes
  • Submit Pull Request
  • I will merge

P.S. I've been a lurker on dev.to for about a year, and finally decided to write. Hopefully everyone will enjoy this information!

Top comments (1)

Collapse
 
gac profile image
Igor Ilic

Really nice. :)

Similar to what I did whit CloudFlare as my ddns as I had a domain pointed to their nameservers.