DEV Community

Franz Wong
Franz Wong

Posted on • Edited on

Create and delete Digital Ocean droplet with curl

Assumed you have already get the Digital Ocean API key and SSH key.

digitalocean_token="<your API key>"
ssh_key="<your ssh key fingerprint>"
Enter fullscreen mode Exit fullscreen mode

Create a droplet

Let's create a droplet. You can find values of available regions, size, distro images from https://slugs.do-api.dev/

droplet_name="<your droplet name>"

region="nyc1"
size="s-1vcpu-512mb-10gb"
image="ubuntu-22-04-x64"

# Create droplet
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
-d '{"name":"'"${droplet_name}"'","region":"'"${region}"'","size":"'"${size}"'","image":"'"${image}"'","ssh_keys":["'"${ssh_key}"'"],"backups":false,"ipv6":false,"monitoring":false}' \
"https://api.digitalocean.com/v2/droplets"
Enter fullscreen mode Exit fullscreen mode

Get droplet status

We can get the status and wait until it becomes active.

curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
"https://api.digitalocean.com/v2/droplets?name=${droplet_name}" | jq -r '.droplets[].status'
Enter fullscreen mode Exit fullscreen mode

Get the IP address

After it is provisioned, we can get the public IP address.

droplet_ip=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
"https://api.digitalocean.com/v2/droplets?name=${droplet_name}" | jq -r '.droplets[].networks.v4[] | select(.type == "public") | .ip_address')

# Login to droplet
ssh -i "${your_ssh_key_path}" root@${droplet_ip}
Enter fullscreen mode Exit fullscreen mode

Delete droplet

After we've done with the droplet, delete it to prevent from charging money.

# Get droplet id
droplet_id=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
"https://api.digitalocean.com/v2/droplets?name=${droplet_name}" | jq -r '.droplets[].id')

# Delete droplet
curl -s -X DELETE \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
"https://api.digitalocean.com/v2/droplets/${droplet_id}"

# It should return no droplet details if it is deleted
curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${digitalocean_token}" \
"https://api.digitalocean.com/v2/droplets?name=${droplet_name}"
Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay