Trying to run a script to poll a Balena device and get the status of the services also.

I’m trying to run a script which polls a Balena device and get the online status (this is working) and also get the list of services (this is working) and their status (this is NOT working).

I’m expecting to be told the status of the service, but I’m only getting unknown (the default value).

Any help or point in the direction of relevant dev doc appreciated!

Code:
‘’’
import requests

BALENA_API_TOKEN = “[REDACTED]”
TEST_UUID = “[REDACTED”

BASE_URL = “https://api.balena-cloud.com/v6
HEADERS = {“Authorization”: f"Bearer {BALENA_API_TOKEN}"}

def get_device(uuid):
“”“Fetch device details by UUID”“”
url = f"{BASE_URL}/device?$filter=uuid eq ‘{uuid}’"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
return data[“d”][0] if data.get(“d”) else None

def get_services(application_id):
“”“Fetch detailed services for an application”“”
url = f"{BASE_URL}/service?$filter=application eq {application_id}&$select=service_name,status"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
return data[“d”] if data.get(“d”) else

try:
# Step 1: Get device details
device = get_device(TEST_UUID)
if not device:
print(“Device not found!”)
else:
print(f"Device Name: {device[‘device_name’]}“)
app_id = device[“belongs_to__application”][”__id"]

    # Step 2: Get services for the application
    services = get_services(app_id)
    if not services:
        print("No services found for this device's application.")
    else:
        print("Service Status:")
        for service in services:
            service_name = service.get('service_name', 'Unknown')
            status = service.get('status', 'Unknown')
            print(f" - {service_name}: {status}")

except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}“)
except Exception as e:
print(f"Error: {e}”)
‘’’

Expected:
Device Name: C1 - Controller
Service Status:

  • arduino-plc: Running
  • aws-upload: Stopped

Actual:
Device Name: C1 - Controller
Service Status:

  • arduino-plc: Unknown
  • aws-upload: Unknown