Python DigitalOcean API Client (Community)
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
-
digitalocean.baseapi.TokenError: Unauthorized. Could not authenticate your request.
cause The DigitalOcean API token provided is missing, incorrect, expired, or does not have the necessary permissions.fixEnsure the `DIGITALOCEAN_ACCESS_TOKEN` environment variable is correctly set with a valid, active API token. Verify the token's scopes in your DigitalOcean account settings. Generate a new token if necessary. -
AttributeError: module 'digitalocean' has no attribute 'Manager'
cause The `Manager` class was not imported correctly, or `digitalocean` was imported in a way that doesn't expose `Manager` at the top level, or there's a typo.fixUse `from digitalocean import Manager` or ensure `import digitalocean` is used, and then access it as `digitalocean.Manager(...)`. Check for typos in the class name. -
digitalocean.baseapi.DataReadError: Cannot create a Droplet. The following errors occurred: Image is not available in the selected region.
cause Attempting to create a DigitalOcean resource (e.g., Droplet, Image) with invalid or incompatible parameters, such as an image slug not available in the specified region, or an incorrect size slug.fixConsult the DigitalOcean API documentation or the library's methods (e.g., `manager.get_all_images()`, `manager.get_all_regions()`, `manager.get_all_sizes()`) to retrieve valid and compatible parameters before attempting resource creation.
Warnings
- gotcha This library (`python-digitalocean`) is a community-maintained client. DigitalOcean also provides an *official* Python client library called `pydo`. Ensure you are using the client that best fits your project's needs and support expectations.
- breaking Hardcoding DigitalOcean API tokens directly into your code is a significant security risk. Tokens provide full access to your account's resources.
- gotcha Be aware of API rate limits when making many requests. Repeatedly hitting rate limits can result in temporary blocking of your API access.
- gotcha Some versions of `python-digitalocean` (e.g., 1.16.0-3 in Debian testing) have been reported to introduce bugs or be blocked from migration due to issues. This suggests potential instability depending on your environment or specific version.
Install
-
pip install python-digitalocean
Imports
- Manager
import digitalocean; manager = digitalocean.manager.Manager(...)
from digitalocean import Manager
- Droplet
from digitalocean import Droplet
- SSHKey
from digitalocean import SSHKey
Quickstart
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}")