Note
While you can do this, it’s much easier to use
doctl
or python or many other methods.
First get your api token from DigitalOcean and set it:
export TOKEN=(token)
Create a New Droplet
curl --request POST "https://api.digitalocean.com/v2/droplets" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data '{"region":"'"${REGION}"'",
"image":"coreos-stable",
"size":"'"$SIZE"'",
"user_data": "'"$(cat ./cloud-config.yaml)"'",
"ssh_keys":["'"$SSH_KEY_ID"'"],
"ipv6",true,
"private_networking",true,
"name":"'"$NAME"'"}'
The above will make a coreos one, and you will need your cloud-config.yaml file. For regular ones like Fedora, Ubuntu, or otherwise, just use
"user_data": null
Create Multiple Droplets at Once
For multiple ones the only change is you have ’names’ as an array rather than ’name’ as a string.
curl --request POST "https://api.digitalocean.com/v2/droplets" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data '{"region":"'"${REGION}"'",
"image":"coreos-stable",
"size":"'"$SIZE"'",
"user_data": "'"$(cat ./cloud-config.yaml)"'",
"ssh_keys":["'"$SSH_KEY_ID"'"],
"ipv6",true,
"private_networking",true,
"backups": false,
"names": [
"sub-01.example.com",
"sub-02.example.com"
}'
List all droplets:
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/droplets?page=1&per_page=1"
It will output something like:
{"droplets":[{"id":1312312,"name":"Container","memory":2048,"vcpus":2,"disk"....e=2&per_page=1"}},"meta":{"total":2}}
Get one droplet
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/droplets/$DROPLET_ID
Output:
{"droplet":{"id":12312323,"name":"Drop","memory":512,"vcpus":1,"disk"...."available":true},"tags":[]}}
Delete a droplet
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN "https://api.digitalocean.com/v2/droplets/$DROPLET_ID"
In the end, Ruby is easier.