Harness Python SDK
harness-python-sdk is the official Python SDK for programmatic interaction with Harness services. It provides a convenient client to automate tasks, manage resources, and integrate Harness functionality into other applications. The current version is 1.0.5, and it appears to have an infrequent release cadence, focusing on stability.
Common errors
-
ModuleNotFoundError: No module named 'harness_python_sdk'
cause The `harness-python-sdk` package has not been installed or is not accessible in your current Python environment.fixInstall the package using pip: `pip install harness-python-sdk` -
AttributeError: module 'harness_python_sdk' has no attribute 'HarnessClient'
cause You are trying to import `HarnessClient` directly from the root `harness_python_sdk` package, but it resides in a submodule.fixCorrect the import statement to `from harness_python_sdk.client import HarnessClient`. -
harness_python_sdk.exceptions.APIError: {'code': '401', 'message': 'Unauthorized', 'detail': ...}cause The API Key or Account ID provided to the `HarnessClient` are incorrect, expired, or lack the necessary permissions for the requested operation.fixDouble-check your `HARNESS_ACCOUNT_ID` and `HARNESS_API_KEY` credentials. Ensure they are valid and have appropriate access rights in Harness.
Warnings
- gotcha Always handle Harness API Key and Account ID securely. Do not hardcode them directly in your source code, especially in production environments.
- gotcha The `HarnessClient` and other service objects (e.g., `PlatformService`) are nested within submodules. Incorrect import paths are a common source of `AttributeError` or `ModuleNotFoundError`.
- gotcha API calls can fail due to network issues, invalid input, or server errors. The SDK raises `harness_python_sdk.exceptions.APIError` for most API-specific errors.
Install
-
pip install harness-python-sdk
Imports
- HarnessClient
from harness_python_sdk import HarnessClient
from harness_python_sdk.client import HarnessClient
- PlatformService
from harness_python_sdk import PlatformService
from harness_python_sdk.services.platform import PlatformService
Quickstart
import os
from harness_python_sdk.client import HarnessClient
# Securely load credentials from environment variables
ACCOUNT_ID = os.environ.get('HARNESS_ACCOUNT_ID', 'your_account_id')
API_KEY = os.environ.get('HARNESS_API_KEY', 'your_api_key')
if not ACCOUNT_ID or not API_KEY:
print("Please set HARNESS_ACCOUNT_ID and HARNESS_API_KEY environment variables or replace placeholders.")
else:
try:
# Initialize the Harness Client
client = HarnessClient(account_id=ACCOUNT_ID, api_key=API_KEY)
print(f"Harness Client initialized for Account ID: {ACCOUNT_ID}")
# Example: Try to access a service (e.g., PlatformService)
# This will raise an exception if credentials are bad or API is unreachable
platform_service = client.platform
# In a real scenario, you'd call a method on platform_service, e.g., list users
print("Successfully accessed platform service. Connection appears valid.")
except Exception as e:
print(f"An error occurred during client initialization or API access: {e}")