AgentcardClient is the class every buy tool and listener shares. The class exchanges your company’s client credentials for an access token, renews that token a minute before it expires, and sends purchase turns, conversation reads and Vault link requests to Agentcard. Build one and keep it for the life of your process.
agent.ts
BuyTurn for the shopper user-42. Most apps never call buy themselves: buyTool calls it for the model. See Quickstart for the full wiring.
Create the client
TypeError for any other combination.
Properties
Methods
bearer
sandbox.
buy
BUY_TURN_TIMEOUT_MS. Name the shopper once, with external_user_id or user_id, when you use company credentials, and leave both out under a shopper’s own token.
The client sends a turn once, because a turn runs against live stores. The client sends again only after a 401, once, with a renewed token. After a timeout, unreachable or invalid_answer error, read the conversation with conversation before you send the turn again.
Pass stream with onProgress, onCatalog or both to ask Agentcard to stream the turn. A stream that breaks after the turn started returns a BuyTurn with error_code set to turn_timeout, so the caller reads the conversation for what the turn left.
conversation
who with company credentials. The call waits up to 30 seconds.
mintVaultLink
TypeError under a shopper’s token.
listen
delivered
message reached the chat of the shopper you call externalUserId, so that shopper’s next buy.turn hands it to your model. listen and agentcardWebhook({ agentcard }) call it once onMessage returns, so call it yourself only for a message you delivered another way. The client keeps a message once per eventId, at most 20 messages per shopper, in this process’s memory.
answered
message with a turn of its own, so the shopper never reads its text. delivered skips the message from then on, and no later turn tells the model its text. buy.nextStep calls it.
unanswered
answered for a message your agent’s turn could not answer. After you post the message’s text, delivered keeps it again. turn.release() calls it.
keptMessages
turn.messages holds yet.
forgetMessages
turn.messages calls it once the history it reads holds them.
Constants
BuyRequest
The body of onebuy call. Every field is optional.
BuyDeliveryAddress has the required fields street, city, state and zip, and the optional fields address2, phone, name, country and can_leave_at_door (boolean).
BuyTurn
One turn’s answer. Every field is present, and a field an older Agentcard does not send reads asnull.
BuyNextAction is one of shipping_details, guest_details, store_login, add_card, confirm, verification_code or approve, in the order a purchase asks for them.
BuyCart
A cart as Agentcard answers it. Every amount is in the smallest unit ofcurrency.
BuyCartLine
BuyCatalog
The products the last search found.BuyCatalogItem
BuyPick, BuyStores and BuyStorePick
BuyConversation
Where one conversation stands.BuyOrder has order_id (string or null), merchant, merchant_name, status, total_cents and placed_at (string or null). BuyLastCheckout has at, merchant, merchant_name, status, code, message, order_id, decline_code, approval_url, approval_expires_at, placement_announced, approval_in_progress, add_card_url, add_card_expires_at and total_cents. All of its fields except at, merchant, merchant_name and status are null when absent.
VaultLink
Shopper
Who a call is for. Name the shopper exactly once with company credentials, and leave both fields out under a shopper’s own token.AgentcardApiError
The error every refusal and failed call throws.AgentcardApiError extends Error.
Through
buy.answer, five refusals still answer the shopper: they come back as needs_input with the reason in reply and the code in error_code. They are cart_changed, nothing_to_confirm, conversation_closed, a delivery address that changed since the cart was built (a code that starts with delivery_address), and turn_in_progress. Every other refusal throws an AgentcardApiError.
Error codes
buyTool sends each purchase request once. Only a request that Agentcard refused with a 401 goes again, once, with a renewed token.