SoftLayer Python Client

6.2.9 · active · verified Fri Apr 17

The `softlayer-python` library provides both a Python API client and a command-line interface (CLI) for interacting with the IBM Cloud SoftLayer API. It abstracts the complexities of the SOAP/REST API, allowing developers to manage SoftLayer resources such as virtual servers, bare metal, storage, and networking. The current version is 6.2.9, and the project maintains an active release cadence with frequent minor updates and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SoftLayer client using environment variables for authentication and retrieve basic account information. It also shows an example of fetching virtual guests, highlighting the use of 'object masks' which are fundamental for getting detailed API responses.

import SoftLayer
import os

# SoftLayer API credentials can be set as environment variables (SL_USERNAME, SL_API_KEY)
# or passed directly. Environment variables are recommended for security.
# SL_ENDPOINT_URL is optional, defaults to 'api.softlayer.com/rest/v3.1'
client = SoftLayer.Client(
    username=os.environ.get('SL_USERNAME', 'YOUR_USERNAME'),
    api_key=os.environ.get('SL_API_KEY', 'YOUR_API_KEY'),
    endpoint_url=os.environ.get('SL_ENDPOINT_URL')
)

try:
    # Example: Get the account details
    account = client['Account'].getObject()
    print(f"Successfully connected to SoftLayer. Account ID: {account['id']}, Company: {account['companyName']}")

    # Example: List up to 5 virtual guests (VMs)
    # Object masks are crucial for retrieving full data
    mask = 'mask[id, hostname, domain, primaryIpAddress, primaryBackendIpAddress]'
    vms = client['Account'].getVirtualGuests(mask=mask, limit=5)
    if vms:
        print("\nVirtual Guests (first 5):")
        for vm in vms:
            print(f"- ID: {vm.get('id')}, Hostname: {vm.get('hostname')}, IP: {vm.get('primaryIpAddress')}")
    else:
        print("\nNo virtual guests found.")

except SoftLayer.exceptions.SoftLayerAPIError as e:
    print(f"Error connecting to SoftLayer API: {e}")
    print("Please ensure your SL_USERNAME and SL_API_KEY environment variables are correct.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →