Automatically add new or restored droplet to digital ocean dns

Posted June 12, 2016 3.2k views

If I own and configured, 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

I can see this questions: but it is three years old. Has anything been developed since then?


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
2 answers

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":"","ip_address":""}' ""

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:

  - export DROPLET_NAME=$(curl -s
  - export DROPLET_IP=$(curl -s
  - 'curl -X POST -H ''Content-Type: application/json'' -d ''{"name":"''"$DROPLET_NAME"''", "ip_address":"''"$DROPLET_IP"''"}'' -H "Authorization: Bearer API_TOKEN"'

replace 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)

by Justin Ellingwood
The DigitalOcean metadata service includes a field called "user-data", which can be used to specify a script that will be run as your server is brought online. The CloudInit program, which runs these scripts, can process a special script type called "cloud-config". In this guide, we'll explore how to create cloud-config files and the best ways to leverage their power.

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: