DEV Community

Cover image for How to Let an AI Agent Buy Things: A Developer's Guide to Agentic Payments
David Stewart
David Stewart

Posted on

How to Let an AI Agent Buy Things: A Developer's Guide to Agentic Payments

So we speak about AI agents as assistants… but how good are they at that really?

When I think of an assistant, my metric of how good it is, is can it complete this task in its entirety. I ask them to do something for me, and to complete it only giving me the results.

And this rings true for research, image generation, generating some text. It's absolutely not true for purchases and payments.

The future that a lot of big companies are currently working on still only has agents doing half the problem, and handing it over to you. But in the true autonomous universe that I'd like to dwell in, that agent hasn't done its job; in fact, it's just given me one — and I don't like work.

So what I'm going to teach you is how to give those agents the ability to complete that last step of the journey and become a true assistant that can actually complete purchases, and can order and schedule things for you in the physical world on your behalf rather than just text behind your screen.

You might have noticed that I see a very… more extreme… universe ahead. One where agents will complete purchases themselves (of course, with the permission of humans — if on their behalf).

So let's dive into how to accomplish that.

How to give my AI agent a wallet?

Now the idea is to not have your agent run up your bank account - at least directly, so naturally… they have to have their own. An agent is a digital being (which is kinda weird to say) so they need to have a digital wallet.

I'll be using a Prudra Wallet utilising the @prudra/payment npm package for this example since they have functionality that allows you to skip all the manual steps involved in completing an x402 purchase with its fetchWithX402() function - and since it take less than a minute to get a wallet.

But you could use any number of digital wallets. You'd just have to learn about x402 handshakes and do it manually. Check it out here.

Making an Agentic Purchase Step by Step:

Once you've funded the digital wallet

1. Step 1: Create AGCX API Key

Once you've funded the account, create an API Key on the Agent Commerce Exchange (AGCX).

You don't need to sign up if you don't want to — you can have an agent create an API key directly through HTTP request, but the API key is like the identity of the agent, and all orders and functionality will be attached to that key. So if you wanna monitor it, and have a UI to see everything, I recommend signing up through the AGCX dashboard.

2. Step 2: Call AGCX Product & Purchase Endpoints

Now with a funded agent wallet account I can call the Agent Commerce Exchange endpoints to see their product catalogue, which is a library of real merchants' products across the web selling things from multiple different sources such as Amazon, Shopify, direct and more.

How to browse for agent purchasable products:

Lets say you lived in the US and was looking for keyboards you could call:

curl "https://api.agcx.org/agent/v1/products?q=keyboard&region=US&sort=price_asc" \
  -H "X-API-Key: $ACE_KEY"
Enter fullscreen mode Exit fullscreen mode

which would show your agent a response like:

{
  "listings": [
    {
      "id": "e69ab382-e174-4d46-b240-48770ca1fac6",
      "agentSku": "KEYC-K8-WIRELESS-MECHANIC",
      "title": "Keychron K8 Wireless Mechanical Keyboard",
      "brand": "Keychron",
      "primaryImageUrl": "https://upload.wikimedia.org/wikipedia/commons/f/fb/Keychron_K8_Non-Backlight_Wireless_Mechanical_Keyboard.jpg",
      "priceMinor": 7900,
      "currency": "USD",
      "availableQuantity": 25,
      "storeId": 6,
      "storeName": "Fuse Store",
      "categoryId": "b1bcde7e-6e40-4af9-9d7c-61171a6ef0b3",
      "categorySlug": "electronics",
      "categoryName": "Electronics",
      "countryCodes": [],
      "ratingAvg": 4.3,
      "ratingCount": 207
    }, 
    ...
  ],
  "total": 20,
  "page": 1,
  "limit": 20,
  "totalPages": 14,
  "nextCursor": null,
  "sort": "price_asc"
}
Enter fullscreen mode Exit fullscreen mode

And when you, but most likely your agent who's searching for you, finds the correct products, presents the options to you, and you've confirmed, they can purchase it directly.

How to complete a agentic purchase of a product:

Now the endpoint for creating an order is gated by an 402 Status endpoint with accepts both x402 or MPP as a payment standard.

curl -X POST https://api.agcx.org/agent/v1/products/KEYC-K8-WIRELESS-MECHANIC/orders \
  -H "X-API-Key: $ACE_KEY" \
  -H "X-PAYMENT: $X402_PAYLOAD" \
  -H "Content-Type: application/json" \
  -d '{
    "quantity": 1,
    "agentWalletAddress": "0x8f…2b",
    "buyerEmail": "agent@buyer.xyz",
    "shippingAddress": {
      "line1": "1 Market St", "city": "San Francisco",
      "postalCode": "94103", "country": "US"
    }
  }'
Enter fullscreen mode Exit fullscreen mode

This method requires you to handle the entire payment flow. Moving of funds from your wallet to the AGCX escrow wallet for that store, and passing the payment credential through the request either as X-PAYMENT (x402) OR PAYMENT-SIGNATURE (MPP).

But I recommend, if being done programatically, using the @prudra/payments fetchWithx402 function which does the entire handshake for you - so no need to worry about handshakes or manually creating payment credential.

npm install @prudra/payments
Enter fullscreen mode Exit fullscreen mode
import { fetchWithX402 } from '@prudra/payments';

const response = await fetchWithX402(
  'https://api.agcx.org/agent/v1/products/KEYC-K8-WIRELESS-MECHANIC/orders',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      quantity: 1,
      agentWalletAddress: '0x8f…2b',
      buyerEmail: 'agent@buyer.xyz',
      shippingAddress: {
        line1: '1 Market St',
        city: 'San Francisco',
        postalCode: '94103',
        country: 'US',
      },
    }),
    walletId: wallet.id,
  }
);
Enter fullscreen mode Exit fullscreen mode

And that's it. I can query the Agent Commerce Exchange for my orders and keep track of it. Getting email updates about my orders just like any other purchase made online.

AGCX.org has their full API reference for other functions such as requesting refunds or getting your order status.

... but... I wanted complete, undeniable automation... my goal was to not touch a thing, so I'd rather have Claude any agent i build make that purchase for me (after giving me giving consent of course).

I'll post a new tutorial showing you how to link up AGCX to claude.

Top comments (0)