Skip to content
aityx
Esc
↑↓navigate↵open⌘Jpreview
On this page

Call it from JavaScript and Python

Submit the complete model, keep the returned receipt and handle missing facts.

These clients load invoice-model.json from the quickstart, submit the complete model with each invoice, and return the full execution receipt for your application to keep.

import { readFile } from "node:fs/promises";

const AITYX = "https://api.aityx.ai";
const model = JSON.parse(await readFile("invoice-model.json", "utf8"));

export async function decidePayment(invoice) {
  const res = await fetch(`${AITYX}/v1/systemone`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.AITYX_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model,
      state: {
        invoice_amount: invoice.amountUsd,
        invoice_date: invoice.issuedOn,
        payment_on: invoice.paymentOn,
        approved: invoice.approved,
      },
    }),
  });
  const result = await res.json();
  if (!res.ok) {
    if (result.code === "invalid_state") {
      return { outcome: "needs_facts", missing: result.errors.map(e => e.path) };
    }
    throw new Error(`aityx ${res.status}: ${result.detail ?? "request failed"}`);
  }
  return {
    outcome: result.answers.payment_action.choice,
    payableUsd: result.answers.payable_amount.number,
    discountBy: result.answers.discount_by.date,
    receipt: result.receipt,
    billing: result.billing,
  };
}
import json
import os
import requests

AITYX = "https://api.aityx.ai"
with open("invoice-model.json") as file:
    model = json.load(file)


def decide_payment(invoice: dict) -> dict:
    res = requests.post(
        f"{AITYX}/v1/systemone",
        headers={"Authorization": f"Bearer {os.environ['AITYX_API_KEY']}"},
        json={
            "model": model,
            "state": {
                "invoice_amount": invoice["amount_usd"],
                "invoice_date": invoice["issued_on"],
                "payment_on": invoice["payment_on"],
                "approved": invoice.get("approved"),
            },
        },
        timeout=30,
    )
    result = res.json()
    if not res.ok:
        if result.get("code") == "invalid_state":
            return {"outcome": "needs_facts", "missing": [e["path"] for e in result["errors"]]}
        res.raise_for_status()
    return {
        "outcome": result["answers"]["payment_action"]["choice"],
        "payable_usd": result["answers"]["payable_amount"]["number"],
        "discount_by": result["answers"]["discount_by"]["date"],
        "receipt": result["receipt"],
        "billing": result["billing"],
    }

Handle the outcome

Outcome What your application can do
discount Use the computed amount and cutoff when scheduling the payment.
full Use the full payable amount; keep the receipt explaining why the discount did not apply.
review Ask a person to approve the invoice. Include the receipt showing the rule.
needs_facts Ask for the missing information and call again. No decision was executed.

Keep the complete model, original state and full receipt together in your own storage. The receipt records established inputs, not a copy of all submitted evidence. An id alone cannot retrieve it later. The returned review decision routes the case; your application implements the approval or payment.

Send text when you do not have structured facts

The state value can instead be text or other JSON evidence. The AI reader extracts the inputs before the engine executes. Model-supplied execution uses System One pricing; extraction adds a separate System Two charge. The receipt lists the extracted facts.

state: "Approved invoice for USD 25,000, issued October 1, 2026. Pay on October 11, 2026."

Build outside the application request path

Create, inspect and test a model before your application starts using it. Deploy the reviewed file with your application, and submit that definition with each call. Sending questions again uses System Two pricing even when the service reuses a cached model.

Console and API calls share one balance. Handle insufficient funds alongside other failed requests, rather than silently using an unverified substitute decision.

Was this page helpful?