CSS API 最佳做法
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文說明使用 CSS API 的一些最佳做法。您不一定要按照本頁的建議使用 API,但這些建議有助於釐清部分預期用途。
設定環境
如要設定開發環境,請按照快速入門說明文件中的步驟操作。
- 在 Google Cloud Console 中產生使用者和權限 JSON 檔案
- 在 Google Cloud Console 中啟用 CSS API
- 將該使用者新增至 CSS 帳戶 (CSS 群組或 CSS 網域),並授予管理員權限
- 請確認您使用的 OAuth 範圍是否正確:
https://www.googleapis.com/auth/content
現在,大多數程式設計語言的標準存放區都提供用戶端程式庫。如需完整清單,請前往用戶端程式庫頁面。
使用正確的 ID
請使用正確的 ID 和 API 端點:
- CSS API (
css.googleapis.com
):與 CSS 產品互動時 (例如accounts/{cssDomainId}/cssProductInputs:insert
)。
- Merchant API (
merchantapi.googleapis.com
):使用 Merchant API 處理標準商家產品。
如果混用,就會發生錯誤。詳情請參閱 CSS API 總覽。
入門好方法
建議您使用下列方法進行測試:
ListChildAccounts
ListChildAccounts
是唯讀呼叫,會列出所有 CSS 網域 (如果呼叫的對象是 CSS 群組) 或商家 (如果呼叫的對象是 CSS 網域)。因此,這是測試所有設定是否正確的好方法。
插入/列出/更新/刪除產品
確認 API 本身運作正常後,請嘗試新增產品。請務必使用您記得的 raw_provided_id
。
CSS API 專為平行呼叫而設計。您會發現單一作業的效能可能較慢,但平行呼叫相同作業多次時,效能會快上許多。如要充分運用這項功能,建議使用程式設計語言的非同步功能。
以下是部分程式設計語言的範例:
找出並使用程式設計語言的 Async 功能,同時插入多項產品。您不必擔心系統負載過重,因為這正是配額限制的用途。
詳情請參閱成效頁面。
驗證酬載
為避免發生常見錯誤,請確認 JSON 酬載的格式正確無誤:
- 參閱官方文件:請務必參閱最新的 CSS API 參考資料,瞭解欄位定義、列舉、資料類型和酬載結構。
- 查看酬載範例:比對您的酬載與提供的程式碼範例,找出差異。
- 資料類型:請務必使用文件指定的正確資料類型 (例如字串、物件、陣列)。
- 逐步測試:先從最少的有效酬載開始,確認基本連線,然後逐步新增更多屬性。
更新產品
產品上傳後,除非更新、刪除或過期,否則會一直保留在系統中。
- 如要更新完整產品,請使用最初使用的
raw_provided_id
再次傳送 InsertCssProductInput
要求。目前,即使只有部分屬性 (可能只有價格/供應情形) 變更,你仍須傳送完整的產品資料。
- 您可以使用 PATCH 方法
UpdateCssProductInput
更新產品的部分內容,方法是指定產品名稱,以及包含要更新產品資料的 JSON 主體。與需要提供所有適用欄位的 InsertCssProductInput
不同,UpdateCssProductInput
只需要您指定要變更的欄位。
- 您可以呼叫
DeleteCssProductInput
並使用相同的 raw_provided_id
刪除產品。
- 產品會在上次更新後約一個月自動過期。
連續運作模式
連續作業模式可能如下所示:
- 使用自己的內部 ID 做為
raw_provided_id
。
- 定期重新上傳所有產品,例如每週一次。這樣才能確保有效產品不會過期。
- 從商家取得變更後的資料後,請立即更新個別產品。
- 如果無法立即對變更做出反應,請經常 (可能每小時) 找出所有變更的產品,然後只重新上傳這些產品。
- 如果產品已停售,你可以使用刪除呼叫或將可用的優惠數量設為 0。
- 請勿頻繁傳送未變更的產品。這些呼叫會計入您的 API 配額。每週更新一次即可。
選取主打商品
主打商品不一定要是網站上最熱門或最便宜的商品,但必須醒目顯示。如果頂端廣告快速變動,您可以使用這項功能選取較穩定的廣告。
不時重新檢查這份文件
我們已收到有關如何改善這項 API 的意見回饋,目前正努力推出部分改善項目。如有可簡化 CSS API 用法的新功能,我們會更新這個頁面。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-13 (世界標準時間)。
[null,null,["上次更新時間:2025-08-13 (世界標準時間)。"],[[["\u003cp\u003eThis document outlines best practices for utilizing the CSS API, including setup, testing methods, performance enhancement, product management, and headline offer selection.\u003c/p\u003e\n"],["\u003cp\u003eOptimize API performance by using asynchronous calls for parallel operations, especially for inserting multiple products simultaneously.\u003c/p\u003e\n"],["\u003cp\u003eMaintain product data by regularly re-uploading all products, updating individual products as needed, and managing product expiration or deletion.\u003c/p\u003e\n"],["\u003cp\u003eThe headline offer can be strategically chosen for prominence and stability, even if it's not the cheapest or top offer on your site.\u003c/p\u003e\n"],["\u003cp\u003eStay informed about API updates and improvements by revisiting this document periodically for new features and simplified usage guidelines.\u003c/p\u003e\n"]]],[],null,["# CSS API Best Practices\n\nThis document describes some of the best practices around using the CSS API. The\nadvice given on this page is not mandatory to use the API, but may help clarify\nsome of the intended use.\n\nSetup up your environment\n-------------------------\n\nTo setup your development environment, follow the steps given from the\n[Quickstart documentation](/comparison-shopping-services/api/guides/quickstart).\n\n- Generate a user and a permissions JSON file on the Google Cloud Console\n- Enable the CSS API in the Google Cloud Console\n- Add that user with Admin permissions to your CSS Account (CSS Group or CSS Domain)\n- Verify you are using the correct OAuth scope: `https://www.googleapis.com/auth/content`\n\nClient libraries are now in the standard repositories for most programming\nlanguage. You can find a list of them on our\n[client library](/comparison-shopping-services/api/client-libraries) page.\n\nUse correct IDs\n---------------\n\nUse the correct IDs with the correct API endpoints:\n\n- **CSS API (`css.googleapis.com`):** Use the **CSS Domain ID** when interacting with CSS products (e.g., `accounts/{cssDomainId}/cssProductInputs:insert`).\n- **Merchant API (`merchantapi.googleapis.com`):** Use the Merchant API for standard merchant products.\n\nMixing these up will lead to errors. Refer to the\n[CSS API Overview](/comparison-shopping-services/api/overview) for more details.\n\nGood methods to get started\n---------------------------\n\nWe recommend testing with the following methods:\n\n### ListChildAccounts\n\n[ListChildAccounts](/comparison-shopping-services/api/reference/rpc/google.shopping.css.v1#google.shopping.css.v1.AccountsService.ListChildAccounts)\nis a read-only call that lists all of your CSS Domains (if called for a\nCSS Group) or your Merchants (if called for a CSS Domain). It is therefore a\ngood method to test if everything is setup correctly.\n\n### Insert/List/Update/Delete a product\n\nOnce you know that the API itself works, try adding a product. Make sure you use\na `raw_provided_id` that you remember.\n\n- Insert a test product using [InsertCssProductInput](/comparison-shopping-services/api/reference/rpc/google.shopping.css.v1#google.shopping.css.v1.CssProductInputsService.InsertCssProductInput). We have [sample code](/comparison-shopping-services/api/code-samples/cssproducts/insert-css-product-input) if you need help on which attributes to send.\n- List all of your products using [ListCssProducts](/comparison-shopping-services/api/reference/rpc/google.shopping.css.v1#google.shopping.css.v1.CssProductsService.ListCssProducts). There is a small processing delay before a product shows up after insertion, so if you don't see it, try again after a few seconds.\n- Update a single product using [UpdateCssProductInput](/comparison-shopping-services/api/reference/rpc/google.shopping.css.v1#google.shopping.css.v1.CssProductInputsService.UpdateCssProductInput) using your `cssproductinput.name`. You need to send only the attributes required to be updated. Refer to [sample code](/comparison-shopping-services/api/code-samples/cssproducts/update-css-product-input) here.\n- Delete the test product using [DeleteCssProductInput](/comparison-shopping-services/api/reference/rpc/google.shopping.css.v1#google.shopping.css.v1.CssProductInputsService.DeleteCssProductInput). You will need to use the `raw_provided_id`.\n\nUse Async to improve performance\n--------------------------------\n\nThe CSS API is designed for parallel calls. You will find that the performance\nof single operations can be slow, but will be much faster when calling the same\noperation multiple times in parallel. The best way to use this feature is to use\nthe async functionality of your programming language.\n\nExamples from some programming languages:\n\n- For Java, use [insertCssProductInputCallable().futureCall()](https://cloud.google.com/java/docs/reference/google-shopping-css/latest/com.google.shopping.css.v1.CssProductInputsServiceClient#com_google_shopping_css_v1_CssProductInputsServiceClient_insertCssProductInputCallable__)\n- For Python, use [CssProductInputsServiceAsyncClient](https://googleapis.dev/python/google-shopping-css/latest/css_v1/css_product_inputs_service.html)\n- For C#, use [InsertCssProductInputAsync](https://googleapis.dev/dotnet/Google.Shopping.Css.V1/latest/api/Google.Shopping.Css.V1.CssProductInputsService.CssProductInputsServiceClient.html#Google_Shopping_Css_V1_CssProductInputsService_CssProductInputsServiceClient_InsertCssProductInputAsync_Google_Shopping_Css_V1_InsertCssProductInputRequest_Grpc_Core_CallOptions_)\n\nFind and use the Async functionality of your programming language to insert\nmultiple products at the same time. You don't need to worry about overloading\nour systems - this is what the\n[quota limits](/comparison-shopping-services/api/guides/limits) are for.\n\nMore details can be found on our [performance\npage](/comparison-shopping-services/api/guides/performance).\n\nValidate Your Payloads\n----------------------\n\nTo avoid common errors, verify your JSON payloads are correctly formatted:\n\n- **Consult Official Documentation:** Always refer to the latest [CSS API reference](/comparison-shopping-services/api/reference/rest) for field definitions, enums, data types, and payload structure.\n- **Review Sample Payloads:** Compare your payloads with the provided [code samples](/comparison-shopping-services/api/code-samples) to identify discrepancies.\n- **Data Types:** Make sure you are using the correct data types (e.g., strings, objects, arrays) as specified in the documentation.\n- **Test Incrementally:** Start with minimal valid payloads to confirm basic connectivity and gradually add more attributes.\n\nUpdate a product\n----------------\n\nOnce a product is uploaded, it will stay in our system until it is either\nupdated, deleted, or expired.\n\n- You can update the full product by sending the `InsertCssProductInput` request again, using the same `raw_provided_id` you used initially. For now, you will need to send the full product data, even if only some attributes (maybe only price/availablibilty) have changed.\n- You can update parts of a product, using PATCH method `UpdateCssProductInput`, specifying product name,and a JSON body containing the data you would like to update for the product. Unlike `InsertCssProductInput`, that requires all applicable fields to be provided, `UpdateCssProductInput` only requires you to specify the fields you would like to change.\n- You can delete a product by calling `DeleteCssProductInput` with the same `raw_provided_id`.\n- Products expire automatically approximately one month after the last update.\n\nContinuous operation mode\n-------------------------\n\nA continuous operation mode could look like the following:\n\n- Use your own internal IDs as `raw_provided_id`.\n- Re-upload all products on a regular schedule, maybe weekly. This will ensure that active products don't expire.\n- Update individual products as soon as you get the changed data from your merchants.\n - If you can't react to changes immediately, find all changed products frequently (maybe hourly) and re-upload only those products.\n - For products which are no longer available, you can either use the delete call or set the number of available offers to 0.\n - Don't send us unchanged products frequently. These calls would count against your API quota. A weekly refresh is sufficient.\n\nHeadline offer selection\n------------------------\n\nThe headline offer does not necessarily need to be the top offer or the\ncheapest offer on your site, but it does need to be featured prominently. You\ncan use this for cases where your top offer is changing fast: Here you could\nselect another offer which is more stable.\n\nRe-Check this document every once in a while\n--------------------------------------------\n\nWe have received feedback on how to improve this API, and are working on making\nsome of these improvements available. This page will be updated when we have new\nfeatures available that will simplify the usage of the CSS API."]]