Files
python-sdk/examples/client.py
T
zooqueenandHanzo Dev 2ba4b9129e auth: the two key shapes are pk- and sk-, and one sentence says so
Cloud's APIKeyPrefixes admits pk- and sk- only (auth_identity.go, 4b030b77).
The lsp tool still told the user to set an hk- key, which cloud resolves to no
principal — following the message produced a key that could never authenticate.

The message is now a single NO_KEY beside the resolver that mints it, exported
from hanzo_tools.core, so a second tool reporting it cannot spell it differently.
Resolution order is untouched: HANZO_API_KEY, then ~/.hanzo/config.json .apiKey,
then the CLI's IAM session.

vector_index names the split cloud actually draws: pk- publishable for read,
sk- secret for write.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-08 16:06:25 -07:00

59 lines
1.8 KiB
Python

"""The one place an example learns where the API is and who it is.
Every flow imports this and nothing else builds a client, so there is a single
answer to "which base URL?" and "which env var?" across all six.
Run any flow from the repo root::
python -m examples.hello
"""
from __future__ import annotations
import os
import sys
from hanzoai.cloud import ApiClient, Configuration
from hanzoai.cloud.exceptions import ApiException
#: Default host. ``HANZO_BASE_URL`` overrides it (staging, a local cloud, a tunnel).
BASE_URL = os.environ.get("HANZO_BASE_URL", "https://api.hanzo.ai")
#: ``zen4`` is the flagship the spec documents as its own example value.
MODEL = os.environ.get("HANZO_MODEL", "zen4")
def api_key() -> str:
"""Fail loudly and early when the key is absent.
Without this the SDK sends an unauthenticated request and the flow dies on a
401 several frames deep, which reads like an API bug rather than an unset
shell variable.
"""
key = os.environ.get("HANZO_API_KEY")
if not key:
raise SystemExit("HANZO_API_KEY is not set — export an IAM JWT or a pk-/sk- cloud key")
return key
def client() -> ApiClient:
"""An ApiClient bound to the configured host and bearer key.
``access_token`` becomes ``Authorization: Bearer <key>``, which is the only
scheme the spec declares.
"""
return ApiClient(Configuration(host=BASE_URL, access_token=api_key()))
def run(main) -> None:
"""Invoke a flow and report an API failure the way a caller can act on.
ApiException stringifies to the status line alone; the server's explanation
is in ``.body``, which is the part worth printing.
"""
try:
main()
except ApiException as e:
print(f"HTTP {e.status}: {e.body}", file=sys.stderr)
raise SystemExit(1) from e