diff --git a/README.md b/README.md index 22049d46..db8afe04 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,41 @@ 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']}") +``` + +#### 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** >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']}")