Examples
Python
Async Python client examples for the Elizon Public API, using httpx.
Examples use httpx for the HTTP calls and httpx-sse to consume the streaming endpoint. Install both:
pip install httpx httpx-sseSynchronous run
import httpx
BASE_URL = "https://api.elizon.com/api/v1"
async def run(agent_id: str, message: str, api_key: str, session_id: str | None = None) -> dict:
headers = {"X-API-Key": api_key, "Content-Type": "application/json"}
body = {"message": message}
if session_id:
body["session_id"] = session_id
async with httpx.AsyncClient() as client:
r = await client.post(f"{BASE_URL}/agents/{agent_id}/run", headers=headers, json=body)
r.raise_for_status()
return r.json()Streaming run
import httpx
from httpx_sse import aconnect_sse
async def run_stream(agent_id: str, message: str, api_key: str) -> str:
headers = {"X-API-Key": api_key, "Content-Type": "application/json"}
full_content = ""
async with httpx.AsyncClient() as client:
async with aconnect_sse(
client, "POST", f"{BASE_URL}/agents/{agent_id}/run-stream",
headers=headers, json={"message": message},
) as event_source:
async for sse in event_source.aiter_sse():
event = sse.json()
if event["event"] == "RunContent":
full_content += event["content"]
elif event["event"] == "RunCompleted":
print("Metrics:", event["metrics"])
elif event["event"] == "error":
raise RuntimeError(f"Run failed: {event['reason']}")
return full_contentAsync job polling
import asyncio
import httpx
async def run_and_poll(agent_id: str, message: str, api_key: str) -> dict:
base = "https://api.elizon.com/api/v1"
headers = {"X-API-Key": api_key, "Content-Type": "application/json"}
async with httpx.AsyncClient() as client:
# Submit async job
r = await client.post(f"{base}/agents/{agent_id}/run-async",
headers=headers, json={"message": message})
job_id = r.json()["jobId"]
# Poll until terminal
interval, elapsed = 2, 0
while True:
await asyncio.sleep(interval)
elapsed += interval
if elapsed > 30:
interval = 5
r = await client.get(f"{base}/jobs/{job_id}", headers=headers)
job = r.json()
if job["status"] in ("completed", "failed", "timeout", "cancelled"):
return jobUsage
import asyncio
async def main():
api_key = "elz_your_key_here"
agent_id = "3f9c1a2e-..."
result = await run(agent_id, "Hello!", api_key)
print(result["content"])
content = await run_stream(agent_id, "Hello!", api_key)
print(content)
job = await run_and_poll(agent_id, "A longer task...", api_key)
print(job["status"], job.get("result"))
asyncio.run(main())