Skip to content
Docs

Python agents over HTTP

A complete Python client: request an action, wait for approval, handle every outcome.

01Setup

This client uses httpx (pip install httpx) and the agent credential from the ARC_AGENT_CREDENTIAL environment variable. There is no SDK to install: Arc is one endpoint.

02The client

Python
import osimport timeimport httpxARC = httpx.Client(    base_url="https://api.arcintell.org",    headers={"Authorization": f"Bearer {os.environ['ARC_AGENT_CREDENTIAL']}"},    timeout=30.0,)class Refused(Exception):    """Arc decided no, a person rejected it, or the approval expired."""class OutcomeUnknown(Exception):    """Arc sent the request but cannot tell whether the system acted."""def request_action(request_id: str, action: str, *, on_behalf_of: dict,                   resource: dict, purpose: str, parameters: dict | None = None,                   amount: str | None = None, currency: str | None = None) -> dict:    body = {        "request_id": request_id,      # stable per intended action: safe to retry        "action": action,        "on_behalf_of": on_behalf_of,        "resource": resource,        "purpose": purpose,        "parameters": parameters or {},    }    if amount is not None:        body["amount"], body["currency"] = amount, currency    for attempt in range(4):        try:            r = ARC.post("/v1/actions", json=body)        except httpx.TransportError:            time.sleep(2 ** attempt)    # network trouble: retry, same request_id            continue        if r.status_code >= 500:            time.sleep(2 ** attempt)    # server trouble: retry, same request_id            continue        r.raise_for_status()            # 401, 409, 422: a bug to fix, not to retry        return r.json()    raise RuntimeError("Arc did not answer")def wait_for(action: dict, every: float = 30.0, give_up_after: float = 3600.0) -> dict:    """Follow a held action until a person decides it."""    waited = 0.0    while action["status"] in ("held", "executing") and waited < give_up_after:        time.sleep(every)        waited += every        action = ARC.get(f"/v1/actions/{action['action_id']}").raise_for_status().json()    return actiondef settle(action: dict) -> dict:    status = action["status"]    if status == "succeeded":        return action["execution"]["captured"]    if status == "unknown":        raise OutcomeUnknown(action["action_id"])    if status == "failed":        e = action["execution"]        raise RuntimeError(f"{e['failure_code']}: {e['failure_message']}")    reason = action["refusal"]["message"] if action.get("refusal") else action["explanation"]    raise Refused(f"{status}: {reason}")

Example

  • request_action retries network and server errors with the same request_id, so a retry can never perform the action twice.
  • wait_for polls a held action. Approvals expire after 24 hours; give up sooner if your task cannot wait.
  • settle turns every final status into a result or an exception, and never retries an unknown outcome.

03Using it

Python
action = request_action(    "refund-ticket-77",    "refund:create",    on_behalf_of={"kind": "customer", "id": "cus_8812"},    resource={"type": "payment", "id": "pay_3141"},    purpose="Refund a duplicate charge",    amount="42.50",    currency="USD",    parameters={"reason": "duplicate"},)if action["status"] == "held":    print("Waiting for approval:", action["action_id"])    action = wait_for(action)refund = settle(action)        # {'id': 're_5521', 'status': 'pending'}

Example

04Next

Every field is in POST /v1/actions, and every status and code in Statuses and refusal codes.