Before you start, you need a user who has added a card to the Vault, and the
user_id you received when they added it. See Adding a card.
Install
anchorbrowser reads your Anchor API key from ANCHORBROWSER_API_KEY. The samples read your Agentcard client id and secret from AGENTCARD_CLIENT_ID and AGENTCARD_CLIENT_SECRET. Create them in the dashboard, as step 1 of the Vault Quickstart shows.
Shop with your own agent
Create an Anchor session, and attach the SDK to its page before your agent reaches the payment form.syncRegistry() loads the list of payment processors the SDK can pause. Call it once after you create the client.
Have your agent type any of Stripe’s published test card numbers as the placeholder. The SDK pauses the payment and calls onApprovalUrl with a link to the user’s approval screen. Send the link from your own app, such as a push notification or an in-app message, and keep it out of your logs: the link opens the approval screen for this payment, and only the user should see it.
If your agent opens the checkout in a new tab, attach to that page too, as the next section shows.
Fix an attach error
Anchor runs its agent and its blockers as browser extensions in the session.@agent-cards/checkout up to 0.19.0, the SDK’s older name, refuses a page next to them, and attachToPlaywright throws:
@agent-cards/sdk 0.20.0 or later instead of creating the context the error asks for. A new context has none of the session’s saved logins.
Shop with Anchor’s agent
Refuse card requests from any tab the SDK has not attached to, attach the SDK to every page in the session, then passsessionId to agentTask, so that Anchor’s agent shops in that same session. The sample reuses vault and the imports from the previous section.
A task without sessionId runs in a new browser where the SDK is not attached. The store receives the placeholder card, and the user’s card never pays.
PLACEHOLDER_CARD_NUMBER to any of Stripe’s published test card numbers. Put the placeholder card in the task, and tell the agent that the payment waits for the user after it clicks Pay, so that the agent does not click Pay again while the user approves. Keep the approval link out of the task, because Anchor’s agent reads the task.
The task returns the agent’s own summary of what it did. This run against the demo store printed:
Confirm the order
The agent’s summary is not proof that the merchant took the order. From here the purchase runs like any Vault purchase: confirm the order with the merchant before you tell the user it is done, as step 4 of Completing a purchase shows.End the session
The session keeps running after you close the Playwright connection, until Anchor times it out. End it in afinally block once you have confirmed the order, so that a failed purchase does not leave it running either. Log a failed cleanup instead of throwing it, so that a paid order never reads as a failure that a retry would pay again. The second sample does both, and the first follows the same shape: