Install the SDK
Get sandbox credentials
Open the Agentcard dashboard, go to Commerce SDK, and choose Start implementing. Step 1 shows your sandbox client ID and secret, labeled Sandbox. Save them as environment variables on your server:.env.local
111111.
If you work with a coding agent such as Claude Code, Cursor, or Codex, step 2 of Start implementing gives you instructions to paste into it. The agent installs the SDK, wires the buy tool into your agent, and checks the result in the sandbox.
Give your agent the buy tool
Build oneAgentcardClient when your server starts and keep it. Build one buy tool for each shopper, name the shopper by your own id for them, and keep that tool too. Open one turn for each message the shopper sends, and give the turn to your model in place of the tool.
agentcard.ts
externalUserId. Agentcard refuses a call when two of your shoppers share one id.
Each turn needs a way to deliver links to the shopper. Pass sendLink, and the turn hands it every Vault, approval, store sign-in, product, and store link, so your model never reads or writes one. “Send each link” shows how to render each kind.
The tab for your model API shows the same turn in each shape. Pick the one your agent already uses.
- Anthropic
- Vercel AI SDK
- OpenAI or OpenRouter
chat.ts
deliverLink and yourModel stand for your own code. The model calls buy with the shopper’s words, shows the shopper the cart, and confirms the cart only after the shopper says yes. OpenRouter’s agent SDK takes the tool through toOpenRouterTool(turn, { tool, z }), as the buyTool reference shows.
Pass deliveryAddress to buyTool when you already hold the shopper’s address. The order goes there, and Agentcard never asks the shopper for it. The model can change neither the address nor the shopper.
Check it in the sandbox
Run the scripted walkthrough with your sandbox credentials. It connects a test shopper with the code111111, asks for a restaurant table, picks one, and confirms it, with no model in the loop.
declined with decline_code: sandbox_unavailable, and the run ends with:
examples/anthropic.mjs and examples/openrouter.mjs, which run the buy tool as a chat in your terminal and print the links your app would send. Each guide in this section ends with a check you run in the sandbox.
Hear from Agentcard
Agentcard tells your server about every moment a shopper should hear about, even when the shopper is not chatting: a card saved, an approval about to expire, an order placed, a delivery on its way. The SDK hands you each one as a message withtext ready to post into that shopper’s chat. Pick the way your host runs.
Listen from a long-running server
Calllisten once when your server starts, on the same AgentcardClient your tools use. Your server opens one connection to Agentcard, so you host no public URL and check no signature.
server.ts
onMessage gets each message under the externalUserId you gave the buy tool. onEvent gets every other event Agentcard sends your company, such as order.placed. The listener reconnects after a dropped connection and resumes after the last event it handled. Run one listener for each company and mode. The listen reference lists the options, including a cursor store that resumes a restarted server where the last process stopped.
Take events at a webhook endpoint on serverless
A host that ends each request after a few seconds cannot keep a connection open. MountagentcardWebhook on a route instead. The handler checks each delivery’s signature, skips an event it already handled, and calls onMessage with your own id for the shopper.
app/api/agentcard/route.ts
POST /api/v2/webhook_endpoints, and subscribe it to purchase.message, order.confirmed, and order.updated by name. A catch-all * alone does not turn purchase.message on. The handler answers 200 once onMessage returns and 500 when it throws, and Agentcard then delivers the event again, so a failed post is not lost. In an Express route, mount the same handler with express.raw({ type: 'application/json' }) before any express.json(). The webhook handler reference shows both.
Let your model read what the shopper read
Between turns, Agentcard posts messages such as “Your Visa ending 3132 is saved” into the shopper’s chat. Your model has to read them too, or it answers “ok lets checkout” with “did you save your card?”. Passturn.messages(history) as the model’s messages, as the code above does, and the turn adds each message the history does not hold yet. “Take the next step once the shopper finishes outside the chat” shows how your agent moves on without waiting for the shopper to say “ok”.
The client keeps each message in the process that received it, so a server that runs listen beside your model hands them to the next turn on its own. A webhook endpoint on serverless answers in another process than your chat route, so write each message into the shopper’s history as an assistant message, as the handler above does, and your model reads it from there. Post the message’s own text to the shopper, and save stripLinks(message.text) for the model, so the model never repeats a link that has since expired. The buy tool hands the shopper a current one.
The order events your company receives are documented in Order events: order.placed, order.confirmed, order.failed, and order.updated.
See what your agent can buy
The buy tool covers four kinds of purchase, and the model picks the right one from the shopper’s words.
Agentcard’s Commerce SDK lists the stores. Each guide in this section covers one moment of the shopper’s experience, from the product cards to the approval.
Go live
Switch from sandbox to live in two steps. The code stays the same.- Open Settings, Developers, Credentials in the dashboard, switch to Live, and register your production app the first time. Copy the production client ID and secret into
AGENTCARD_CLIENT_IDandAGENTCARD_CLIENT_SECRET. A webhook endpoint belongs to a mode, so create a live endpoint for the live credentials and use its secret. - Open Commerce SDK, Balance in the dashboard and choose how to pay the fee. Agentcard takes 1% of each live purchase. With prefund, you add money to a balance and each fee comes out of it. With pay as you go, you save a card and each fee is charged to it as the purchase completes. Fees too small to charge wait and are charged together with the next one.
Keep going
Showing products and carting the one the shopper picks
Render product cards and cart the exact product the shopper taps.
Sending each link: approval, Vault and store sign-in
Deliver every link to the shopper while the model never reads one.
Approving a purchase
Send the approval link and place the order only after the shopper says yes.
Commerce SDK reference
Every export, option, and message type.