rdew1
By:
rdew1

Simple URL to reboot droplet with API V2 ?

April 16, 2016 1.3k views
API

Hello,

With the API V1 I used to be able to reboot a droplet simply by calling a url with the parameters in url, is that still possible to do in V2 ?

I can't figure out what parameters to use from the documentation, even a simple php script that i could use to reboot a droplet would be very useful, if anyone can help ?

1 Answer

The documentation for "Reboot a Droplet" includes an example of a curl command for performing the action:

curl -X POST -H "Content-Type: application/json" \
             -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" \
             -d '{"type":"reboot"}' \
             "https://api.digitalocean.com/v2/droplets/3164450/actions"

Just replace the b7d03a... with your own API token, and replace 3164450 with the ID of the droplet that you want to reboot.

  • Thank you ! So if someone else needs this here is a working code to reboot a droplet via PHP using api v2 :

    <?php
    $data = array("type" => "reboot");
    $data_string = json_encode($data);
    
    $ch = curl_init('https://api.digitalocean.com/v2/droplets/DROPLET_ID/actions');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer YOUR_TOKEN',
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    
    $result = curl_exec($ch);
    print_r($result);
    ?>
    

    You would need to change YOURTOKEN and DROPLETID

    • Then how to get droplet id etc from the api?

      <?php
      
      $ch = curl_init('https://api.digitalocean.com/v2/droplets/DROPLET_ID/');
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Authorization: Bearer YOUR_TOKEN',
          'Content-Type: application/json',
          'Content-Length: ' . strlen($data_string))
      );
      
      $result = curl_exec($ch);
      print_r($result); 
      
      and then what code for here?
      
      ?>
      
Have another answer? Share your knowledge.