How to create and configure an Infrahub client
This guide shows you how to create and configure an Infrahub client using the Python SDK. You'll learn how to set up authentication, configure proxy settings, and customize client behavior for your infrastructure automation workflows.
At the end of this guide, you'll have a fully configured client ready to interact with your Infrahub instance.
If you prefer to jump right in, check out the "Hello World" example at the end of this guide for a quick reference implementation.
Prerequisites
- Python 3.9 or higher
- Infrahub SDK installed (
pip install infrahub-sdkoruv add infrahub-sdk) - Access to an Infrahub instance (local or remote)
- Valid credentials (API token or username/password) if authentication is required
Step 1: Create a client instance
The SDK offers both asynchronous and synchronous client implementations. Choose the one that best fits your application architecture:
- Asynchronous client (
InfrahubClient): Ideal for modern async applications using Python'sasync/awaitsyntax - Synchronous client (
InfrahubClientSync): Better for traditional synchronous workflows or scripts
- Async
- Sync
Create an InfrahubClient for async/await workflows:
from infrahub_sdk import InfrahubClient
# Connect to local Infrahub instance
client = InfrahubClient()
Create an InfrahubClientSync for traditional synchronous code:
from infrahub_sdk import InfrahubClientSync
# Connect to local Infrahub instance
client = InfrahubClientSync()
Step 2: Configure authentication and address
Next, configure the address of your Infrahub instance and set up authentication credentials. Infrahub supports two authentication methods: API tokens and username/password.
You can configure the client in two ways:
- Environment variables: Ideal for sensitive information
Configobject: Better for explicit configuration in code
You can also combine both methods, where the Config object takes precedence over environment variables.
You can find the full list of configuration options in the SDK configuration reference.