Skip to main content

Money

The typed meta-tool wrappers: charge, ship, ledger, issue and shop.

5 min read
View MarkdownEdit on GitHub

Money

Five methods that call a meta-tool with a typed argument and hand back a typed result instead of a ToolResult to cast. Each one is execute("codespar_<tool>", args) on the wire, so what a page under Meta-tools says about the tool's actions and providers applies here unchanged; what these pages add is the TypeScript shape and what the wrapper throws.

All five share one failure rule: when the tool result is not success, the wrapper throws a plain Error whose message is <method> failed: <error>. Network failures and timeouts surface as they do on execute.

charge

Writecharge(args: ChargeArgs): Promise<ChargeResult>
Can move moneyPythonsession.charge(args)

Inbound charge: the buyer pays the merchant. Routes to the tenant's connected charge-issuing providers.

Creates something the buyer pays: a Pix QR, a boleto, a hosted card checkout or a wallet redirect. Distinct from codespar_pay, which is an outbound transfer and has no typed wrapper. amount is in major units.

ParameterTypeRequiredDescription
argsChargeArgsyesAmount, currency, method, description and the buyer
ChargeArgs fieldTypeRequiredDescription
amountnumberyesMajor currency units: R$ 125.00 is 125
currencystringyesISO 4217: BRL, USD, EUR
method"pix" | "boleto" | "card" | "wallet"yesThe rail
descriptionstringyesShown to the buyer
buyer{ name: string; email?: string; document?: string; phone?: string }yesCharges are merchant-issued, so the buyer is always named
due_datestringnoISO 8601; boleto due date or Pix expiration
Example
const  = await .({
  : 150,
  : "BRL",
  : "pix",
  : "Order 0000",
  : { : "Example Buyer", : "buyer@example.com" },
});
.(., ., .);
Result: ChargeResult
interface ChargeResult {
  id: string;
  status: string;
  amount: number;
  currency: string;
  method: string;
  /** Hosted payment URL when the provider issues one */
  charge_url?: string;
  pix_qr_code?: string;
  pix_copy_paste?: string;
  raw?: unknown;
}

Throws Error("charge failed: ...") when the tool result is not success; otherwise as execute.

Related codespar_charge. Settlement is asynchronous: correlate it with paymentStatus using the tool_call_id that execute returns for the same call.

ship

Writeship(args: ShipArgs): Promise<ShipResult>
Pythonsession.ship(args)

Quote carriers, buy a label, or track a shipment, over one address-and-items envelope. Routes BR domestic shipments through Melhor Envio (Correios and private carriers); the router picks the cheapest carrier unless service_level says otherwise.

ParameterTypeRequiredDescription
argsShipArgsyesaction plus what that action needs
ShipArgs fieldTypeRequiredDescription
action"label" | "track" | "quote"yesBuy a label, track, or price a route
originShipAddressfor label and quote{ postal_code, city?, state?, country?, line_1?, number? }
destinationShipAddressfor label and quotesame shape
itemsShipItem[]for label and quote{ weight_g, width_cm?, height_cm?, length_cm?, quantity?, declared_value?, description? }
service_level"fastest" | "cheapest" | "standard"noDefault cheapest
tracking_codestringfor trackThe code a label returned
metadataRecord<string, unknown>noProvider overrides: Melhor Envio service id, NF-e key for declared value
Example
const  = await .({
  : "quote",
  : { : "01310100" },
  : { : "20040020" },
  : [{ : 500, : 20, : 10, : 30 }],
});
.(., ., .);
Result: ShipResult
interface ShipResult {
  id: string;
  status: string;
  tracking_code?: string;
  label_url?: string;
  carrier?: string;
  estimated_delivery?: string;
  /** Minor units */
  cost_minor?: number;
  raw?: unknown;
}

Throws Error("ship failed: ...") when the tool result is not success; otherwise as execute.

Related codespar_ship.

ledger

Writeledger(args: LedgerArgs): Promise<LedgerResult>
Pythonsession.ledger(args)

The books, not the money: post a double-entry journal entry, read an account's balances, create an account, or read an agentic receipt, on the tenant's self-hosted Lerian Midaz ledger. Source debits must equal destination credits in the same asset. Amounts are in minor units.

ParameterTypeRequiredDescription
argsLedgerArgsyesaction plus what that action needs
LedgerArgs fieldTypeRequiredDescription
action"entry" | "balance" | "account" | "receipt" | "receipts"yesWhich operation
assetstringfor entry and accountBRL, USD, USDC, ...
scalenumbernoDecimal places; default 2
source, destination{ account: string; amount: number }[]for entryDebit and credit legs; account is an alias such as @wallet/user_0000
descriptionstringnoEntry description
accountstringfor balanceAccount UUID
alias, name, typestringfor accountAlias, display name, Midaz account type (default deposit)
receipt_idstringfor receiptrcpt_...
consumer_id, limitstring, numberfor receiptsWhose receipts, how many (default 50)
metadataRecord<string, unknown>noStored on the entry or account
Example
const  = await .({
  : "entry",
  : "BRL",
  : 2,
  : [{ : "@wallet/user_0000", : 15000 }],
  : [{ : "@revenue/store", : 15000 }],
  : "Order 0000 settled",
});
.(., .);
Result: LedgerResult
interface LedgerResult {
  /** Transaction or account id (entry / account) */
  id?: string | null;
  status?: string;
  account_id?: string | null;
  alias?: string | null;
  /** Per-asset available + on-hold amounts (balance) */
  balances?: unknown;
  raw?: unknown;
}

// action=receipt and action=receipts return these instead:
interface LedgerReceiptResult { found: boolean; receipt?: AgenticReceipt; receipt_id?: string }
interface LedgerReceiptsResult { receipts: AgenticReceipt[]; count: number }

The return type is LedgerResult for every action; for receipt and receipts the value on the wire has the LedgerReceiptResult and LedgerReceiptsResult shapes, which @codespar/types exports for the cast.

Throws Error("ledger failed: ...") when the tool result is not success; otherwise as execute.

Related codespar_ledger, Audit chain for what an agentic receipt proves.

issue

Writeissue(args: IssueArgs): Promise<IssueResult>
Pythonsession.issue(args)

Issue a virtual or physical card, freeze, unfreeze or cancel one, or read its status, on the tenant's card-issuing program. Creates spend instruments; it does not move money by itself.

ParameterTypeRequiredDescription
argsIssueArgsyesaction plus what that action needs
IssueArgs fieldTypeRequiredDescription
action"card-virtual" | "card-physical" | "card-control" | "card-get"yesWhich operation
cardholder_idstringto issueThe issuer's user id
program_idstringto issueThe card program (BIN)
card_idstringfor card-control and card-get
control"freeze" | "unfreeze" | "cancel"for card-control
reasonstringnoStamped on a control action
shipping_addressRecord<string, unknown>for card-physical
metadataRecord<string, unknown>no
Example
const  = await .({
  : "card-virtual",
  : "chd_0000",
  : "prg_0000",
});
.(., ., .);
Result: IssueResult
interface IssueResult {
  id?: string | null;
  status?: string | null;
  card_type?: string | null;
  last_four?: string | null;
  cardholder_id?: string | null;
  program_id?: string | null;
  raw?: unknown;
}

Throws Error("issue failed: ...") when the tool result is not success; otherwise as execute.

Related codespar_issue.

shop

Writeshop(args: ShopArgs): Promise<ShopResult>
Can move moneyPythonsession.shop(args)

Checkout mints the store's real Pix copia-e-cola, to be settled from the agent's governed wallet; the contract stops at the Pix and performs no settlement.

Buy-side shopping: search a store's live catalog, start a checkout, poll it to a payable Pix. The argument and result are discriminated on action, so the result type follows the action without a cast. Checkout is asynchronous: checkout returns in_progress at once; poll checkout_status until ready_for_payment (which carries pix_copia_e_cola and total_minor) or canceled (which carries error). Requires a runtime with a registered codespar_shop implementation; a self-hosted runtime without one answers Tool not registered.

ParameterTypeRequiredDescription
argsShopSearchArgs | ShopCheckoutArgs | ShopStatusArgsyesDiscriminated on action
actionFieldsResult
"search"query (required), limit? (1..20, default 10), merchant?ShopSearchResult: { rail, products: ShopOffer[] }
"checkout"items? (VTEX rail, by variant_id) or url? (Mercado Livre PDP), never both; merchant?, consumer_id?, buyer?, address?ShopCheckoutResult: { checkout_session_id, status: "in_progress", message? }
"checkout_status"checkout_session_id (required)ShopStatusResult: { checkout_session_id, status, rail?, total_minor?, pix_copia_e_cola?, order_status?, error? }
Example
const  = await .({ : "search", : "dog food", : "cobasi" });
if ("products" in ) {
  const  = .[0]?.[0]?.;
  .();
}

const  = await .({
  : "checkout",
  : "cobasi",
  : [{ : "sku_0000", : 1 }],
});
if ("checkout_session_id" in ) {
  const  = await .({
    : "checkout_status",
    : .,
  });
  if ("pix_copia_e_cola" in ) .(.);
}
ShopOffer
interface ShopOffer {
  product_id: string;
  /** Offer-level SKU when there is a single buyable SKU */
  sku_id?: string;
  title?: string;
  /** Minor units */
  price_minor?: number;
  currency?: string;
  image?: string;
  url?: string;
  available: boolean;
  variants: ShopVariant[];
}

interface ShopVariant {
  /** Pass this as the checkout item's variant_id; the product id is not buyable */
  sku_id: string;
  title?: string;
  price_minor?: number;
  currency?: string;
  available: boolean;
}

Throws Error("shop failed: ...") when the tool result is not success, including Tool not registered; otherwise as execute. A canceled checkout is a value with error set, not a throw.

Related codespar_shop for the rails, the cancel reasons and the state machine; settle the Pix with codespar_pay through execute.

Money | CodeSpar