~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Digital Ocean API

Published on

Updated on

2 minute reading time

This post is meant for the times that you want to quickly query the Digital Ocean API v2, but do not want to download their great client doctl. The prerequisite for this post is that you have a personal access token configured inside the $DO_TOKEN environmental variable.

List SSH Keys

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/account/keys

If you have jq configured:

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/account/keys |
jq '.ssh_keys[] | "\(.name): \(.id)"'

List Images

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/images?type=distribution

If you have jq configured:

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/images?type=distribution |
jq '.images[] | .slug'

List Regions

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/regions

If you have jq configured:

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/regions |
jq '.regions[] | "\(.name): \(.slug)"'

List Snapshots

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/snapshots

If you have jq configured:

curl --silent \
     --request GET \
     --header "Authorization: Bearer $DO_TOKEN" \
     https://api.digitalocean.com/v2/snapshots |
jq '.snapshots[] | .name'

Bash Script

Here is a small bash script that combines the functionality above into one file.

#!/bin/bash
JQ_INSTALLED=$(command -v jq > /dev/null)

function show_usage {
        echo "Usage: ./do-list (keys|images|regions|snapshots)"
}

function query_api {
    curl --silent \
         --request GET \
         --header "Authorization: Bearer $DO_TOKEN" \
         https://api.digitalocean.com/v2/"$1"
}

function list_keys {
    output=$(query_api account/keys)
    if $JQ_INSTALLED; then
        echo "$output" | jq '.ssh_keys[] | "\(.name): \(.id)"'
    else
        echo "$output"
    fi
}

function list_images {
    output=$(query_api images?type=distribution)
    if $JQ_INSTALLED; then
        echo "$output" | jq '.images[] | .slug'
    else
        echo "$output"
    fi
}

function list_regions {
    output=$(query_api regions)
    if $JQ_INSTALLED; then
        echo "$output" | jq '.regions[] | "\(.name): \(.slug)"'            
    else
        echo "$output"
    fi
}

function list_snapshots {
    output=$(query_api snapshots)
    if $JQ_INSTALLED; then
        echo "$output" | jq '.snapshots[] | .name' 
    else
        echo "$output"  
    fi
}

case $1 in
    keys)
        list_keys;;

    images)
        list_images;;

    regions)
        list_regions;;

    snapshots)
        list_snapshots;;

    *)
        echo Unknown Parameter "'$1'"
        show_usage;;
esac
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter