Skip to main content
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
The call returns one 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

Pass your company’s credentials, or one shopper’s own access token, and not both. The constructor throws a TypeError for any other combination.

Properties

Methods

bearer

Returns the access token the client sends: the shopper’s own, or your company’s, exchanged once and renewed a minute before it expires. Pass a company token that Agentcard refused to get a new one. The first call also sets sandbox.

buy

Sends one purchase turn and waits up to 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

Reads where one conversation stands: whether a turn is running, the last checkout, the orders it placed and the carts on the table. Pass who with company credentials. The call waits up to 30 seconds.
Returns the Vault link a shopper opens to connect to your company and add a card. Agentcard returns the link it already opened for that shopper while the link has time left, so a shopper who asks twice gets one link. The method needs company credentials and throws a TypeError under a shopper’s token.

listen

Hears every event Agentcard sends your company over one connection your server opens. See listen.

delivered

Records that 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

Records that your agent answers 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

Takes back 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

Returns the messages kept for this shopper, oldest first: each one that reached the shopper’s chat and that no history passed to turn.messages holds yet.

forgetMessages

Forgets these messages for this shopper. turn.messages calls it once the history it reads holds them.

Constants

BuyRequest

The body of one buy 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 as null. 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 of currency.

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.

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.