Skip to main content

Using the API from Python

The MeetLoyd API is plain HTTPS and JSON, so any Python HTTP library works: requests, httpx or aiohttp. There is no separate Python package to install.

List agents and run one​

import os
import requests

BASE_URL = "https://api.meetloyd.com/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['MEETLOYD_API_KEY']}"} # ml_…

agents = requests.get(f"{BASE_URL}/agents", headers=HEADERS, timeout=30)
agents.raise_for_status()
for agent in agents.json()["agents"]:
print(agent["id"], agent["name"], agent["status"])

run = requests.post(
f"{BASE_URL}/agents/{agent['id']}/execute",
headers=HEADERS,
json={"message": "Summarise yesterday's tickets"},
timeout=300,
)
run.raise_for_status()
print(run.json()["output"])

Running an agent waits for its answer, so give that call a generous timeout.

Async with httpx​

import os
import httpx

async def list_agents():
async with httpx.AsyncClient(
base_url="https://api.meetloyd.com/api/v1",
headers={"Authorization": f"Bearer {os.environ['MEETLOYD_API_KEY']}"},
) as client:
response = await client.get("/agents")
response.raise_for_status()
return response.json()["agents"]

Errors​

A failed call returns a JSON body with a short error code and usually a message:

response = requests.get(f"{BASE_URL}/agents/agent_missing", headers=HEADERS, timeout=30)
if response.status_code == 404:
print(response.json().get("message"))

See the API Overview for status codes, pagination and every resource, and Conversations for streamed answers.