def apikey(sessionKey, hostname, debug=False):
    try:
        # Get the API Key for the device or Panorama if Splunk knows it
        apikey = get_firewall_apikey(sessionKey)
        log(debug, "API Key found in Splunk credential store")
        return apikey
    except NoCredentialsFound:
        try:
            log(debug, "API Key was not in Splunk credential store")
            # If Splunk doesn't know the API Key, get the username and password instead
            log(debug, "Getting credentials from Splunk credential store")
            fw_username, fw_password = get_firewall_credentials(sessionKey)
            # Use the username and password to determine the API key
            log(debug, "Getting API Key from firewall/Panorama")
            device = pandevice.base.PanDevice(hostname, fw_username, fw_password)
            apikey = device.api_key
            # Save the API key to the Splunk credential store inside the App
            log(debug, "Adding API Key to Splunk credential store")
            set_firewall_apikey(sessionKey, apikey)
            return apikey
        except NoCredentialsFound as e:
            exit_with_error("No Firewall/Panorama credentials for searchbar command. Please set the username and password in the App set up page.")
        except Exception as e:
            exit_with_error("Unable to get apikey from firewall: %s" % str(e))
Login to a Palo Alto Networks device (firewall or Panorama)
dataset/ETHPy150Open PaloAltoNetworks/SplunkforPaloAltoNetworks/bin/lib/common.py/apikey