rocket
Giới thiệu
Merchant API – API chính thức thay thế cho Content API for Shopping.
add_alert
Lưu ý: Content API for Shopping sẽ ngừng hoạt động từ ngày 18 tháng 8 năm 2026.
Bắt đầu yêu cầu
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này giả định rằng bạn đã xem Hướng dẫn bắt đầu của chúng tôi và đã thiết lập để đưa ra các yêu cầu được uỷ quyền.
Sau khi thiết lập mọi thứ, bạn có thể gửi yêu cầu đến Google Content API for Shopping. Các mẫu mã sau đây minh hoạ cách gửi một số yêu cầu đơn giản:
- Tạo sản phẩm.
- Lấy danh sách sản phẩm.
- Truy xuất một sản phẩm cụ thể trong danh sách.
- Cập nhật giá của sản phẩm.
Để xem danh sách đầy đủ các phương thức, hãy xem
tài liệu tham khảo.
Để biết thêm thông tin, hãy xem trang Các phương pháp hay nhất. Trang này giải thích cách sử dụng API hiệu quả nhất.
Trang này cũng giải thích lý do bạn nên sử dụng các yêu cầu theo lô.
Nếu bạn gặp vấn đề với Content API, hãy kiểm tra
trang tổng quan trạng thái của Merchant Center để biết tình trạng ngừng hoạt động và trang Hỗ trợ.
Tạo một sản phẩm
Bạn có thể xem thông tin chi tiết về tất cả các thuộc tính mà một sản phẩm có thể có trong Quy cách nguồn cấp dữ liệu sản phẩm.
Để tạo một sản phẩm
, hãy dùng mã sau:
Giao thức
POST /content/v2.1/YOUR_MERCHANT_ID/products
{
"offerId": "book123",
"title": "A Tale of Two Cities",
"description": "A classic novel about the French Revolution",
"link": "http://my-book-shop.com/tale-of-two-cities.html",
"imageLink": "http://my-book-shop.com/tale-of-two-cities.jpg",
"contentLanguage": "en",
"targetCountry": "GB",
"feedLabel": "GB",
"channel": "online",
"availability": "in stock",
"condition": "new",
"googleProductCategory": "Media > Books",
"gtin": "9780007350896",
"price": {
"value": "2.50",
"currency": "GBP"
},
"shipping": [{
"country": "GB",
"service": "Standard shipping",
"price": {
"value": "0.99",
"currency": "GBP"
}
}],
"shippingWeight": {
"value": "200",
"unit": "grams"
}
}
Java
Product product = new Product();
product.setOfferId("book123");
product.setTitle("A Tale of Two Cities");
product.setDescription("A classic novel about the French Revolution");
product.setLink("http://my-book-shop.com/tale-of-two-cities.html");
product.setImageLink("http://my-book-shop.com/tale-of-two-cities.jpg");
product.setContentLanguage("en");
product.setTargetCountry("GB");
product.setChannel("online");
product.setAvailability("in stock");
product.setCondition("new");
product.setGoogleProductCategory("Media > Books");
product.setGtin("9780007350896");
Price price = new Price();
price.setValue("2.50");
price.setCurrency("GBP");
product.setPrice(price);
Price shippingPrice = new Price();
shippingPrice.setValue("0.99");
shippingPrice.setCurrency("GBP");
ProductShipping shipping = new ProductShipping();
shipping.setPrice(shippingPrice);
shipping.setCountry("GB");
shipping.setService("Standard shipping");
ArrayList shippingList = new ArrayList();
shippingList.add(shipping);
product.setShipping(shippingList);
Product result = service.products().insert(merchantId, product).execute();
PHP
$product = new Google_Service_ShoppingContent_Product();
$product->setOfferId('book123');
$product->setTitle('A Tale of Two Cities');
$product->setDescription('A classic novel about the French Revolution');
$product->setLink('http://my-book-shop.com/tale-of-two-cities.html');
$product->setImageLink('http://my-book-shop.com/tale-of-two-cities.jpg');
$product->setContentLanguage('en');
$product->setTargetCountry('GB');
$product->setChannel('online');
$product->setAvailability('in stock');
$product->setCondition('new');
$product->setGoogleProductCategory('Media > Books');
$product->setGtin('9780007350896');
$price = new Google_Service_ShoppingContent_Price();
$price->setValue('2.50');
$price->setCurrency('GBP');
$shipping_price = new Google_Service_ShoppingContent_Price();
$shipping_price->setValue('0.99');
$shipping_price->setCurrency('GBP');
$shipping = new Google_Service_ShoppingContent_ProductShipping();
$shipping->setPrice($shipping_price);
$shipping->setCountry('GB');
$shipping->setService('Standard shipping');
$shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight();
$shipping_weight->setValue(200);
$shipping_weight->setUnit('grams');
$product->setPrice($price);
$product->setShipping(array($shipping));
$product->setShippingWeight($shipping_weight);
$result = $service->products->insert($merchant_id, $product);
Lấy danh sách sản phẩm
Để lấy danh sách sản phẩm, hãy dùng mã sau:
Giao thức
GET /content/v2.1/YOUR_MERCHANT_ID/products
Java
List productsList = service.products().list(merchantId);
ProductsListResponse page = productsList.execute();
while ((page.getResources() != null) && !page.getResources().isEmpty()) {
for (Product product : page.getResources()) {
System.out.printf("%s %s%n", product.getId(), product.getTitle());
}
if (page.getNextPageToken() == null) {
break;
}
productsList.setPageToken(page.getNextPageToken());
page = productsList.execute();
}
PHP
$products = $service->products->listProducts($merchantId);
$parameters = array();
while (!empty($products->getResources()) {
foreach ($products->getResources() as $product) {
printf("%s %s\n", $product->getId(), $product->getTitle());
}
if (!empty($products->getNextPageToken()) {
break;
}
$parameters['pageToken'] = $products->nextPageToken;
$products = $service->products->listProducts($merchantId, $parameters);
}
Truy xuất một sản phẩm cụ thể
Để truy xuất một sản phẩm cụ thể khỏi danh sách, hãy sử dụng đoạn mã sau:
Giao thức
GET /content/v2.1/YOUR_MERCHANT_ID/products/online:en:GB:book123
Java
Product product = service.products()
.get(merchantId, "online:en:GB:book123")
.execute();
System.out.printf("%s %s\n", product.getId(), product.getTitle());
PHP
$product = $service->products->get($merchant_id, 'online:en:GB:book123');
printf("%s %s\n", $product->getId(), $product->getTitle());
Cập nhật tình trạng còn hàng của sản phẩm trực tuyến
Bạn có thể sử dụng
Nguồn cấp dữ liệu bổ sung trong tài nguyên Sản phẩm để cập nhật dữ liệu sản phẩm trực tuyến bằng mã sau:
Giao thức
POST /content/v2.1/YOUR_MERCHANT_ID/products?YOUR_SUPPLEMENTAL_FEED_ID
{
"offerId": "book123",
"contentLanguage": "en",
"targetCountry": "GB",
"feedLabel": "GB",
"channel": "online",
"availability": "out of stock"
}
Java
Product product = new Product();
// Mandatory Fields
product.setOfferId("book123");
product.setContentLanguage("en");
product.setTargetCountry("GB");
product.setChannel("online");
// Optional Fields to Update
product.setAvailability("out of stock");
// Your unique supplemental feedId
feedId=123456789
Product result = service.products().insert(merchantId, product, feedId).execute();
PHP
$product = new Google_Service_ShoppingContent_Product();
// Mandatory Fields
$product->setOfferId('book123');
$product->setContentLanguage('en');
$product->setTargetCountry('GB');
$product->setChannel('online');
// Optional Fields to Update
$product->setAvailability('out of stock');
// Your unique supplemental feedId
$feedId=123456789
$result = $service->products->insert($merchant_id, $product, $feedId);
Cập nhật tình trạng còn hàng của sản phẩm tại địa phương
Bạn có thể sử dụng
Dịch vụ kho hàng tại địa phương để cập nhật dữ liệu sản phẩm tại địa phương bằng mã sau:
Giao thức
POST /content/v2.1/YOUR_MERCHANT_ID/localinventory/online/products/online:en:GB:book123
{
"availability": "out of stock"
}
Java
Product product = new Product();
// Mandatory Fields
product.setOfferId("book123");
product.setContentLanguage("en");
product.setTargetCountry("GB");
product.setChannel("online");
// Optional Fields to Update
product.setAvailability("out of stock");
Product result = service.localinventory().insert(merchantId, product).execute();
PHP
$product = new Google_Service_ShoppingContent_Product();
// Mandatory Fields
$product->setOfferId('book123');
$product->setContentLanguage('en');
$product->setTargetCountry('GB');
$product->setChannel('online');
// Optional Fields to Update
$product->setAvailability('out of stock');
$result = $service->localinventory->insert($merchant_id, $product);
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-29 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[[["\u003cp\u003eThe Merchant API is the new version of the Content API for Shopping and represents the future of product data integration with Google.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides code samples to manage products, including creating, retrieving, listing, and updating them via the API.\u003c/p\u003e\n"],["\u003cp\u003eYou can find comprehensive details on product attributes in the Products Feed Specification and best practices for using the API in the provided links.\u003c/p\u003e\n"],["\u003cp\u003eUtilize supplemental feeds to update online product data and the Local Inventory service to manage local product information efficiently.\u003c/p\u003e\n"],["\u003cp\u003eFor support or to check for service outages, refer to the Merchant Center status dashboard and the Support page.\u003c/p\u003e\n"]]],["The Merchant API beta, a new version of the Content API for Shopping, allows users to manage product data. Key actions include creating a product with details like title, price, and shipping, retrieving a list of products, and fetching a specific product. Additionally, users can update product availability for both online and local inventory using supplemental feeds or the local inventory service. The provided code examples demonstrate these actions in Protocol, Java, and PHP.\n"],null,["# Make requests\n\nThis guide assumes you've worked through\nour [Getting Started](/shopping-content/guides/quickstart) guide and are set up to\nmake authorized requests.\n\nAfter you've set everything up, you can send requests to the Google Content API for Shopping. The following code\nsamples demonstrate how to send a few simple requests:\n\n- Create a product.\n- Get a list of products.\n- Retrieve a specific product from the list.\n- Update a product's price.\n\nFor a full list of methods, see the [reference documentation](/shopping-content/reference/rest/v2.1).\n\nFor more information, see the [Best Practices](/shopping-content/guides/best-practices) page, which\nexplains how to best use the API.\nThis page also explains why batch requests are recommended.\n\n\nIf you're having issues with the Content API, check the [Merchant Center status dashboard](//merchants.google.com/status) for outages, and the\n[Support](/shopping-content/support/contact-us) page.\n\n### Create a product\n\nYou can find full details of all the attributes that a product can have in the\n[Products Feed Specification](//support.google.com/merchants/answer/188494).\nTo [create a product](/shopping-content/reference/rest/v2.1/products/insert), use the following code: \n\nProtocol\n--------\n\n```scdoc\nPOST /content/v2.1/YOUR_MERCHANT_ID/products\n\n{\n \"offerId\": \"book123\",\n \"title\": \"A Tale of Two Cities\",\n \"description\": \"A classic novel about the French Revolution\",\n \"link\": \"http://my-book-shop.com/tale-of-two-cities.html\",\n \"imageLink\": \"http://my-book-shop.com/tale-of-two-cities.jpg\",\n \"contentLanguage\": \"en\",\n \"targetCountry\": \"GB\",\n \"feedLabel\": \"GB\",\n \"channel\": \"online\",\n \"availability\": \"in stock\",\n \"condition\": \"new\",\n \"googleProductCategory\": \"Media \u003e Books\",\n \"gtin\": \"9780007350896\",\n \"price\": {\n \"value\": \"2.50\",\n \"currency\": \"GBP\"\n },\n \"shipping\": [{\n \"country\": \"GB\",\n \"service\": \"Standard shipping\",\n \"price\": {\n \"value\": \"0.99\",\n \"currency\": \"GBP\"\n }\n }],\n \"shippingWeight\": {\n \"value\": \"200\",\n \"unit\": \"grams\"\n }\n}\n```\n\nJava\n----\n\n```java\nProduct product = new Product();\n\nproduct.setOfferId(\"book123\");\nproduct.setTitle(\"A Tale of Two Cities\");\nproduct.setDescription(\"A classic novel about the French Revolution\");\nproduct.setLink(\"http://my-book-shop.com/tale-of-two-cities.html\");\nproduct.setImageLink(\"http://my-book-shop.com/tale-of-two-cities.jpg\");\nproduct.setContentLanguage(\"en\");\nproduct.setTargetCountry(\"GB\");\nproduct.setChannel(\"online\");\nproduct.setAvailability(\"in stock\");\nproduct.setCondition(\"new\");\nproduct.setGoogleProductCategory(\"Media \u003e Books\");\nproduct.setGtin(\"9780007350896\");\n\nPrice price = new Price();\nprice.setValue(\"2.50\");\nprice.setCurrency(\"GBP\");\nproduct.setPrice(price);\n\nPrice shippingPrice = new Price();\nshippingPrice.setValue(\"0.99\");\nshippingPrice.setCurrency(\"GBP\");\n\nProductShipping shipping = new ProductShipping();\nshipping.setPrice(shippingPrice);\nshipping.setCountry(\"GB\");\nshipping.setService(\"Standard shipping\");\n\nArrayList shippingList = new ArrayList();\nshippingList.add(shipping);\nproduct.setShipping(shippingList);\n\nProduct result = service.products().insert(merchantId, product).execute();\n```\n\nPHP\n---\n\n```php\n$product = new Google_Service_ShoppingContent_Product();\n$product-\u003esetOfferId('book123');\n$product-\u003esetTitle('A Tale of Two Cities');\n$product-\u003esetDescription('A classic novel about the French Revolution');\n$product-\u003esetLink('http://my-book-shop.com/tale-of-two-cities.html');\n$product-\u003esetImageLink('http://my-book-shop.com/tale-of-two-cities.jpg');\n$product-\u003esetContentLanguage('en');\n$product-\u003esetTargetCountry('GB');\n$product-\u003esetChannel('online');\n$product-\u003esetAvailability('in stock');\n$product-\u003esetCondition('new');\n$product-\u003esetGoogleProductCategory('Media \u003e Books');\n$product-\u003esetGtin('9780007350896');\n\n$price = new Google_Service_ShoppingContent_Price();\n$price-\u003esetValue('2.50');\n$price-\u003esetCurrency('GBP');\n\n$shipping_price = new Google_Service_ShoppingContent_Price();\n$shipping_price-\u003esetValue('0.99');\n$shipping_price-\u003esetCurrency('GBP');\n\n$shipping = new Google_Service_ShoppingContent_ProductShipping();\n$shipping-\u003esetPrice($shipping_price);\n$shipping-\u003esetCountry('GB');\n$shipping-\u003esetService('Standard shipping');\n\n$shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight();\n$shipping_weight-\u003esetValue(200);\n$shipping_weight-\u003esetUnit('grams');\n\n$product-\u003esetPrice($price);\n$product-\u003esetShipping(array($shipping));\n$product-\u003esetShippingWeight($shipping_weight);\n\n$result = $service-\u003eproducts-\u003einsert($merchant_id, $product);\n```\n\n### Get a list of products\n\nTo [get a list of\nproducts](/shopping-content/reference/rest/v2.1/products/list), use the following code: \n\nProtocol\n--------\n\n```scdoc\nGET /content/v2.1/YOUR_MERCHANT_ID/products\n```\n\nJava\n----\n\n```text\nList productsList = service.products().list(merchantId);\n\nProductsListResponse page = productsList.execute();\nwhile ((page.getResources() != null) && !page.getResources().isEmpty()) {\n for (Product product : page.getResources()) {\n System.out.printf(\"%s %s%n\", product.getId(), product.getTitle());\n }\n\n if (page.getNextPageToken() == null) {\n break;\n }\n\n productsList.setPageToken(page.getNextPageToken());\n page = productsList.execute();\n}\n```\n\nPHP\n---\n\n```ecl\n$products = $service-\u003eproducts-\u003elistProducts($merchantId);\n$parameters = array();\nwhile (!empty($products-\u003egetResources()) {\n foreach ($products-\u003egetResources() as $product) {\n printf(\"%s %s\\n\", $product-\u003egetId(), $product-\u003egetTitle());\n }\n if (!empty($products-\u003egetNextPageToken()) {\n break;\n }\n $parameters['pageToken'] = $products-\u003enextPageToken;\n $products = $service-\u003eproducts-\u003elistProducts($merchantId, $parameters);\n}\n```\n\n### Retrieve a specific product\n\nTo [retrieve a specific\nproduct from the list](/shopping-content/reference/rest/v2.1/products/get), use the following code: \n\nProtocol\n--------\n\n```scdoc\nGET /content/v2.1/YOUR_MERCHANT_ID/products/online:en:GB:book123\n```\n\nJava\n----\n\n```css+lasso\nProduct product = service.products()\n .get(merchantId, \"online:en:GB:book123\")\n .execute();\nSystem.out.printf(\"%s %s\\n\", product.getId(), product.getTitle());\n```\n\nPHP\n---\n\n```css+lasso\n$product = $service-\u003eproducts-\u003eget($merchant_id, 'online:en:GB:book123');\nprintf(\"%s %s\\n\", $product-\u003egetId(), $product-\u003egetTitle());\n```\n\n### Update online product availability\n\nYou can use [Supplemental Feeds](/shopping-content/guides/products/supplemental-feeds) in the Products resource to update online product data with the following\ncode: \n\nProtocol\n--------\n\n```scdoc\nPOST /content/v2.1/YOUR_MERCHANT_ID/products?YOUR_SUPPLEMENTAL_FEED_ID\n\n{\n \"offerId\": \"book123\",\n \"contentLanguage\": \"en\",\n \"targetCountry\": \"GB\",\n \"feedLabel\": \"GB\",\n \"channel\": \"online\",\n \"availability\": \"out of stock\"\n}\n```\n\nJava\n----\n\n```text\nProduct product = new Product();\n// Mandatory Fields\nproduct.setOfferId(\"book123\");\nproduct.setContentLanguage(\"en\");\nproduct.setTargetCountry(\"GB\");\nproduct.setChannel(\"online\");\n\n// Optional Fields to Update\nproduct.setAvailability(\"out of stock\");\n\n// Your unique supplemental feedId\nfeedId=123456789\n\nProduct result = service.products().insert(merchantId, product, feedId).execute();\n```\n\nPHP\n---\n\n```ecl\n$product = new Google_Service_ShoppingContent_Product();\n// Mandatory Fields\n$product-\u003esetOfferId('book123');\n$product-\u003esetContentLanguage('en');\n$product-\u003esetTargetCountry('GB');\n$product-\u003esetChannel('online');\n\n// Optional Fields to Update\n$product-\u003esetAvailability('out of stock');\n\n// Your unique supplemental feedId\n$feedId=123456789\n\n$result = $service-\u003eproducts-\u003einsert($merchant_id, $product, $feedId);\n```\n\n### Update local product availability\n\nYou can use the [Local Inventory Service](/shopping-content/reference/rest/v2.1/localinventory) to update local product data with the following code: \n\nProtocol\n--------\n\n```scdoc\nPOST /content/v2.1/YOUR_MERCHANT_ID/localinventory/online/products/online:en:GB:book123\n\n{\n \"availability\": \"out of stock\"\n}\n```\n\nJava\n----\n\n```text\nProduct product = new Product();\n// Mandatory Fields\nproduct.setOfferId(\"book123\");\nproduct.setContentLanguage(\"en\");\nproduct.setTargetCountry(\"GB\");\nproduct.setChannel(\"online\");\n\n// Optional Fields to Update\nproduct.setAvailability(\"out of stock\");\n\nProduct result = service.localinventory().insert(merchantId, product).execute();\n```\n\nPHP\n---\n\n```ecl\n$product = new Google_Service_ShoppingContent_Product();\n// Mandatory Fields\n$product-\u003esetOfferId('book123');\n$product-\u003esetContentLanguage('en');\n$product-\u003esetTargetCountry('GB');\n$product-\u003esetChannel('online');\n\n// Optional Fields to Update\n$product-\u003esetAvailability('out of stock');\n\n$result = $service-\u003elocalinventory-\u003einsert($merchant_id, $product);\n```"]]