How to reboot a Droplet with DigitalOcean API - PHP, Python, BASH examples?

Posted December 12, 2019 3.4k views
Linux BasicsPHPPythonAPI

Recently someone asked me how to reboot a Droplet with the DigitalOcean API so I decided to put to together a few examples on how to do that with different scripts including:

  • PHP script
  • Python script
  • BASH script
  • And last but not least just a simple curl command

Hope that this helps!

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.

Submit an Answer
1 answer

According to the official API documentation, you can reboot a droplet with the following curl request:

curl -X POST -H "Content-Type: application/json" \
             -H "Authorization: Bearer API_TOKEN_HERE" \
             -d '{"type":"reboot"}' \
  • PHP example

To do the same thing with PHP you could use the following script:


// Update your API Token and Droplet ID here:
$api_token  = 'YOUR_TOKEN_HERE';
$droplet_id = 'YOUR_DROPLET_ID_HERE';

$data = array("type" => "reboot");
$data_string = json_encode($data);

$ch = curl_init('' . $droplet_id . '/actions');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $api_token,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))

$result = curl_exec($ch);


Quick rundown:

  1. We first specify our API key and our Droplet ID as variables
  2. We then specify the action that we would like to execute, in our case just a reboot
  3. Then by the help with curl_init we prepare our URL, note that you would need to install php-curl in order to be able to do so
  4. With curl_setopt we build up our CURL request
  5. Lastly with curl_exec we trigger the POST request
  6. With the print_r($result); we print the result back
  • Python example

As an example I will use this Python module to manage droplets:

So just install it using php:

pip install -U python-digitalocean

Then the script would look something like this:

import digitalocean
manager = digitalocean.Manager(token="YOUR_TOKEN_HERE")
droplet = manager.get_droplet(droplet_id="YOUR_DROPLET_ID_HERE")

For more information on how to use the DigitalOcean API with Python check the following tutorial:

  • BASH example

This would be quite similar to the plain curl command but the benefit is that we could add some variables, comments and etc.


# Variables


curl -X POST -H "Content-Type: application/json" \
             -H "Authorization: Bearer ${API_TOKEN}" \
             -d '{"type":"reboot"}' \

If you have any other suggestions or examples, please feel free to share them here!


by Brian King
An API, or Application Program Interface, makes it easy for developers to integrate one app with another. They expose some of a program's inner workings in a limited way. You can use APIs to get information from other programs, or to automate things you normally do in your...