> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentcard.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Agentcard is for companies whose agent buys for their users. Pick one path: the opencommerce SDK (@agent-cards/commerce-sdk) gives your agent one buy tool that finds the product, builds the cart, gets the user's approval and places the order at real stores; the Agentcard SDK (@agent-cards/sdk) pays with the user's own card when your agent already shops in a browser (Browser Use, Kernel, Browserbase, Anchor or Notte); the Purchase API (POST /buy) does the same purchase flow over plain HTTP in any language; Issuing gives the agent its own card.
> Start with `npx agent-cards init` in the app's folder (Node 22 or later). No invite is needed: it creates your Agentcard account if you don't have one, installs the opencommerce SDK and puts the credentials in the env file, kept out of git. From a coding agent, run `npx agent-cards init --print --categories all --implementation tool --wallet vault --skip-install` to get the setup steps to apply yourself.
> Vault is by invitation: request it at https://agentcard.com/get-started?utm_source=docs&utm_medium=llms&utm_campaign=agent-instructions. opencommerce needs no invite.
> Never ask a user for a card number, expiry date or CVV in chat. Send them the Vault link; they add their card on Agentcard's page, and the agent never sees the number.
> Every purchase is approved by the user, with a passkey or Face ID, or by an auto-approval they granted. An approval covers one merchant and one exact total. If the total changes, ask again. When a result says the user already said yes, don't ask again.
> `init` writes PRODUCTION credentials, which place real orders, so test in sandbox first. For opencommerce, run `npx agent-cards credentials --mode sandbox` and put the two lines it prints in place of the production ones in the env file `init` wrote, because that command only prints them; sandbox then runs the whole flow against real stores and stops before payment, so no order is placed and nobody is charged. For Vault and Issuing, sandbox credentials issue test cards that no real merchant accepts; try the checkout against shop.agentcard.sh, a test store on Stripe test mode.
> Tell the user an order is placed only after Agentcard reports it placed.
> Vault is free up to 5,000 users (https://agentcard.com/pricing). opencommerce takes 1% of each completed order. Full docs in one file: https://docs.agentcard.com/llms-full.txt

# Show results that arrive after the reply

> Show a better price or more hotels and flights that Agentcard finds after the reply, without moving the cards the shopper already numbered.

Show the shopper what a slower store or travel search finds after Agentcard's reply, in the same turn. A best-price ask can end with "Found a better price: LaCroix Lime 12 pack at Walmart, \$4.98." and that product in a row of its own. A hotel search can end with "I found 2 more hotels." and those hotels in a row under it.

The cards the reply showed never change after it, so "the second one" still means the card the shopper saw second. A late product either gets its own row under one message, or joins the products behind "show more".

## Keep the turn open

A streamed turn stays open after the reply while a store or a travel search is still out, for up to 60 seconds. The shopper's next message ends the wait at once. A call that does not stream answers at the reply and waits for nothing.

> [tab] SDK
>
> The buy tool streams every turn by default and reads the late results for you. Nothing else to set.

> [tab] MCP
>
> The `buy` tool answers once, with what it found by the reply. A better price found later reaches your webhook endpoint as a `purchase.message`.

> [tab] API
>
> Call `POST /buy?stream=1`. Send `stream_purchase_messages: true` when your app reads purchase messages from the stream, and `stream_more_found: true` beside it when your app shows more hotels or flights as a row:
>
> ```bash theme={null}
> curl -N "https://api.agentcard.sh/buy?stream=1" \
>   -H "Authorization: Bearer $AGENTCARD_TOKEN" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "external_user_id": "user-42",
>     "ask": "best price on lacroix lime 12 pack",
>     "stream_purchase_messages": true,
>     "stream_more_found": true
>   }'
> ```
>
> The `envelope` event still comes last, once the turn ends.

## Stop the dots at the reply

Once the reply is written, Agentcard says so, though the turn is still open. Stop your typing dots then, so the shopper reads the reply as finished.

> [tab] SDK
>
> Pass `onReplyDone` to `buyTool` or `buy.turn`:
>
> ```ts chat.ts theme={null}
> const turn = buy.turn(message, {
>   onReplyDone: () => hideDots(shopper.id),
> });
> ```
>
> `hideDots` stands for your own code. `onReplyDone` comes once a call, after the tool showed the whole reply through `onText`, or through `onItem` or `onMessage` with `replyAsWritten: true`. A turn with nothing still out hears none, and its call answers at once.

> [tab] MCP
>
> The `buy` tool answers once, so the dots stop when it answers.

> [tab] API
>
> The stream sends `reply_written` once, and only when the turn then waits. `texts` holds the reply's messages:
>
> ```text theme={null}
> event: reply_written
> data: {"conversation_id":"conv_143abb3d7b6735f1dabab870","texts":["Target has the LaCroix Lime 12 pack for $6.49. Walmart is still being checked."]}
> ```

## Show the check for a better price

On a best-price ask, a quiet line under the reply tells the shopper that more stores are still being checked. Take the line away once the check is done.

> [tab] SDK
>
> Pass `onBetterPriceCheck`:
>
> ```ts chat.ts theme={null}
> const turn = buy.turn(message, {
>   onBetterPriceCheck: (u) => (u.state === 'checking' ? showQuietLine(shopper.id, u.text) : hideQuietLine(shopper.id)),
> });
> ```
>
> `showQuietLine` and `hideQuietLine` stand for your own code. `checking` carries `text` as Agentcard wrote it. `done` comes once, after `checking`, with `found` set to `true` when a better price was shown.

> [tab] MCP
>
> The `buy` tool sends no check line.

> [tab] API
>
> The stream sends `better_price_check` twice:
>
> ```text theme={null}
> event: better_price_check
> data: {"conversation_id":"conv_143abb3d7b6735f1dabab870","state":"checking","text":"Checking a few more stores for a better price…"}
>
> event: better_price_check
> data: {"conversation_id":"conv_143abb3d7b6735f1dabab870","state":"done","found":true}
> ```

## Show a better price

A store that answers after a best-price reply, at least 5% and at least \$1 below the price the reply led with, sends one `better_price` message. Show its text as one message and its product as a row of its own under it, with the "Best price" tag on that product and off the reply's card.

A shopper who says yes, taps the card, or names the store gets that product in the cart. Agentcard keeps the message in the conversation, so your agent's next turn hears it too.

> [tab] SDK
>
> Pass `onBetterPrice`:
>
> ```ts chat.ts theme={null}
> const turn = buy.turn(message, {
>   onBetterPrice: (u) => (u.withdrawn
>     ? removeBetterRow(u.eventId, u.leadProductId)
>     : showBetterRow(u.eventId, u.text, u.card, u.leadProductId)),
> });
> ```
>
> `removeBetterRow` and `showBetterRow` stand for your own code. `card` is a product card, as `sendLink` hands one, with `bestPrice: true`. `leadProductId` names the reply's card that gives up the tag. `product` carries the same values one by one, with `leadPriceCents`, the price it beats.
>
> The same `eventId` again, with `replaced: true`, means a store found a lower price still: replace the message and the card in place. `withdrawn: true` means the product is gone, such as sold out: take the message and the row away, and give the tag back to `leadProductId`. A tap on the card sends `card.productId` as the shopper's pick.
>
> Without `onBetterPrice`, the message goes where the turn's messages go and the card to `sendLink`, once.

> [tab] MCP
>
> The `buy` tool answers before the late stores do. The message reaches your webhook endpoint as a `purchase.message`.

> [tab] API
>
> The stream sends `purchase_message`, with the same `data` a `purchase.message` webhook carries:
>
> ```json theme={null}
> {
>   "conversation_id": "conv_143abb3d7b6735f1dabab870",
>   "event_id": "evt_3f9a1c07b2d84e6a51c09d2e",
>   "type": "purchase.message",
>   "data": {
>     "kind": "better_price",
>     "user_id": "usr_...",
>     "conversation_id": "conv_143abb3d7b6735f1dabab870",
>     "text": "Found a better price: LaCroix Lime 12 pack at Walmart, $4.98.",
>     "note": "[event: After your last reply, a store answered with a lower price ...]",
>     "facts": {
>       "merchant_name": "Walmart",
>       "better_price": {
>         "product_id": "c3:9f2b7d41",
>         "offer_id": "off_51a8e0c3",
>         "title": "LaCroix Lime 12 pack",
>         "store_name": "Walmart",
>         "store": "walmart.com",
>         "price_cents": 498,
>         "currency": "usd",
>         "url": "https://www.walmart.com/ip/lacroix-lime-12-pack/123456789",
>         "image_url": "https://i5.walmartimages.com/lacroix-lime.jpg",
>         "lead_price_cents": 649
>       }
>     }
>   }
> }
> ```
>
> | Field | What it holds |
> | - | - |
> | `product_id` | The id to send as the shopper's pick when they tap the card. |
> | `offer_id` | The store's offer, or `null` when the product has none. |
> | `title`, `store_name`, `store` | The product's name, the store as the shopper knows it, and its website. |
> | `price_cents`, `currency` | The price, in the currency's smallest unit, and the lowercase currency code. |
> | `url`, `image_url` | The product's page and picture, each an `https` address. Absent when Agentcard has none. |
> | `lead_price_cents` | The price the reply led with, which this product beats. |
>
> A turn sends at most one `better_price` message, under one `event_id`. The same `event_id` again replaces the message and its card. When the product is gone, the stream withdraws it under the same id:
>
> ```json theme={null}
> {
>   "conversation_id": "conv_143abb3d7b6735f1dabab870",
>   "event_id": "evt_3f9a1c07b2d84e6a51c09d2e",
>   "type": "purchase.message",
>   "withdrawn": true
> }
> ```

## Show more hotels or flights

A hotel or flight search can ask more than one source. When the slower one adds results after the reply, Agentcard sends one `more_found` message, such as "I found 2 more hotels.", with up to 12 of them for a row of their own. The reply's cards keep their place and their numbers. A shopper who taps one, or names it, gets it in the cart.

> [tab] SDK
>
> Pass `onMoreFound`:
>
> ```ts chat.ts theme={null}
> const turn = buy.turn(message, {
>   onMoreFound: (u) => (u.withdrawn ? removeRow(u.eventId) : showRow(u.eventId, u.text, u.cards)),
> });
> ```
>
> `removeRow` and `showRow` stand for your own code. Each card in `cards` is a card as `sendLink` hands one, and a tap sends its `productId` as the pick. The same `eventId` again, with `replaced: true`, replaces the row in place. `onFound`'s `more` leaves the row's cards out while the row shows.
>
> Without `onMoreFound`, the message goes where the turn's messages go and each card to `sendLink`, once.

> [tab] MCP
>
> The `buy` tool answers once, with the results it had by the reply.

> [tab] API
>
> Send `stream_purchase_messages: true` and `stream_more_found: true`. The stream then sends `purchase_message` with kind `more_found`:
>
> ```json theme={null}
> {
>   "conversation_id": "conv_8d02f6b1e47a93c5d0e2a118",
>   "event_id": "evt_a71c39e04bd25f8806c1e3d9",
>   "type": "purchase.message",
>   "data": {
>     "kind": "more_found",
>     "user_id": "usr_...",
>     "conversation_id": "conv_8d02f6b1e47a93c5d0e2a118",
>     "text": "I found 2 more hotels.",
>     "note": "[event: After your last reply, more hotels results joined ...]",
>     "facts": {
>       "more_found": {
>         "merchant": "hotels",
>         "cards": [
>           { "id": "htl_4b1e90", "name": "Hotel Zephyr San Francisco", "priceCents": 21900, "currency": "usd" },
>           { "id": "htl_7c22d1", "name": "The Marker San Francisco", "priceCents": 24500, "currency": "usd" }
>         ]
>       }
>     }
>   }
> }
> ```
>
> `cards` lists each result as the turn's `catalog` lists its items. Without both fields, the stream sends the message as a plain text message after the reply, and the results join the products behind "show more". A `more_found` message reaches only the stream: no webhook copy follows it.

## Show a late store behind "show more"

A store that answers after the reply, with nothing below the price the reply led with, sends no message. Its products join the turn's products behind "show more", so a shopper who asks for more sees them first.

> [tab] SDK
>
> The late products reach `onFound`'s `more`. The cards the reply showed stay as they were.

> [tab] MCP
>
> The `buy` tool answers once, with the products it had by the reply.

> [tab] API
>
> The stream sends the `catalog` event again as each late store answers. `items` keeps the cards the reply showed, in their order, and each late card goes to `more`.

## Show each message once

A better price can reach your app twice: live in the stream, and as a `purchase.message` webhook with the same event id. Drop a webhook whose event id the stream already delivered. Agentcard sends no webhook copy when the stream wrote the message and your request sent `stream_purchase_messages: true`. A webhook still follows when the shopper left before the stream wrote it, when the call did not stream, or when the request sent `late_results: 'hold'`.

> [tab] SDK
>
> Pass your buy tools' client to the webhook handler, and it drops a message the stream already showed. `agentcard` is the client your buy tools share, and `postToChat` stands for your own code:
>
> ```ts app/agentcard/webhooks/route.ts theme={null}
> import { agentcardWebhook } from '@agent-cards/commerce-sdk';
> import { agentcard } from '../../../agentcard';
>
> const webhook = agentcardWebhook({
>   secret: process.env.AGENTCARD_WEBHOOK_SECRET ?? '',
>   agentcard,
>   onMessage: (shopperId, message) => postToChat(shopperId, message.text),
> });
>
> export const POST = webhook.fetch;
> ```
>
> `listen` and `agentcard.updates` drop it the same way. A message you read another way can be checked with `agentcard.shownInStream(message.eventId)`.

> [tab] MCP
>
> The `buy` tool reads no stream, so every better price arrives once, at your webhook endpoint.

> [tab] API
>
> Keep the `event_id` of each `purchase_message` the stream delivered, and skip a webhook event whose `id` matches one.

## Answer at the reply instead

A texting app that cannot add a message after its reply asks Agentcard to end the turn at the reply. The stream then waits for nothing after the reply, and a better price found later reaches your webhook endpoint as a `purchase.message`.

> [tab] SDK
>
> ```ts sms.ts theme={null}
> const buy = buyTool({ agentcard, externalUserId: shopper.id, lateResults: 'hold' });
> ```

> [tab] MCP
>
> The `buy` tool always answers at the reply.

> [tab] API
>
> Send `"late_results": "hold"`. [Choose how long a reply waits](https://docs.agentcard.com/opencommerce/guides/choosing-how-long-a-reply-waits.md) covers how `hold` also makes the reply wait for every store first.

## Show a row again later

A shopper who reloads the chat, or whose connection dropped, still sees the rows. Agentcard keeps each late message in the conversation, after the reply.

> [tab] SDK
>
> `agentcard.conversation(conversationId)` returns the reply in `latest_reply.messages`, with a better price's text among them. The SDK does not read rows of more hotels or flights yet: read them from `GET /buy/conversations/:id`, as the API tab shows.

> [tab] MCP
>
> The `buy` tool keeps no rows.

> [tab] API
>
> `GET /buy/conversations/:id` returns `latest_reply.late_rows`, one entry per `more_found` row, under the message's event id:
>
> ```json theme={null}
> {
>   "latest_reply": {
>     "late_rows": [
>       {
>         "event_id": "evt_a71c39e04bd25f8806c1e3d9",
>         "text": "I found 2 more hotels.",
>         "merchant": "hotels",
>         "cards": [
>           { "id": "htl_4b1e90", "name": "Hotel Zephyr San Francisco", "priceCents": 21900, "currency": "usd" }
>         ]
>       }
>     ],
>     "purchase_message_event_ids": ["evt_3f9a1c07b2d84e6a51c09d2e"]
>   }
> }
> ```
>
> `purchase_message_event_ids` names each purchase message among the reply's rows, so an app that already showed one drops it by the same id.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.