Question

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

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!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
December 12, 2019
Accepted 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"}' \
             "https://api.digitalocean.com/v2/droplets/DROPLET_ID_HERE/actions"
  • PHP example

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

<?php

// 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('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 ' . $api_token,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
print_r($result);

?>

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 digitalocean.com droplets:

https://www.digitalocean.com/community/tools/python-digitalocean

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")
droplet.reboot()

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

https://www.digitalocean.com/community/tutorials/how-to-use-web-apis-in-python-3

  • 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.

#!/bin/bash

##
# Variables
##

API_TOKEN=YOUR_TOKEN_HERE
DROPLET_ID=YOUR_DROPLET_ID_HERE

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

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

Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel