I hope someone can help. I'm new to Python and API programming, but would like to use Python to communicate with the Fox cloud and get some info from my inverter.
My code keeps returning 'Illegal Signature'.
I've double checked the API key and the serial number for typo's.
The code is below, so any help would be gratefully received

I've removed my API key and inverter serial number.
import requests
import hashlib
import time
import json
# -------------------------
API_KEY = 'xxxx' # From FoxESS Cloud API Management
DEVICE_SN = 'xxxx' # Like "H1-XXXXXXX"
# ------------------------
API_PATH = 'op/v1/device/real/query'
API_URL = f'https://www.foxesscloud.com/{API_PATH}'
def generate_signature(path, token, timestamp):
to_sign = f"{path}\r\n{token}\r\n{timestamp}"
print(f"\n[DEBUG] String to sign:\n{repr(to_sign)}")
sig = hashlib.md5(to_sign.encode('utf-8')).hexdigest()
print(f"[DEBUG] MD5 Signature: {sig}")
return sig
def get_data():
timestamp = str(int(time.time() * 1000))
signature = generate_signature(API_PATH, API_KEY, timestamp)
headers = {
'token': API_KEY,
'timestamp': timestamp,
'signature': signature,
'lang': 'en',
'Content-Type': 'application/json'
}
payload = {
'sn': DEVICE_SN,
'variables': [] # ← Get everything
}
print(f"\n[DEBUG] Headers:\n{json.dumps(headers, indent=2)}")
print(f"\n[DEBUG] Payload:\n{json.dumps(payload, indent=2)}")
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
print(f"\n[DEBUG] Status code: {response.status_code}")
print(f"[DEBUG] Response text:\n{response.text}")
try:
data = response.json()
pv_power = data.get('result', {}).get('pvPower', {}).get('value', 'N/A')
print(f"\nPV Power Output: {pv_power} W")
except Exception as e:
print("\n Error parsing response:", e)
get_data()
DEBUG LOG
---------
[DEBUG] String to sign:
'{path}\r\n{token}\r\n{timestamp}'
[DEBUG] MD5 Signature: 24c56d0c365016ecbc32503fab6048bc
[DEBUG] Headers:
{
"token": "xxxxx",
"timestamp": "1754005322469",
"signature": "24c56d0c365016ecbc32503fab6048bc",
"lang": "en",
"Content-Type": "application/json"
}
[DEBUG] Payload:
{
"sn": "xxxxx",
"variables": []
}
[DEBUG] Status code: 200
[DEBUG] Response text:
{"errno":40256,"msg":"Parameter could not be parsed correctly illegal signature"}
PV Power Output: N/A W
The program 'python3.13.exe' has exited with code 0 (0x0).