導入 Cart API

本指南提供技術 API 參考資料和酬載結構定義,說明如何整合 2026-04-08 版的通用商務通訊協定 (UCP) 購物車 API。

建立端點前,請務必先參閱購物車 API 總覽,瞭解高階概念和必要條件。

建立購物車

這個端點可建立新的購物車工作階段。如要整合,請實作 CreateCart 端點 (POST /carts)。使用者選擇轉移購物車時,Google 會使用明細項目詳細資料呼叫這個端點。系統必須傳回 continue_url,將使用者導向網站上預先填入的購物車。

  • 端點: POST /carts
  • 觸發條件:使用者點選產品的「加入購物車」,並將第一個項目加入商家的購物車。

要求:Google 會傳送要加入購物車的 line_items 陣列。

要求範例:

{
  "line_items": [
    {
      "item": {
        "id": "item_123"
      },
      "quantity": 2
    }
  ]
}

回應:您會傳回已初始化的購物車工作階段,包括明細項目詳細資料、總計和 continue_url。回應中的 continue_url 欄位必須將使用者導回網站上的頁面,讓他們繼續管理購物車。這通常會連結至購物車或結帳頁面,並預先載入 id 識別的階段作業。

回應範例:

{
  "ucp": {
    "version": "2026-04-08",
    "capabilities": {
      "dev.ucp.shopping.cart": [{"version": "2026-04-08"}]
    }
  },
  "id": "cart_abc123",
  "line_items": [
    {
      "id": "li_1",
      "item": {
        "id": "item_123",
        "title": "Red T-Shirt",
        "price": 2500
      },
      "quantity": 2,
      "totals": [
        {"type": "subtotal", "amount": 5000},
        {"type": "total", "amount": 5000}
      ]
    }
  ],
  "currency": "USD",
  "totals": [
    {
      "type": "subtotal",
      "amount": 5000
    },
    {
      "type": "total",
      "amount": 5000,
      "display_text": "Estimated total (taxes calculated at checkout)"
    }
  ],
  // Used for redirecting the user back to the merchant's cart experience from Google surfaces.
  "continue_url": "https://business.example.com/checkout?cart=cart_abc123",
  // Indicate the timestamp at which the cart session will expire and become invalid.
  "expires_at": "2026-01-16T12:00:00Z"
}

處理錯誤

如需錯誤訊息格式的完整指南,以及通訊協定錯誤和商業邏輯錯誤的區別,請參閱「錯誤代碼」。