From ecb92d5821769542fc90ffb632327d64a080f115 Mon Sep 17 00:00:00 2001 From: Anabelmara18 Date: Wed, 8 Oct 2025 23:14:33 +0100 Subject: [PATCH 1/2] docs: add example for listing droplets using environment variable --- README.md | 19 +++++++++++++++++++ examples/list_droplets_with_env.py | 14 ++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 examples/list_droplets_with_env.py diff --git a/README.md b/README.md index 22049d46..6231d919 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,25 @@ or client = Client(token=os.getenv("DIGITALOCEAN_TOKEN"), retry_policy=MyRetryPolicy()) ``` +#### Example: Listing Droplets Using Environment Variables + +You can safely use your DigitalOcean API token stored as an environment variable to list all droplets in your account. + +```python +import os +from pydo import Client + +# Initialize the client using an environment variable for the token +client = Client(token=os.getenv("DIGITALOCEAN_TOKEN")) + +print("Fetching list of your DigitalOcean droplets...\n") + +droplets = client.droplets.list() +for d in droplets["droplets"]: + print(f"ID: {d['id']}, Name: {d['name']}, Status: {d['status']}") +``` + + # **Contributing** >Visit our [Contribuing Guide](CONTRIBUTING.md) for more information on getting diff --git a/examples/list_droplets_with_env.py b/examples/list_droplets_with_env.py new file mode 100644 index 00000000..86269f9a --- /dev/null +++ b/examples/list_droplets_with_env.py @@ -0,0 +1,14 @@ + +import os +from pydo import Client + +# Initialize the client with your DigitalOcean token +# Make sure your environment variable DIGITALOCEAN_TOKEN is set +client = Client(token=os.getenv("DIGITALOCEAN_TOKEN")) + +print("Fetching list of your DigitalOcean droplets...\n") + +# List all available droplets in your account +droplets = client.droplets.list() +for d in droplets["droplets"]: + print(f"ID: {d['id']}, Name: {d['name']}, Status: {d['status']}") From 27e7b30fad5d192c3c3394ba5a73fc7c94fc2a5d Mon Sep 17 00:00:00 2001 From: Anabelmara18 Date: Wed, 8 Oct 2025 23:34:26 +0100 Subject: [PATCH 2/2] Added example for listing droplets and environment variable setup instructions --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 6231d919..db8afe04 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,22 @@ for d in droplets["droplets"]: print(f"ID: {d['id']}, Name: {d['name']}, Status: {d['status']}") ``` +#### Setting Environment Variables + +Before running the script, make sure you set your DigitalOcean API token as an environment variable: + +**Windows (PowerShell):** +```bash +setx DIGITALOCEAN_TOKEN "your_api_token_here" +``` + +**MacOS/Linux(Terminal):** +```bash +export DIGITALOCEAN_TOKEN="your_api_token_here" +``` + +This keeps your API token secure and avoids hardcoding it directly in your scripts. + # **Contributing**