CSS API 最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档介绍了一些有关使用 CSS API 的最佳实践。本页面上提供的建议并非使用该 API 的强制要求,但可能有助于明确某些预期用途。
设置环境
如需设置开发环境,请按照快速入门文档中的步骤操作。
- 在 Google Cloud 控制台中生成用户和权限 JSON 文件
- 在 Google Cloud 控制台中启用 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 专为并行调用而设计。您会发现,单个操作的性能可能较慢,但如果并行多次调用同一操作,性能会快得多。使用此功能的最佳方式是使用编程语言的异步功能。
以下是一些编程语言的示例:
找到并使用编程语言的异步功能,同时插入多个商品。您无需担心我们的系统过载,因为我们有配额限制。
如需了解详情,请参阅我们的性能页面。
验证载荷
为避免常见错误,请验证您的 JSON 载荷是否格式正确:
- 查阅官方文档:请务必参阅最新的 CSS API 参考文档,了解字段定义、枚举、数据类型和载荷结构。
- 查看载荷示例:将您的载荷与提供的代码示例进行比较,以找出差异。
- 数据类型:请确保您使用的是文档中指定的正确数据类型(例如字符串、对象、数组)。
- 逐步测试:从最小的有效载荷开始,确认基本连接,然后逐步添加更多属性。
更新商品
商品上传后,除非更新、删除或过期,否则会一直保留在我们的系统中。
- 您可以再次发送
InsertCssProductInput
请求,并使用最初使用的相同 raw_provided_id
来更新完整商品。目前,您需要发送完整的商品数据,即使只有部分属性(可能只有价格/供应情况)发生了变化也是如此。
- 您可以使用 PATCH 方法
UpdateCssProductInput
更新商品的部分内容,指定商品名称以及包含您要更新的商品数据的 JSON 正文。与需要提供所有适用字段的 InsertCssProductInput
不同,UpdateCssProductInput
仅要求您指定要更改的字段。
- 您可以通过使用相同的
raw_provided_id
调用 DeleteCssProductInput
来删除商品。
- 产品会在上次更新后大约一个月自动过期。
持续运行模式
连续操作模式可能如下所示:
- 使用您自己的内部 ID 作为
raw_provided_id
。
- 定期(例如每周)重新上传所有商品。这样可确保有效商品不会过期。
- 在从商家处获得更改后的数据后,立即更新各个商品。
- 如果您无法立即对更改做出反应,请经常(可能每小时)查找所有更改后的商品,然后仅重新上传这些商品。
- 对于不再提供的商品,您可以使用删除调用,也可以将可用的出价数量设置为 0。
- 请勿频繁向我们发送未更改的商品。这些调用会计入您的 API 配额。每周刷新一次就足够了。
主推优惠选择
主推优惠不一定需要是您网站上的顶级优惠或最便宜的优惠,但必须以醒目的方式呈现。如果您的热门优惠经常变化,您可以使用此功能:您可以选择另一个更稳定的优惠。
不时重新检查此文档
我们收到了有关如何改进此 API 的反馈,目前正在努力实现其中一些改进。当我们推出可简化 CSS API 使用的新功能时,会更新本页面。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):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."]]