Skip to main content
agentcard.listen(options) opens one connection from your server to Agentcard with your company credentials. Agentcard sends every event for your company down it, so you need no public URL, no webhook secret and no signature check. Each event is the envelope a webhook endpoint would receive, with the same id.
server.ts
postToChat stands for your own code. onMessage posts each shopper message into the right chat, onEvent logs every other event, and ready resolves once the listener reaches Agentcard. The Quickstart page shows where listen fits in a server. listen needs a process that keeps running, such as a Node server, a worker or a container. On a host that ends each request after a few seconds, such as a function the host starts for each request, take the events at a webhook endpoint instead.

Start listening

Both forms do the same thing, and the method calls the function. The call returns at once. The function throws a TypeError when the client was built with a shopper’s access token, when you pass neither onMessage nor onEvent, when events is empty or names something Agentcard does not send, or when transport is not auto, stream or poll. Run one listener per company and mode. Two listeners each hear every event, so with two, skip a message.eventId you already stored. Sandbox credentials hear sandbox events, live credentials hear live ones, and a shopper message is always live.

Options

onMessage and onEvent

onMessage gets the same ready-to-send message as agentcardWebhook: a card saved, an order placed, an approval about to expire, a delivery on its way. See Message types for the fields. onEvent gets every other event, such as vault.card_stored or checkout_authorization.approved. A message goes to onEvent instead when it is for a shopper you gave Agentcard no id for, or when its link or window closed before it arrived. Once onMessage returns, the client keeps the message for that shopper’s next buy.turn, which hands it to your model. A message whose onMessage threw every time never reached the chat, so the client does not keep it.

Return value

listen returns an AgentcardListener.

Delivery rules

Events run one at a time, in the order Agentcard recorded them. The listener waits for your callback before it hands over the next event. A cursor Agentcard no longer keeps goes to onError, and the listener starts from now.

Resume across restarts

A new process starts from the moment it connects. Pass a cursor store to resume where the last process stopped.
load returns the cursor saved last, or null to start from now. save runs after each handled event, and at most once a minute while no event arrives. Treat the saved string as opaque. After a resume, Agentcard hands over the last 10 seconds of events again, in case one of them arrived late. The listener saves the cursor with the id of each event it handled in that window, so a new process skips the ones it already handled.
server.ts
redis stands for your own store. A restarted server resumes after the last event it handled.

Event types

AGENTCARD_EVENT_TYPES lists every event type Agentcard sends your company, the same list a webhook endpoint subscribes to by name. AgentcardEventType is the union of those names. The tables below link each event that has its own page.

Purchases and orders

The Orders events are covered in Order events. purchase.message, order.confirmed and order.updated carry a message for a shopper, so onMessage receives them. Subscribe a webhook endpoint to each by name.

Vault and checkout authorizations

Cards and connections

Company balance

Other types in the list

AGENTCARD_EVENT_TYPES also holds these types, which have no page of their own yet: cardholder.updated, connected_card.updated, vault.session_card_picked, action.confirmed, action.done, checkout_authorization.cancelled, checkout_authorization.duplicate_charge, checkout_authorization.outcome_reported, transfer.initiated, transfer.failed, transfer.released, user_wallet.funding_detected, user_wallet.funded, recovery.completed, recovery.rejected and wallet_link.opened. A type Agentcard adds later reaches onEvent under its own name. The Webhooks tab describes how each event’s payload reads.