How to Use the DigitalOcean API with Python

DigitalOcean API with Python

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you’ll learn how to use the DigitalOcean API with Python. This assumes you already have a DigitalOcean API key. We will be using v2 of the API which is documented here.

DigitalOcean API with Curl

To demonstrate the concepts of the API, let’s first interact with the API on the command line with the curl command.

In order to create aVPS server (called a droplet) with 1 GB of RAM and 1 CPU core in DigitalOcean’s New York City datacenter that’s running WordPress, we can use the curl command like this.

Instead of 1234567890, please use your actual DigitalOcean API key here. Of course, feel free to change the name, size, region, image, and any other option to suite your needs.

curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 1234567890" -d '{
  "name": "HelloFromTerminal",
  "region": "nyc3",
  "size": "s-1vcpu-1gb",
  "image": "wordpress-20-04",
  "backups": false,
  "ipv6": false,
  "user_data": null,
  "private_networking": null,
  "volumes": null,
  "tags": [
    "this is a tag"
}' ""

The command will output a bunch of information about the droplet. Part of this text will be the droplet’s unique ID. We will use this in our next command.

If you check back on your DigitalOcean dashboard, you will see the droplet being created.

In order to get the IP address of the droplet, we can use the curl command again. We must wait a few seconds to do this since the IP address is not immediately assigned to the droplet.

curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 1234567890" ""

Notice how we use the droplet’s unique ID 227936008 from the output of our first command at the end of the API URL this time. Please change this to your droplet’s actual ID.

The command will output a bunch of information about the droplet. Part of this text will be the public IP address of the droplet.

DigitalOcean API with Python

Here is a Python script that interacts with the DigitalOcean API. Very similar to above, this Python script creates a droplet, waits 60 seconds for the droplet to boot, and gets the droplet’s public IP address in a second API call.

import os
import time
import glob
import requests

# private constants
api_key = "1234567890"

# api url
url = ""

headers = {"Authorization": "Bearer {}".format(api_key)}
data = {
  "name": "HelloFromPython",
  "region": "nyc3",
  "size": "s-1vcpu-1gb",
  "image": "wordpress-20-04",
  "backups": False,
  "ipv6": False,
  "user_data": None,
  "private_networking": None,
  "volumes": None,
response =, data=data, headers=headers)
droplet_id = response.json()['droplet']['id']


# get droplet ip address
droplet_url = "{}/{}".format(url, droplet_id)
droplet_response = requests.get(droplet_url, headers=headers)
ip_address = droplet_response.json()['droplet']['networks']['v4'][1]['ip_address']

For a full tutorial on this topic, check out the following video.

YouTube video


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Reply

Your email address will not be published. Required fields are marked *