Skip to main content
The Infrahub Python SDK provides a Python interface to interact with Infrahub’s GraphQL API. It enables you to query, create, update, and delete infrastructure objects programmatically.

Key Features

  • Async-first design - Built with asyncio for high-performance operations
  • Type-safe models - Pydantic-based models with full type hints
  • GraphQL abstraction - Simplified GraphQL query and mutation building
  • Batch operations - Efficient bulk create/update operations
  • Branch support - Work with multiple branches seamlessly
  • Schema introspection - Dynamic schema loading and validation

Architecture

The SDK is organized into several key components:
  • InfrahubClient - Main client for API interactions
  • Node objects - Represent Infrahub objects (devices, interfaces, etc.)
  • Query builder - Construct GraphQL queries programmatically
  • Batch operations - Handle multiple operations efficiently
  • Schema management - Load and validate schemas

Use Cases

Infrastructure Automation

Automate infrastructure provisioning, configuration, and management:
from infrahub_sdk import InfrahubClient

client = InfrahubClient()
device = await client.create(
    kind="InfraDevice",
    name="router-01",
    role="edge"
)
await device.save()

Data Migration

Migrate data from external sources into Infrahub:
batch = await client.create_batch()
for device_data in external_devices:
    device = await client.create(kind="InfraDevice", **device_data)
    batch.add(task=device.save, node=device)

async for node, result in batch.execute():
    print(f"Created {node.name.value}")

Custom Generators

Build custom generators to transform data and create artifacts:
from infrahub_sdk.generator import InfrahubGenerator

class ConfigGenerator(InfrahubGenerator):
    async def generate(self, data: dict) -> None:
        # Transform data and generate configurations
        devices = await self.client.all(kind="InfraDevice")
        for device in devices:
            config = self.render_template(device)
            await self.create_artifact(config)

Integration Scripts

Integrate Infrahub with external systems:
async def sync_from_netbox():
    client = InfrahubClient()
    netbox_devices = fetch_netbox_devices()
    
    for nb_device in netbox_devices:
        device = await client.get(
            kind="InfraDevice",
            name__value=nb_device.name
        )
        if not device:
            device = await client.create(
                kind="InfraDevice",
                name=nb_device.name
            )
        await device.save()

Next Steps

Installation

Install the Infrahub Python SDK

Quick Start

Get started with your first SDK script

Client Setup

Configure the InfrahubClient

Queries

Query data from Infrahub

Build docs developers (and LLMs) love