目录能力

从搜索到加购

用户想找一款蓝色 750 ml 水壶,并把两件加入购物车。下面用 UCP 2026-08-25 的 REST 请求走完这条路径。请求和响应使用模拟商家数据,可以用固定版 Schema 离线核对。

先读取商家 Profile 的 /.well-known/ucp,从 ucp.services["dev.ucp.shopping"] 选择已协商的 rest 端点。确认双方协商了相同版本的 dev.ucp.shopping.catalog.search、dev.ucp.shopping.catalog.lookup 和 dev.ucp.shopping.cart。把下面的路径接到该端点,并发送 Content-Type: application/json。UCP-Agent 使用调用方自己的 Profile URL,例如 profile="https://platform.example/.well-known/ucp"。示例在三步都使用 US 地区上下文,方便比较同一市场的结果。

1. 搜索商品

发送 POST /catalog/search。用 query 表达用户要找的规格。

请求

{
  "query": "blue 750 ml bottle",
  "context": {
    "address_country": "US"
  }
}

响应

{
  "ucp": {
    "version": "2026-08-25",
    "capabilities": {
      "dev.ucp.shopping.catalog.search": [
        {
          "version": "2026-08-25"
        }
      ]
    }
  },
  "products": [
    {
      "id": "trail-bottle",
      "title": "Trail Bottle",
      "description": {
        "plain": "Reusable trail bottle"
      },
      "price_range": {
        "min": {
          "amount": 1800,
          "currency": "USD"
        },
        "max": {
          "amount": 1800,
          "currency": "USD"
        }
      },
      "variants": [
        {
          "id": "trail-bottle-750-blue",
          "title": "Blue · 750 ml",
          "description": {
            "plain": "Blue 750 ml bottle"
          },
          "price": {
            "amount": 1800,
            "currency": "USD"
          },
          "availability": {
            "available": true
          }
        }
      ]
    }
  ]
}

从 products[] 读取商品 ID trail-bottle,从 variants[] 读取用户选中的变体 ID trail-bottle-750-blue。在该币种下,1800 个 USD 最小单位表示 18.00 美元。availability.available 是本次目录返回的可用性信号。

2. 查询选中的变体

发送 POST /catalog/lookup。下面故意同时查一个不存在的红色变体,便于观察部分命中。

请求

{
  "ids": [
    "trail-bottle-750-blue",
    "trail-bottle-750-red"
  ],
  "context": {
    "address_country": "US"
  }
}

响应

{
  "ucp": {
    "version": "2026-08-25",
    "capabilities": {
      "dev.ucp.shopping.catalog.lookup": [
        {
          "version": "2026-08-25"
        }
      ]
    }
  },
  "products": [
    {
      "id": "trail-bottle",
      "title": "Trail Bottle",
      "description": {
        "plain": "Reusable trail bottle"
      },
      "price_range": {
        "min": {
          "amount": 1800,
          "currency": "USD"
        },
        "max": {
          "amount": 1800,
          "currency": "USD"
        }
      },
      "variants": [
        {
          "id": "trail-bottle-750-blue",
          "title": "Blue · 750 ml",
          "description": {
            "plain": "Blue 750 ml bottle"
          },
          "price": {
            "amount": 1800,
            "currency": "USD"
          },
          "availability": {
            "available": true
          },
          "inputs": [
            {
              "id": "trail-bottle-750-blue",
              "match": "exact"
            }
          ]
        }
      ]
    }
  ],
  "messages": [
    {
      "type": "info",
      "code": "not_found",
      "content": "trail-bottle-750-red"
    }
  ]
}

按 variants[].inputs[] 的 id 和 match 关联原请求。exact 表示标识直接命中该变体。featured 表示商家为匹配到的商品选出了代表变体。批量查询可以在 HTTP 200 下只返回已命中的商品,并可附上 not_found 消息。按输入关联判断缺项。需要查看单个商品的详情和可选规格时,沿 Lookup 能力调用 POST /catalog/product,传入单个 id。

3. 创建购物车

发送 POST /carts。将商家确认可用于购物车的蓝色变体 ID 填入 line_items[].item.id,数量设为 2。商品 ID 用于定位商品,sku 能否用作查询标识由商家决定。

请求

{
  "line_items": [
    {
      "item": {
        "id": "trail-bottle-750-blue"
      },
      "quantity": 2
    }
  ],
  "context": {
    "address_country": "US"
  }
}

响应

{
  "ucp": {
    "version": "2026-08-25",
    "capabilities": {
      "dev.ucp.shopping.cart": [
        {
          "version": "2026-08-25"
        }
      ],
      "dev.ucp.shopping.checkout": [
        {
          "version": "2026-08-25"
        }
      ]
    }
  },
  "id": "cart_bottles",
  "line_items": [
    {
      "id": "li_bottle",
      "item": {
        "id": "trail-bottle-750-blue",
        "title": "Blue · 750 ml",
        "price": 1900
      },
      "quantity": 2,
      "totals": [
        {
          "type": "subtotal",
          "amount": 3800
        },
        {
          "type": "total",
          "amount": 3800
        }
      ]
    }
  ],
  "currency": "USD",
  "totals": [
    {
      "type": "subtotal",
      "amount": 3800
    },
    {
      "type": "total",
      "amount": 3800
    }
  ]
}

此成功样本返回 HTTP 201。保存购物车 ID cart_bottles,逐项核对返回的商品、数量、messages[] 和 totals[]。示例中的当前单价为 19.00 美元,两件共 38.00 美元。把变价后的金额交给用户确认后再继续结账。

如果 Cart 返回 ucp.status: error 和 out_of_stock,即使 HTTP 状态为 200,也要先确认是否创建了购物车。全部缺货的示例不会创建购物车。部分商品缺货时,按实际保留的行商品和消息决定是否调整数量或回到目录重新选择。购物车创建与缺货示例

此时创建的购物车还没有支付与收货信息。把它转成结账会话时,按购物车到结账的转换以 cart_id 初始化结账。本地复跑官方花店样例则是另一条独立练习:它自带示例数据,不接收本教程的 cart_bottles,在回环商户上用模拟支付数据跑通从购物车到完成结账的链路,只用于本地演练,不代表生产扣款。

固定版来源