search

Using python's requests library

Making API calls using the python requests library is relatively easy but requires a bit of boilerplate code. For more in-depth documentation on the requests library, refer to Requests: HTTP for Humans™

For specifics on the structure of API calls, see Anatomy of an API request

Getting a list of devices with python's requests library

  1. Import the requests and json library; pprint is optional

    import requests, json, pprint
  2. Set your router endpoint URL, headers, SSL verification, and your username and password

    api_endpoint = 'https://my_zenoss_instance.company.com/zport/dmd/device_router'
    api_headers = {'Content-Type': 'application/json'}
    api_ssl = True
    api_username = 'myApiUsername'
    api_password = 'my$up3rS3cUr3ApiP@$$w0rd'
  3. Build the API request

    api_action = 'DeviceRouter'
    api_method = 'getDevices'
    api_data = []
    
    payload = json.dumps({'action': api_action,
                          'method': api_method,
                          'data': api_data,
                          'tid': 1})
  4. Make the API request

    response = requests.post(api_endpoint,
                             payload,
                             headers=api_headers,
                             auth=(api_username, api_password),
                             verify=api_ssl)
  5. Use pprint to view the result

    pprint.pprint(response.json())