Skip to main content

Python client library for Unraid's official GraphQL API

Project description

unraid-api

CI PyPI version Python versions Coverage License: MIT

An async Python client library for the Unraid GraphQL API (v4.21.0+, Unraid 7.1.4+).

Primary consumer: ha-unraid Home Assistant integration

Features

  • 🔄 Async/await - Built with aiohttp for non-blocking operations
  • 🏠 Home Assistant ready - Accepts external aiohttp.ClientSession for integration
  • 🔒 Secure by design - GraphQL variables, no string interpolation
  • 📦 Typed - Full type hints with py.typed marker (PEP 561)
  • 🧩 Pydantic models - Structured response parsing
  • 🔌 SSL auto-discovery - Handles Unraid's "No", "Yes", and "Strict" SSL modes
  • Redirect handling - Supports myunraid.net remote access

Installation

pip install unraid-api

Quick Start

import asyncio
from unraid_api import UnraidClient

async def main():
    async with UnraidClient("192.168.1.100", "your-api-key") as client:
        # Test connection
        if await client.test_connection():
            print("Connected!")

            # Get version info
            version = await client.get_version()
            print(f"Unraid {version['unraid']}, API {version['api']}")

asyncio.run(main())

Usage Examples

Basic Queries

async with UnraidClient(host, api_key) as client:
    # Custom GraphQL query
    result = await client.query("""
        query {
            info {
                os { hostname uptime }
                cpu { name cores threads }
                memory { total used }
            }
        }
    """)
    print(result["info"]["os"]["hostname"])

Docker Container Control

async with UnraidClient(host, api_key) as client:
    # Start a container
    await client.start_container("container:plex")

    # Stop a container
    await client.stop_container("container:plex")

VM Management

async with UnraidClient(host, api_key) as client:
    # Start a VM
    await client.start_vm("vm:windows-11")

    # Stop a VM
    await client.stop_vm("vm:windows-11")

Array Operations

async with UnraidClient(host, api_key) as client:
    # Start/stop array
    await client.start_array()
    await client.stop_array()

    # Parity check
    await client.start_parity_check(correct=True)
    await client.pause_parity_check()
    await client.resume_parity_check()
    await client.cancel_parity_check()

    # Disk spin control
    await client.spin_up_disk("disk:1")
    await client.spin_down_disk("disk:1")

Session Injection (Home Assistant)

import aiohttp
from unraid_api import UnraidClient

async def setup_client(session: aiohttp.ClientSession):
    """Use shared session from Home Assistant."""
    client = UnraidClient(
        host="192.168.1.100",
        api_key="your-api-key",
        session=session,  # Injected session won't be closed by client
        verify_ssl=False,
    )
    return client

Pydantic Models

Response data can be parsed into typed models:

from unraid_api.models import DockerContainer, VmDomain, UnraidArray

# Parse container data
container = DockerContainer(**container_data)
print(f"{container.name}: {container.state}")

# Parse array data
array = UnraidArray(**array_data)
print(f"Array: {array.state}, Capacity: {array.capacity.usage_percent}%")

Exception Handling

from unraid_api import UnraidClient
from unraid_api.exceptions import (
    UnraidAPIError,
    UnraidAuthenticationError,
    UnraidConnectionError,
    UnraidTimeoutError,
)

async with UnraidClient(host, api_key) as client:
    try:
        await client.query("query { online }")
    except UnraidAuthenticationError:
        print("Invalid API key")
    except UnraidConnectionError:
        print("Cannot reach server")
    except UnraidTimeoutError:
        print("Request timed out")
    except UnraidAPIError as e:
        print(f"API error: {e}")

API Reference

UnraidClient

Method Description
test_connection() Test if server is reachable
get_version() Get Unraid and API version
query(query, variables) Execute GraphQL query
mutate(mutation, variables) Execute GraphQL mutation
start_container(id) Start Docker container
stop_container(id) Stop Docker container
start_vm(id) Start VM
stop_vm(id) Stop VM
start_array() Start disk array
stop_array() Stop disk array
start_parity_check(correct) Start parity check
pause_parity_check() Pause parity check
resume_parity_check() Resume parity check
cancel_parity_check() Cancel parity check
spin_up_disk(id) Spin up disk
spin_down_disk(id) Spin down disk

Models

  • SystemInfo - System information
  • UnraidArray - Array state and disks
  • ArrayDisk - Individual disk info
  • ArrayCapacity - Capacity calculations
  • DockerContainer - Container details
  • VmDomain - VM details
  • UPSDevice - UPS status
  • Share - User share info
  • PhysicalDisk - Physical disk info
  • Notification - System notification

Requirements

  • Python 3.11+
  • Unraid 7.1.4+ with API 4.21.0+
  • API key with appropriate permissions

Development

# Clone repository
git clone https://github.com/ruaan-deysel/unraid-api.git
cd unraid-api

# Create virtual environment
python -m venv .venv
source .venv/bin/activate

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v --cov=src/unraid_api

# Lint and type check
ruff check .
ruff format .
mypy src/

License

MIT License - see LICENSE for details.

Contributing

Contributions welcome! Please ensure:

  1. Tests are written first (TDD)
  2. All tests pass with 80%+ coverage
  3. No linting errors (ruff check . && mypy src/)
  4. GraphQL variables used (no string interpolation)

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unraid_api-1.1.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unraid_api-1.1.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file unraid_api-1.1.0.tar.gz.

File metadata

  • Download URL: unraid_api-1.1.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unraid_api-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a93b676dd30188c4023ea45c3e8f473b74e957e2f05cd5466b8f44f4d24edbbf
MD5 c80f9dd43cacf1ad9a19bb56581c6f6c
BLAKE2b-256 5296d85ff59c2e0fb61e852c408637e0552b8ce6b12c22bd38b6a0b2086daa90

See more details on using hashes here.

Provenance

The following attestation bundles were made for unraid_api-1.1.0.tar.gz:

Publisher: workflow.yml on ruaan-deysel/unraid-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unraid_api-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: unraid_api-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unraid_api-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea71cb219b75268a0b1c510226a7abd518f0901f36bf05f8d009a035defe1da0
MD5 e0474e064ab1976134abb47d176500f7
BLAKE2b-256 41331b5d2a726e5175abb5d1212dd35a284ddbcf8b4bad212487a17c0fa28e1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for unraid_api-1.1.0-py3-none-any.whl:

Publisher: workflow.yml on ruaan-deysel/unraid-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page