Why /v2/sizes doesn't returns all available sizes?

January 4, 2019 357 views
API

Hi,
A GET request to "https://api.digitalocean.com/v2/sizes" should return a list of the available sizes.
But when i try with

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ****" "https://api.digitalocean.com/v2/sizes" | jq ".sizes[] | .slug"

I get

  • "512mb"
  • "s-1vcpu-1gb"
  • "1gb"
  • "s-1vcpu-2gb"
  • "s-1vcpu-3gb"
  • "s-2vcpu-2gb"
  • "s-3vcpu-1gb"
  • "2gb"
  • "s-2vcpu-4gb"
  • "4gb"
  • "c-2"
  • "s-4vcpu-8gb"
  • "8gb"
  • "c-4"
  • "s-6vcpu-16gb"
  • "16gb"
  • "c-8"
  • "s-8vcpu-32gb"
  • "s-12vcpu-48gb"
  • "32gb"

I have tried to resize a machine to a not-listed-above "c-16" size and its works! So my question is, why it is missing from the list returned by "/v2/sizes" ?

1 Answer
asb MOD January 4, 2019
Accepted Answer

List responses from the DigitalOcean API are paginated. By default, only 20 items are listed in the first page. For example, this will return 20:

curl -X GET -H "Content-Type: application/json" \
    -H "Authorization: Bearer $DO_TOKEN" \
    "https://api.digitalocean.com/v2/sizes" | \
    jq ".sizes[] | .slug" | wc -l

If we made this query without the jq filter, we would see the response contains:

  "links": {
    "pages": {
      "last": "https://api.digitalocean.com/v2/sizes?page=2",
      "next": "https://api.digitalocean.com/v2/sizes?page=2"
    }
  },
  "meta": {
    "total": 29
  }

This indicates that there are additional results that can be accessed on the next page by appending the page=2 query parameter to the request.

If you want to access more than 20 results per page, you can adjust this using the per_page query parameter. The maximum number of results in a page is 200. So adjusting our request to use per_page=200:

curl -X GET -H "Content-Type: application/json" \
    -H "Authorization: Bearer $DO_TOKEN" \
    "https://api.digitalocean.com/v2/sizes?per_page=200" | \
    jq ".sizes[] | .slug" | wc -l

we get 29 results. Also, the links section of the response will be empty indicating that there are no additional pages.

Have another answer? Share your knowledge.