Python DigitalOcean API Client (Community)

1.17.0 · active · verified Thu Apr 16

python-digitalocean is a community-maintained Python library for interacting with the DigitalOcean API, allowing management of resources like Droplets, Images, and more. It is currently at version 1.17.0 and receives releases for new API features and bug fixes. While widely used, DigitalOcean also provides an official client, `pydo`.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the DigitalOcean Manager with an API token from an environment variable and lists all active Droplets in your account. Requires a DigitalOcean API token with read access configured as DIGITALOCEAN_ACCESS_TOKEN.

import os
import digitalocean

# It's recommended to store your DigitalOcean API token as an environment variable
# export DIGITALOCEAN_ACCESS_TOKEN='YOUR_API_TOKEN'
api_token = os.environ.get('DIGITALOCEAN_ACCESS_TOKEN', '')

if not api_token:
    print("Error: DIGITALOCEAN_ACCESS_TOKEN environment variable not set.")
    print("Please set it with your DigitalOcean API token.")
else:
    try:
        manager = digitalocean.Manager(token=api_token)
        my_droplets = manager.get_all_droplets()
        if my_droplets:
            print("Your Droplets:")
            for droplet in my_droplets:
                print(f"  - {droplet.name} (ID: {droplet.id}, Region: {droplet.region['slug']}, Status: {droplet.status})")
        else:
            print("No Droplets found in your account.")
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →