Hetzner Cloud Python Library
The `hcloud` library is the official Python client for interacting with the Hetzner Cloud API, enabling programmatic management of resources such as servers, networks, volumes, and more. It is actively maintained with frequent releases, currently at version 2.17.1, providing access to the latest Hetzner Cloud features and API updates.
Common errors
-
hcloud: unable to authenticate your token (unauthorized)
cause The provided Hetzner Cloud API token is incorrect, expired, or has insufficient permissions. A common mistake is using a truncated token (only the prefix) displayed in the console instead of the full 64-character token generated upon creation.fixEnsure the `HCLOUD_TOKEN` environment variable contains the *full* 64-character API token generated from the Hetzner Cloud console. Generate a new token if unsure. -
KeyError: 'HCLOUD_TOKEN'
cause The `HCLOUD_TOKEN` environment variable, which the `hcloud` client expects by default for authentication, has not been set in the execution environment.fixSet the `HCLOUD_TOKEN` environment variable before running your Python script. Example: `export HCLOUD_TOKEN='your_api_token'` (Linux/macOS) or `$env:HCLOUD_TOKEN='your_api_token'` (PowerShell). -
SyntaxError: invalid non-printable character U+00A0
cause This error typically occurs when code, especially API keys or configuration strings, is copy-pasted from a web page or document that includes hidden Unicode characters (like a non-breaking space U+00A0) that Python's parser cannot handle.fixCarefully retype the problematic line of code, especially where API tokens or other string literals are defined, or use an editor with whitespace visualization to remove any hidden characters. Ensure your file encoding is UTF-8.
Warnings
- breaking The `datacenter` property in `Servers` and `Primary IPs` objects and API requests is deprecated and will be removed after July 1, 2026. Use the `location` property instead.
- gotcha New features or API endpoints may be introduced as 'experimental'. During an experimental phase, breaking changes to these features may occur in minor releases. Check the release notes and docstrings for 'Experimental' notices.
- deprecated Specific methods may be deprecated in favor of more robust or precise alternatives. For instance, `hcloud.images.client.ImagesClient.get_by_name` was deprecated in favor of `get_by_name_and_architecture`.
Install
-
pip install hcloud
Imports
- Client
from hcloud import Client
Quickstart
import os
from hcloud import Client
HCLOUD_TOKEN = os.environ.get('HCLOUD_TOKEN', '')
if not HCLOUD_TOKEN:
print("Error: HCLOUD_TOKEN environment variable not set.")
exit(1)
client = Client(token=HCLOUD_TOKEN, application_name="my-app", application_version="1.0.0")
# Example: List all servers
try:
servers = client.servers.get_all()
if servers:
print("Hetzner Cloud Servers:")
for server in servers:
print(f" - ID: {server.id}, Name: {server.name}, Status: {server.status}")
else:
print("No servers found.")
except Exception as e:
print(f"An error occurred: {e}")