OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  技能包  ›  agentic-commerce:AI 驱动的商品搜索与加密货币支付购物 API

agentic-commerce:AI 驱动的商品搜索与加密货币支付购物 API

 
  migration ·  2026-02-06 10:54:22 · 3 次点击  · 0 条评论  

名称: purch-api
描述: |
基于 AI 的购物 API,支持商品搜索与加密货币结算。适用于以下场景:
- 从 Amazon 和 Shopify 搜索商品
- 构建购物助手或商品推荐功能
- 使用加密货币(Solana 或 Base 链上的 USDC)创建购买订单
- 在应用中集成电商结账功能
- 为购买行为签名并提交区块链交易


Purch 公共 API

基础 URL: https://api.purch.xyz

速率限制

每个 IP 每分钟 60 次请求。每个响应都包含以下头部信息:
- X-RateLimit-Limit: 每个时间窗口的最大请求数
- X-RateLimit-Remaining: 剩余请求数
- X-RateLimit-Reset: 重置剩余秒数

接口端点

GET /search - 结构化商品搜索

使用筛选条件查询商品。

curl "https://api.purch.xyz/search?q=headphones&priceMax=100"

参数:
| 参数 | 类型 | 必填 | 说明 |
|-------|------|----------|-------------|
| q | 字符串 | 是 | 搜索关键词 |
| priceMin | 数字 | 否 | 最低价格 |
| priceMax | 数字 | 否 | 最高价格 |
| brand | 字符串 | 否 | 按品牌筛选 |
| page | 数字 | 否 | 页码(默认:1) |

响应:

{
  "products": [
    {
      "id": "B0CXYZ1234",
      "title": "Sony WH-1000XM5",
      "price": 348.00,
      "currency": "USD",
      "rating": 4.8,
      "reviewCount": 15420,
      "imageUrl": "https://...",
      "productUrl": "https://amazon.com/dp/B0CXYZ1234",
      "source": "amazon"
    }
  ],
  "totalResults": 20,
  "page": 1,
  "hasMore": true
}

POST /shop - AI 购物助手

自然语言商品搜索。从 Amazon 和 Shopify 返回 20 个以上商品。

curl -X POST "https://api.purch.xyz/shop" \
  -H "Content-Type: application/json" \
  -d '{"message": "comfortable running shoes under $100"}'

请求体:

{
  "message": "comfortable running shoes under $100",
  "context": {
    "priceRange": { "min": 0, "max": 100 },
    "preferences": ["comfortable", "breathable"]
  }
}

响应:

{
  "reply": "Found 22 running shoes. Top pick: Nike Revolution 6 at $65...",
  "products": [
    {
      "asin": "B09XYZ123",
      "title": "Nike Revolution 6",
      "price": 65.00,
      "currency": "USD",
      "rating": 4.5,
      "reviewCount": 8420,
      "imageUrl": "https://...",
      "productUrl": "https://amazon.com/dp/B09XYZ123",
      "source": "amazon"
    },
    {
      "asin": "gid://shopify/p/abc123",
      "title": "Allbirds Tree Runners",
      "price": 98.00,
      "source": "shopify",
      "productUrl": "https://allbirds.com/products/tree-runners",
      "vendor": "Allbirds"
    }
  ]
}

POST /buy - 创建购买订单

创建订单并获取待签名的交易。区块链网络根据钱包地址格式自动识别:
- Solana 钱包 (base58): 7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU
- Base/EVM 钱包 (0x): 0x1234567890abcdef1234567890abcdef12345678

Amazon 商品 (Solana):

curl -X POST "https://api.purch.xyz/buy" \
  -H "Content-Type: application/json" \
  -d '{
    "asin": "B0CXYZ1234",
    "email": "buyer@example.com",
    "walletAddress": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
    "shippingAddress": {
      "name": "John Doe",
      "line1": "123 Main St",
      "line2": "Apt 4B",
      "city": "New York",
      "state": "NY",
      "postalCode": "10001",
      "country": "US",
      "phone": "+1-555-123-4567"
    }
  }'

Amazon 商品 (Base):

curl -X POST "https://api.purch.xyz/buy" \
  -H "Content-Type: application/json" \
  -d '{
    "asin": "B0CXYZ1234",
    "email": "buyer@example.com",
    "walletAddress": "0x1234567890abcdef1234567890abcdef12345678",
    "shippingAddress": {
      "name": "John Doe",
      "line1": "123 Main St",
      "city": "New York",
      "state": "NY",
      "postalCode": "10001",
      "country": "US"
    }
  }'

Shopify 商品 - 需同时提供 productUrlvariantId

curl -X POST "https://api.purch.xyz/buy" \
  -H "Content-Type: application/json" \
  -d '{
    "productUrl": "https://store.com/products/item-name",
    "variantId": "41913945718867",
    "email": "buyer@example.com",
    "walletAddress": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
    "shippingAddress": {
      "name": "John Doe",
      "line1": "123 Main St",
      "city": "New York",
      "state": "NY",
      "postalCode": "10001",
      "country": "US"
    }
  }'

响应:

{
  "orderId": "550e8400-e29b-41d4-a716-446655440000",
  "status": "awaiting-payment",
  "serializedTransaction": "NwbtPCP62oXk5fmSrgT...",
  "product": {
    "title": "Sony WH-1000XM5",
    "imageUrl": "https://...",
    "price": { "amount": "348.00", "currency": "usdc" }
  },
  "totalPrice": { "amount": "348.00", "currency": "usdc" },
  "checkoutUrl": "https://www.crossmint.com/checkout/550e8400..."
}

CLI 脚本

本技能包含适用于所有端点的即用型 CLI 脚本,提供 Python 和 TypeScript/Bun 版本。

依赖项 (Solana):

# Python
pip install solana solders base58

# TypeScript/Bun
bun add @solana/web3.js bs58

依赖项 (Base/EVM):

# TypeScript/Bun
bun add viem

搜索商品

# Python
python scripts/search.py "wireless headphones" --price-max 100
python scripts/search.py "running shoes" --brand Nike --page 2

# TypeScript
bun run scripts/search.ts "wireless headphones" --price-max 100

AI 购物助手

# Python
python scripts/shop.py "comfortable running shoes under $100"

# TypeScript
bun run scripts/shop.ts "wireless headphones with good noise cancellation"

创建订单(不签名)

# 通过 ASIN 购买 Amazon 商品
python scripts/buy.py --asin B0CXYZ1234 --email buyer@example.com \
  --wallet 7xKXtg... --address "John Doe,123 Main St,New York,NY,10001,US"

# 购买 Shopify 商品
bun run scripts/buy.ts --url "https://store.com/products/item" --variant 41913945718867 \
  --email buyer@example.com --wallet 7xKXtg... --address "John Doe,123 Main St,NYC,NY,10001,US"

地址格式:姓名,地址行1,城市,州/省,邮政编码,国家[,地址行2][,电话]

创建订单并签名交易 (Solana)

端到端购买流程 - 创建订单并签名/提交 Solana 交易:

# Python
python scripts/buy_and_sign.py --asin B0CXYZ1234 --email buyer@example.com \
  --wallet 7xKXtg... --private-key 5abc123... \
  --address "John Doe,123 Main St,New York,NY,10001,US"

# TypeScript
bun run scripts/buy_and_sign.ts --url "https://store.com/products/item" --variant 41913945718867 \
  --email buyer@example.com --wallet 7xKXtg... --private-key 5abc123... \
  --address "John Doe,123 Main St,NYC,NY,10001,US"

创建订单并签名交易 (Base)

使用 Base/EVM 钱包的端到端购买流程:

bun run scripts/buy_and_sign_base.ts --asin B0CXYZ1234 --email buyer@example.com \
  --wallet 0x1234567890abcdef1234567890abcdef12345678 \
  --private-key 0xabc123... \
  --address "John Doe,123 Main St,New York,NY,10001,US"

仅签名交易 (Solana)

如果您已从 /buy 端点获取了 serializedTransaction

# Python
python scripts/sign_transaction.py "<serialized_tx>" "<private_key_base58>"

# TypeScript
bun run scripts/sign_transaction.ts "<serialized_tx>" "<private_key_base58>"

仅签名交易 (Base)

bun run scripts/sign_transaction_base.ts "<serialized_tx_hex>" "<private_key_hex>"

输出 (Solana):

✅ 交易成功!
   签名: 5UfgJ3vN...
   浏览器: https://solscan.io/tx/5UfgJ3vN...

输出 (Base):

✅ 交易成功!
   哈希: 0x1234...
   浏览器: https://basescan.org/tx/0x1234...

编程方式签名交易

用于不使用捆绑脚本的自定义集成:

JavaScript/TypeScript

import { Connection, Transaction, clusterApiUrl } from "@solana/web3.js";
import bs58 from "bs58";

async function signAndSendTransaction(
  serializedTransaction: string,
  wallet: { signTransaction: (tx: Transaction) => Promise<Transaction> }
) {
  // 解码 base58 交易
  const transactionBuffer = bs58.decode(serializedTransaction);
  const transaction = Transaction.from(transactionBuffer);

  // 使用用户钱包签名(例如 Phantom、Solflare)
  const signedTransaction = await wallet.signTransaction(transaction);

  // 发送到 Solana 网络
  const connection = new Connection(clusterApiUrl("mainnet-beta"));
  const signature = await connection.sendRawTransaction(
    signedTransaction.serialize()
  );

  // 确认交易
  await connection.confirmTransaction(signature, "confirmed");

  return signature;
}

React with Wallet Adapter

import { useWallet, useConnection } from "@solana/wallet-adapter-react";
import { Transaction } from "@solana/web3.js";
import bs58 from "bs58";

function CheckoutButton({ serializedTransaction }: { serializedTransaction: string }) {
  const { signTransaction, publicKey } = useWallet();
  const { connection } = useConnection();

  const handlePayment = async () => {
    if (!signTransaction || !publicKey) {
      throw new Error("钱包未连接");
    }

    // 解码并签名
    const tx = Transaction.from(bs58.decode(serializedTransaction));
    const signed = await signTransaction(tx);

    // 发送并确认
    const sig = await connection.sendRawTransaction(signed.serialize());
    await connection.confirmTransaction(sig, "confirmed");

    console.log("支付完成:", sig);
  };

  return <button onClick={handlePayment}>使用 USDC 支付</button>;
}

Python (使用 solana-py)

import base58
from solana.rpc.api import Client
from solana.transaction import Transaction
from solders.keypair import Keypair

def sign_and_send(serialized_tx: str, keypair: Keypair) -> str:
    # 解码 base58 交易
    tx_bytes = base58.b58decode(serialized_tx)
    transaction = Transaction.deserialize(tx_bytes)

    # 签名
    transaction.sign(keypair)

    # 发送
    client = Client("https://api.mainnet-beta.solana.com")
    result = client.send_transaction(transaction)

    return result.value  # 交易签名

签名 Base/EVM 交易

对于 Base 链订单,serializedTransaction 是一个需要 EVM 钱包签名的 EVM 交易。

TypeScript with viem

import { createWalletClient, http, parseTransaction } from "viem";
import { base } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";

async function signAndSendBaseTransaction(
  serializedTransaction: string,
  privateKey: `0x${string}`
) {
  const account = privateKeyToAccount(privateKey);

  const client = createWalletClient({
    account,
    chain: base,
    transport: http(),
  });

  // 解析并发送序列化交易
  const tx = parseTransaction(serializedTransaction as `0x${string}`);
  const hash = await client.sendTransaction(tx);

  console.log("交易哈希:", hash);
  console.log("浏览器:https://basescan.org/tx/" + hash);

  return hash;
}

React with wagmi

import { useSendTransaction } from "wagmi";
import { parseTransaction } from "viem";

function CheckoutButton({ serializedTransaction }: { serializedTransaction: string }) {
  const { sendTransaction } = useSendTransaction();

  const handlePayment = async () => {
    const tx = parseTransaction(serializedTransaction as `0x${string}`);
    sendTransaction(tx);
  };

  return <button onClick={handlePayment}>在 Base 上使用 USDC 支付</button>;
}

完整结账流程 (Solana)

// 1. 搜索商品
const searchResponse = await fetch("https://api.purch.xyz/shop", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ message: "wireless headphones under $100" })
});
const { products, reply } = await searchResponse.json();

// 2. 用户选择商品,创建订单(Solana 钱包)
const orderResponse = await fetch("https://api.purch.xyz/buy", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    asin: products[0].asin,
    email: "buyer@example.com",
    walletAddress: wallet.publicKey.toBase58(), // Solana 地址
    shippingAddress: {
      name: "John Doe",
      line1: "123 Main St",
      city: "New York",
      state: "NY",
      postalCode: "10001",
      country: "US"
    }
  })
});
const { orderId, serializedTransaction, checkoutUrl } = await orderResponse.json();

// 3. 签名并发送 Solana 交易
const tx = Transaction.from(bs58.decode(serializedTransaction));
const signed = await wallet.signTransaction(tx);
const signature = await connection.sendRawTransaction(signed.serialize());
await connection.confirmTransaction(signature, "confirmed");

console.log(`订单 ${orderId} 已支付。交易:${signature}`);

完整结账流程 (Base)

import { parseTransaction } from "viem";

// 1. 搜索商品
const searchResponse = await fetch("https://api.purch.xyz/shop", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ message: "wireless headphones under $100" })
});
const { products } = await searchResponse.json();

// 2. 用户选择商品,创建订单(EVM 钱包)
const orderResponse = await fetch("https://api.purch.xyz/buy", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    asin: products[0].asin,
    email: "buyer@example.com",
    walletAddress: "0x1234...", // Base/EVM 地址 - 链自动识别
    shippingAddress: {
      name: "John Doe",
      line1: "123 Main St",
      city: "New York",
      state: "NY",
      postalCode: "10001",
      country: "US"
    }
  })
});
const { orderId, serializedTransaction } = await orderResponse.json();

// 3. 签名并发送 Base 交易
const tx = parseTransaction(serializedTransaction as `0x${string}`);
const hash = await walletClient.sendTransaction(tx);

console.log(`订单 ${orderId} 已支付。交易:${hash}`);

备选方案:浏览器结账

如果钱包签名失败或不可用,可重定向到 checkoutUrl 进行基于浏览器的支付:

```

3 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor