Question
Droplet ID Number to Place in Variable
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×curl -X GET "https://api.digitalocean.com/v1/droplets/?client_id=$DO_CLIENT_ID&api_key=$DO_API_KEY"The results will look like:
{ "status": "OK", "droplets": [ { "id": 100823, "name": "test222", "image_id": 420, "size_id":33, "region_id": 1, "backups_active": false, "ip_address": "127.0.0.1", "private_ip_address": null, "locked": false, "status": "active", "created_at": "2013-01-01T09:30:00Z" } ] }The "id" value is the droplet's id. If you just want to get the id, you need to parse the returned json. Our community has written a number of API wrappers in different programing languages that could be helpful here. Search GitHub for "DigitalOcean" and you'll see that there are hundreds of open source integrations using our API. Just about every language is covered. If you just want to parse it on the command line, take a look at the "jq" tool. On Ubuntu, you can install it with:
sudo apt-get install jqYou can use it parse json directly on the command line. To get just a list of droplet ids, run:
curl -X GET "https://api.digitalocean.com/v1/droplets/?client_id=$DO_CLIENT_ID&api_key=$DO_API_KEY" | jq ".droplets[] | .id"
Before starting with some PHP coding, I was just trying to see if there might be some sort of simple command, unknown to me, to use before figuring out the code. Your responses helped me with the direction to take. Some truncating/parsing of the JSON with strstr and substr got me the droplet id number to assign to a variable.
Thanks again for the help.
No problem. If you’re working in PHP, there is a wrapper for the API written by a community member over on GitHub. He’s even started on one of version two of the API which just entered public beta yesterday.