DigitalOcean Python Client

0.30.0 · active · verified Fri Apr 17

pydo is the official Python client library for interacting with the DigitalOcean API. It provides a programmatic interface to manage DigitalOcean resources such as Droplets, Kubernetes clusters, databases, and more. Currently at version 0.30.0, the library is frequently updated (often monthly minor releases) to reflect changes and additions to the DigitalOcean OpenAPI specification.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the DigitalOcean client using an API token (preferably from an environment variable) and then fetch basic account information. It also includes commented-out examples for listing resources like Droplets, highlighting the common pattern for interacting with different services.

import os
from pydo import Client
from pydo.models import Account

# Initialize a client with your DigitalOcean API token.
# It's highly recommended to set DIGITALOCEAN_TOKEN as an environment variable.
# Example: export DIGITALOCEAN_TOKEN="YOUR_DO_TOKEN"
# For local testing without setting env var:
DO_TOKEN = os.environ.get("DIGITALOCEAN_TOKEN", "YOUR_DO_TOKEN") 

if DO_TOKEN == "YOUR_DO_TOKEN":
    print("WARNING: Please set the DIGITALOCEAN_TOKEN environment variable for authentication.")
    print("Using a placeholder token for demonstration purposes. API calls will likely fail.")

client = Client(token=DO_TOKEN)

try:
    # Fetch current account information
    account: Account = client.account.get_account()
    print(f"Account Email: {account.email}")
    print(f"Droplet Limit: {account.droplet_limit}")
    print(f"Email Verified: {account.email_verified}")

    # Example: List up to 5 Droplets (adjust for pagination on large accounts)
    # droplets = client.droplets.list_droplets(per_page=5)
    # if droplets:
    #     print(f"First Droplet Name: {droplets[0].name}")

except Exception as e:
    print(f"An error occurred: {e}")
    if "Unauthorized" in str(e):
        print("Please ensure your DigitalOcean API token is valid and correctly set.")

view raw JSON →