Skip to content

Instantly share code, notes, and snippets.

@thikade
Last active September 2, 2019 14:30
Show Gist options
  • Save thikade/310d240b04bd76a42175694e6ac5edf1 to your computer and use it in GitHub Desktop.
Save thikade/310d240b04bd76a42175694e6ac5edf1 to your computer and use it in GitHub Desktop.
Filtering Digital Ocean API request using jq

Prereqs

export TOKEN=yourtoken

Display DO available droplet sizes

API Link

All sizes

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/sizes?page=1&per_page=500&type=distribution" | jq '.sizes[] | { slug: .slug, cpu: .vcpus, memory: .memory, disk: .disk, transfer: .transfer, pricePerMonth: .price_monthly, pricePerHour: .price_hourly  } '

Sizes that cost 10 USD

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/sizes?page=1&per_page=500&type=distribution" | jq '.sizes[] | select(.price_monthly  | contains(10))  | { slug: .slug, cpu: .vcpus, memory: .memory, disk: .disk, transfer: .transfer, pricePerMonth: .price_monthly, pricePerHour: .price_hourly  } '

Sizes below 100 USD

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/sizes?page=1&per_page=500&type=distribution" | jq '.sizes[] | select(.price_monthly < 100)  | { slug: .slug, cpu: .vcpus, memory: .memory, disk: .disk, transfer: .transfer, pricePerMonth: .price_monthly, pricePerHour: .price_hourly  } '

Sizes that have 8 GB memory

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/sizes?page=1&per_page=500&type=distribution" | jq '.sizes[] | select(.memory == (8 * 1024)) | { slug: .slug, cpu: .vcpus, memory: .memory, disk: .disk, transfer: .transfer, pricePerMonth: .price_monthly, pricePerHour: .price_hourly  } '

again, formatted as table

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/sizes?page=1&per_page=500&type=distribution" 
| jq -r '["SLUG","CPUs", "MEMORY", "DISK", "TRANSFER", "monthly", "hourly"], 
(.sizes[] | select(.memory == (8 * 1024) and (.slug|length > 3))  
|  [.slug, .vcpus,.memory,.disk, .transfer, .price_monthly, .price_hourly ])| @tsv'

Display DO image details

Query DO API to display all CentOS image slugs

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/images?page=1&per_page=500&type=distribution"| jq '.images[] | select(.distribution | contains("CentOS")) | { id: .id, name: .name, dist: .distribution, slug: .slug } '

Query DO API to display all image slugs

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/im
ages?page=1&per_page=500&type=distribution"| jq '.images[] | select(.distribution) | { id: .id, na
me: .name, dist: .distribution, slug: .slug } '

Query DO API to display all SSH Public Keys' fingerprints

curl -ss -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "https://api.digitalocean.com/v2/account/keys" | jq '.ssh_keys[] | { name: .name , fingerprint: .fingerprint }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment