如何使用 Shoply AI Search API

Shoply AI Search API connecting backend code to storefront product search results

您可以從自己的後端查詢 Shoply AI Search,而不需載入 Shoply 的 storefront 小工具,也不需要透過您的 storefront 轉送請求。此 API 適用於 Shopify 商店,以及使用 SiteChat(Shoply 提供給任何網站的 AI 助理)的其他平台網站。

標準 Search API 在所有 Shoply 方案中都可使用,包括 Forever Free,且同時適用於 Shopify 商店與 SiteChat 網站。每個方案可建立索引的商品數量不同;請參閱我們的價格頁面。透過 API 進行的搜尋不會出現在您的 Shoply 分析資料中。

如果您預期 API 流量很高或穩定,或您需要服務等級協議、自訂驗證、保證吞吐量,或自訂資料來源,請聯絡我們洽詢 Enterprise 方案。

開始之前

每個請求都會使用 store_key 指定要搜尋的商品目錄。您應使用的值取決於您的平台。

Shopify 商店

請在您要搜尋其商品目錄的 Shopify 商店安裝 Shoply AI,並等待初始商品索引完成。您的 store_key 是商店永久的 myshopify.com 網域,即使顧客實際造訪的是自訂網域也一樣:

text
your-store.myshopify.com

其他平台上的網站

註冊 SiteChat ,並將您的商品目錄提供給 Shoply。Search API 只會搜尋該商品目錄;SiteChat 一般爬取的網頁內容會成為聊天知識,而不是可搜尋的商品。Shoply 可透過以下任一方式取得商品目錄:

  • 在 SiteChat 管理後台的 Product Catalog 頁面,一次新增一項商品,或匯入 CSV/Excel 檔案。
  • 使用 SiteChat 商品上傳 API 從您自己的系統上傳商品。
  • 如果您的網站是 Magento 商店,Shoply 在爬取網站時會讀取其公開商品目錄;請參閱如何允許 Shoply crawler。

當商品被新增或變更後,Shoply 會先重新建立商店的商品索引,之後才能被搜尋到。您的 store_key 是您的 SiteChat store key,也就是安裝在您網站上的 SiteChat 程式碼中的 store_key 值。

兩種平台皆適用

此 API 可從任何後端環境呼叫。標準商品搜尋端點不需要另外的 API key,因為它回傳的是與 Shoply 在 storefront 搜尋體驗中可顯示的相同公開商品目錄資訊。

端點

Search API 有兩個端點。大多數整合只需要第一個。

Endpoint功能
POST https://api.shoplyai.ai/product_query_v2回傳排序後的商品、結果數量、排序選項,以及篩選條件(在搜尋頁模式下,篩選條件有時會分開傳送)
POST https://api.shoplyai.ai/search_display_filters回傳某次搜尋頁結果中未一併帶回的篩選條件;請參閱分開載入篩選條件

兩個端點都接受 Content-Type: application/json 的 JSON request body。

每個請求的 URL 都要加上 store_key,並在 body 中傳送相同的值:

text
https://api.shoplyai.ai/product_query_v2?store_key=your-store.myshopify.com

Shoply 可能會依據此 URL 值來路由請求,因此即使 body 中也有相同欄位,仍請務必帶上。

Request Body

欄位必填說明
store_key是您的 store key:Shopify 商店永久的 myshopify.com 網域,或您的 SiteChat store key
query是一個 JSON 編碼字串,內容至少需包含 keywords 欄位
start否用於分頁的零起始結果位移;預設為 0
limit否最多回傳的商品數量,範圍為 1 到 200;預設為 4
question_context否一個 JSON 物件,用於選擇搜尋頁模式、套用篩選、排序結果或設定語言;請參閱搜尋頁模式、篩選與排序

query 是 request body 裡的一個 JSON 字串,不是巢狀 JSON 物件。例如,搜尋詞 waterproof hiking boots 會這樣傳送:

json
"{\"keywords\":\"waterproof hiking boots\"}"

query 也可以包含 description,用來傳送顧客以自然語言描述的較長需求,例如 {"keywords":"boots","description":"waterproof, for wide feet"}。

question_context 請以一般 JSON 物件傳送,而不是字串。

cURL 範例

請在兩個位置都將 your-store.myshopify.com 替換成您的 store key。範例使用的是 Shopify store key;SiteChat store key 的使用方式相同。

bash
curl --request POST "https://api.shoplyai.ai/product_query_v2?store_key=your-store.myshopify.com" \ --header "Content-Type: application/json" \ --data '{ "store_key": "your-store.myshopify.com", "query": "{\"keywords\":\"waterproof hiking boots\"}", "start": 0, "limit": 8 }'

JavaScript 範例

此範例可在 Node.js 18 或更新版本中,以 ES module 執行(例如 .mjs 檔案)。在其他環境中,請將其包在 async 函式內。

js
const storeKey = "your-store.myshopify.com"; // Build the inner query separately because the API expects it as a JSON-encoded string. const searchQuery = JSON.stringify({ keywords: "waterproof hiking boots" }); const url = `https://api.shoplyai.ai/product_query_v2?store_key=${encodeURIComponent(storeKey)}`; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ store_key: storeKey, query: searchQuery, start: 0, limit: 8 }) }); if (!response.ok) { throw new Error(`Shoply search failed with status ${response.status}`); } const results = await response.json(); console.log(results.docs);

Python 範例

python
import json import requests store_key = "your-store.myshopify.com" # The query value is a JSON-encoded string, not a nested object. payload = { "store_key": store_key, "query": json.dumps({"keywords": "waterproof hiking boots"}), "start": 0, "limit": 8, } response = requests.post( "https://api.shoplyai.ai/product_query_v2", params={"store_key": store_key}, json=payload, timeout=30, ) response.raise_for_status() results = response.json() print(results["docs"])

回應

成功的請求會回傳類似以下的回應,這裡已簡化為只顯示單一商品:

json
{ "num_results": 34, "docs": [ { "product_id": "gid://shopify/Product/1234567890", "url": "https://your-store.example/products/example-product", "product_name": "Example Product", "brand": "Example Brand", "current_price": "129.00", "currency": "USD", "main_image": "https://cdn.shopify.com/example-product.jpg", "selected_variant_id": "gid://shopify/ProductVariant/1234567891" } ], "display_filters": [ { "key": "current_price", "name": "Price", "selections": [ { "value": "range_0_100", "name": "$0.00 - $99.99", "count": 12 }, { "value": "range_100_200", "name": "$100.00 - $199.99", "count": 15 } ] } ], "display_filters_pending": false, "num_results_final": true, "sort_by": "relevance", "sort_options": { "price": true, "rating": false } }
  • num_results 是 Shoply 排序後的相符商品數量,上限為 200。當套用篩選條件時,它只計算符合條件的商品。
  • docs 包含所請求頁面的排序後商品。價格、幣別與商品連結皆以商店的預設 market 為準。
  • display_filters 包含可用的篩選條件,例如價格、品牌、顏色或其他商品目錄特定屬性。每個篩選條件都有一個 key、顯示用的 name,以及 selections 清單。每個 selection 都有套用篩選時要回傳的 value、顯示用的 name,以及通常會有的商品 count。若商家隱藏了篩選條件,或搜尋結果只有一項商品或沒有商品,該清單會是空的。
  • num_results_final 在 AI 仍在檢查較低排序商品的相關性時會是 false,這種情況大多發生在搜尋頁模式。在它變成 true 之前,num_results 都只是暫定值:檢查完成後,它可能增加也可能減少。Shoply 自家的搜尋頁面會以加號顯示這種數量,例如「34+」。
  • display_filters_pending 只會在搜尋頁模式中為 true,表示商品已先送出,但篩選條件尚未準備完成。此時 display_filters 會是空的;請參閱分開載入篩選條件。
  • sort_by 與 sort_options 說明排序資訊;請參閱排序。

若要取得暫定結果的最終數量,請在大約兩秒後重送相同請求。重送時可將 limit 設為 1。

商品 ID 取決於平台。Shopify 商品使用 Shopify ID,例如 gid://shopify/Product/1234567890。SiteChat 商品使用由 Shoply 指派的 ID,因此請將 product_id 視為不透明字串處理。

商品物件可能會根據商店的商品目錄、變體、定價與 Shoply 設定而包含其他欄位。篩選條件也可能包含額外欄位,例如價格區間的 min 與 max。用戶端應使用所需欄位,並可安全忽略不熟悉的欄位。

分頁

使用 start 與 limit 來請求更多結果。例如,當您已使用 start: 0 與 limit: 8 請求了八項商品後,可用以下方式請求下一頁:

json
{ "start": 8, "limit": 8 }

當 docs 為空時即可停止。您也可以在 num_results_final 為 true 且 start + docs.length 已達到 num_results 時停止。超過第 200 項商品之後的頁面一定是空的。

當 num_results_final 為 false 時,不要只依據數量就停止,因為它仍可能變動。後面的頁面也可能重複先前頁面中的商品,或跳過某些商品,因此請排除您已經顯示過之 product_id 的商品。

搜尋頁模式、篩選與排序

若不帶 question_context,product_query_v2 會回傳一份精簡的最佳匹配清單,類似 Shoply 聊天中的行為。若要取得與 Shoply AI Search 頁面相同的結果,請加入 question_context,並將 ui_type 設為 search_app:

json
{ "store_key": "your-store.myshopify.com", "query": "{\"keywords\":\"snowboard\"}", "start": 0, "limit": 24, "question_context": { "ui_type": "search_app" } }

搜尋頁模式會回傳與 Shoply AI Search 頁面相同的較長結果清單。它也會套用商家的搜尋頁設定,例如隱藏已售罄商品。若您要建立自訂搜尋結果頁,請使用此模式。

question_context 接受以下欄位:

欄位說明
ui_type設為 search_app 以取得搜尋頁結果;省略則回傳預設的精簡清單
selected_filters要套用的篩選條件,格式為一個物件,將篩選 key 對應到 selection value 清單
sort_byprice_asc、price_desc 或 rating;省略則依相關性排序
language_code顧客的語言,例如 fr。Shoply 會用它來比對關鍵字並翻譯篩選名稱。若省略,則使用商店預設語言。

篩選

若要套用篩選條件,請從 display_filters 複製該篩選的 key 與 selection 的 value。例如,以下請求只保留價格低於 100 美元的商品:

json
{ "store_key": "your-store.myshopify.com", "query": "{\"keywords\":\"snowboard\"}", "start": 0, "limit": 24, "question_context": { "ui_type": "search_app", "selected_filters": { "current_price": ["range_0_100"] } } }

排序

回應中的 sort_options 欄位會告訴您此商店對該次搜尋可提供哪些排序方式,例如 {"price": true, "rating": false}。若商店無法提供某種排序,或 sort_by 值無法辨識,則會回退為依相關性排序。回應中的 sort_by 欄位會指出實際套用的排序方式。

分開載入篩選條件

由於篩選條件需要額外的 AI 步驟,因此建立它們所需的時間比商品清單更長。對於搜尋頁模式中的某些短搜尋,Shoply 會先回傳商品,再於背景建立篩選條件。

Shoply 會針對每次搜尋個別決定是否這樣做,因此請務必檢查 display_filters_pending,不要自行預測。只有在以下條件都成立時才可能發生:

  • question_context.ui_type 為 search_app。
  • 搜尋詞為一到兩個單字,沒有 description,且不是商品代碼或型號代碼。
  • 前 18 筆結果中至少有 12 筆已包含所搜尋的字詞。

當這種情況發生時,product_query_v2 的回應會包含 display_filters_pending: true 與空的 display_filters 清單。請先立即顯示商品,然後將以下欄位 POST 到 search_display_filters:

欄位必填說明
store_key是您傳送至 product_query_v2 的相同 store_key
query是您傳送至 product_query_v2 的相同 query 字串
question_context是您傳送至 product_query_v2 的相同 question_context 物件

Shoply 會將每次搜尋的完整結果保存最多一天。此端點會根據這些欄位找到已保存的搜尋,並從中讀取篩選條件,因此請傳送與 product_query_v2 相同的值。start 與 limit 並非必要,因為篩選條件涵蓋的是完整結果清單,而不是單一頁面。

回應如下所示:

json
{ "found": true, "display_filters": [ { "key": "current_price", "name": "Price", "selections": [ { "value": "range_0_100", "name": "$0.00 - $99.99", "count": 12 } ] } ] }
  • 如果篩選條件仍在建立中,端點最多會等待 5 秒後再回應。
  • 若等待後 display_filters 仍然是空的,請直接顯示商品而不顯示篩選條件。空清單也可能代表商家隱藏了篩選條件,或搜尋結果商品數太少。不要只是為了取得篩選條件而重新執行搜尋。
  • 當 Shoply 找不到對應該 store_key、query 與 question_context 的已保存搜尋時,found 會是 false。此端點本身不會執行搜尋,因此請先呼叫 product_query_v2。

您不需要等篩選條件載入完成後,才套用其中一個條件。若顧客選擇了某個篩選,照常將它放在 selected_filters 中傳送給 product_query_v2 即可。Shoply 在套用該選項前,會先等待自己的篩選資料準備完成。

js
const API_BASE = "https://api.shoplyai.ai"; // Both endpoints take store_key in the URL as well as in the body. async function postToShoply(endpoint, body) { const url = `${API_BASE}/${endpoint}?store_key=${encodeURIComponent(body.store_key)}`; const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); if (!response.ok) { throw new Error(`Shoply ${endpoint} failed with status ${response.status}`); } return response.json(); } // Returns the products at once, plus a promise for the filters, so the page can show // products before the filters arrive. async function searchWithFilters(storeKey, keywords) { const searchRequest = { store_key: storeKey, query: JSON.stringify({ keywords }), question_context: { ui_type: "search_app" } }; const results = await postToShoply("product_query_v2", { ...searchRequest, start: 0, limit: 24 }); const filters = results.display_filters_pending // Same store_key, query, and question_context, so Shoply can find the saved search. ? postToShoply("search_display_filters", searchRequest) .then(answer => (answer.found ? answer.display_filters : [])) : Promise.resolve(results.display_filters); return { results, filters }; }

未帶 question_context 的請求,或帶有非 search_app 的 ui_type 的請求,總是會在 product_query_v2 回應中直接回傳篩選條件。它們永遠不會設定 display_filters_pending。

錯誤

Status發生情況處理方式
400body 不是有效 JSON、缺少 store_key 或其為空白、欄位型別錯誤、query 不是 JSON 編碼物件、question_context 不是物件、start 為負數,或 limit 不在 1 到 200 範圍內修正請求。回應中的 detail 欄位會指出問題。
409Shoply 沒有此 store_key 的搜尋索引:可能是 key 拼錯、傳送了 Shopify 商店的自訂網域而非其 myshopify.com 網域、商店尚未設定 Shoply AI 或 SiteChat,或首次索引仍在建立中請先檢查 store_key。若它正確且商店是最近才設定完成,請幾分鐘後再試。

整合注意事項

  • 若您想集中處理快取、記錄、重試或存取控制,請將呼叫保留在您的後端。
  • Shopify 商店必須持續與 Shoply AI 保持連線,商品索引才能維持最新。SiteChat 網站的結果則來自其商品目錄中的商品,因此請保持該目錄為最新狀態。
  • 在搜尋頁模式中,請處理 display_filters_pending,這樣短搜尋仍能顯示篩選條件。
  • 在所有模式中,都請將 num_results_final: false 視為暫定數量。
  • 如果您的自訂 storefront 也需要對話功能,請參閱如何將聊天功能加入 headless Shopify storefront。

如果您需要協助確認 store key,或規劃正式環境整合,請聯絡 Shoply AI。