Skip to main content
The Infrahub Python SDK (infrahub-sdk) provides a comprehensive, type-safe interface to interact with Infrahub’s GraphQL API and manage your infrastructure data programmatically.

Overview

The Python SDK enables you to:
  • Query and manage objects: Create, read, update, and delete nodes in your infrastructure graph
  • Work with schemas: Load and validate schema definitions dynamically
  • Manage branches: Create and work with Git-like branches for your infrastructure data
  • Handle relationships: Define and traverse complex relationships between infrastructure components
  • Execute batch operations: Perform bulk operations efficiently
  • Use async/await: Built with modern async Python for optimal performance

Key Features

Type Safety

Full type hints and Pydantic models ensure type safety across your codebase.

Async First

Built with async/await for high-performance concurrent operations.

GraphQL Integration

Seamlessly work with Infrahub’s GraphQL API without writing queries manually.

Schema Management

Dynamically load and validate schemas for your infrastructure models.

Architecture

The SDK is structured around key components:

InfrahubClient

The main client class providing access to all SDK functionality:
from infrahub_sdk import InfrahubClient

client = InfrahubClient()

Node Objects

Represent infrastructure objects with attributes and relationships:
device = await client.get(kind="InfraDevice", id="abc-123")
print(device.name.value)  # Access attributes
print(device.location.id)  # Access relationships

Schema Management

Manage and validate your infrastructure schema:
await client.schema.load(schemas=[schema_dict])
await client.schema.fetch(namespaces=["Infra"])

Branch Operations

Work with Git-like branches:
branch = await client.branch.create(branch_name="feature-update")

Python Version Support

The SDK requires Python 3.12 or higher.
requires-python = ">=3.12,<3.14"

Key Dependencies

The SDK is built on top of:
  • Pydantic 2.x: Data validation and settings management
  • httpx: Modern async HTTP client
  • GraphQL: For API communication
  • Rich: Enhanced terminal output

Use Cases

Infrastructure Automation

Automate infrastructure provisioning and configuration:
# Create devices programmatically
for device_config in device_list:
    device = await client.create(
        kind="InfraDevice",
        name=device_config["name"],
        location=location_obj
    )
    await device.save()

Data Migration

Migrate infrastructure data between systems:
# Export from existing system
devices = await client.all(kind="InfraDevice")

# Transform and import
for device in devices:
    # Transform data...
    await device.save()

Reporting & Analytics

Query and analyze infrastructure data:
# Gather metrics
devices = await client.all(kind="InfraDevice")
by_location = {}
for device in devices:
    location = device.location.value
    by_location[location] = by_location.get(location, 0) + 1

Testing & Validation

Validate infrastructure configurations:
# Test schema migrations
await client.schema.load(schemas=[new_schema])
assert await client.schema.in_sync()

Next Steps

Installation

Install the SDK and configure your environment

Quickstart

Get started with your first SDK application

Client Setup

Configure the client for your environment

Querying Data

Learn how to query infrastructure objects

Build docs developers (and LLMs) love