Question

Automatically add new or restored droplet to digital ocean dns

If I own and configured example.com, I would like for all new droplets to be added on as second level domain. For example, if a new droplet called dev, could it not be automatically added as dev.example.com?

I can see this questions: https://www.digitalocean.com/community/questions/automaticaly-add-new-droplet-to-digitalocean-dns but it is three years old. Has anything been developed since then?

Regards, Analytium


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

To follow on what @MichaelOjeda said, once you have an API Token, the curl request to add a domain via the API is:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer APITOKEN" -d '{"name":"example.com","ip_address":"1.2.3.4"}' "https://api.digitalocean.com/v2/domains"

For NEW droplets, you can automate this by pasting a user-data cloud-config script in the User Data field on the Droplet Create page:

#cloud-config
runcmd:
  - export DROPLET_NAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
  - export DROPLET_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
  - 'curl -X POST -H ''Content-Type: application/json'' -d ''{"name":"''"$DROPLET_NAME"''.example.com", "ip_address":"''"$DROPLET_IP"''"}'' -H "Authorization: Bearer API_TOKEN" https://api.digitalocean.com/v2/domains'

replace example.com and API_TOKEN

The #cloud-config line specifies that you are using cloud-init language (which interprets this as YAML), runcmd: specifies that the following lines should be run in shell when server first starts.

The two export lines set the droplet’s IP address and name as environment variables, and the final line makes the HTTP request to create a new domain via the API.

(All the extra quotation marks are due to making YAML and JSON play nice together)

This is possible, but you would need to do it through the API. There isn’t (and probably will never be) a option to do this in the portal. Here’s the link to the documentation: https://developers.digitalocean.com/documentation/v2/#domain-records