You can use batch requests with the Merchant API to send multiple HTTP requests in a single API call.
If you prefer to perform batching with client libraries, see Refactor code for concurrent requests.
A batch request is a single standard HTTP request containing multiple API calls,
using the multipart/mixed
content type. Within the main HTTP request, each
part contains a nested HTTP request.
You can send the batch request to the specified batchPath
for the API. The
batchPath
for the Merchant API beta is batch/{sub-api}/v1beta
. You can find
the batchPath
for other APIs in their discovery
documents. Examples of reasons to batch your
requests include:
- You just started using the API and have a lot of data to upload.
- A user made changes to data while your application was offline, and your application needs to synchronize local data with the server.
Write a batch request
Here's a sample Merchant API batch request. This request combines a get request to retrieve the regional inventory for a product, and an insert request to update the regional inventory for the same product. You should follow the format of the example exactly:
- Use
https://merchantapi.googleapis.com/batch/{sub-api}/v1beta
as the base URL. - Specify a boundary to separate each nested request, for example:
-H 'Content-Type: multipart/mixed,boundary=batch_inventory' \
- Separate each nested request with the boundary, for example
--batch_inventory
. - Include
Content-Type: application/http
at the beginning of each nested request. - Use
Content-ID
to label each nested request with your own ID. For example:Content-ID: <get:online:en:US:123456>
. - Include a blank line between the header, path and body of each nested request. If the nested request doesn't have a body, leave a blank line before the next boundary.
- Don't include the base URL in each individual nested request.
- End the main request with a final boundary, for example
--batch_inventory–
.
curl https://merchantapi.googleapis.com/batch/inventories/v1beta \
-H 'Authorization: Bearer <TOKEN>' \
-H 'Content-Type: multipart/mixed,boundary=batch_inventory' \
--data '
--batch_inventory
Content-Type: application/http
Content-ID: <get:online:en:US:123456>
GET /inventories/v1beta/accounts/123/products/online:en:US:123456/regionalInventories
--batch_inventory
Content-Type: application/http
Content-ID: <post:online:en:US:123456>
POST /inventories/v1beta/accounts/123/products/online:en:US:123456/regionalInventories:insert
{
"region: "123456",
"price": {
"amountMicros": "100000000",
"currencyCode": "USD"
}
}
--batch_inventory--
'
Notes on ordering
- Requests might not execute in the order you specify them.
- Use
Content-ID
to identify individual requests. - If you need to execute your calls in a given order, send them separately and wait for the response to the first request before sending the next one.
Read a batch response
Here's an example of an HTTP batch response. The order of the responses might
not match the order of the requests. Use Content-ID
to identify the nested
request each nested response belongs to. In the responses, the API adds a
response-
prefix to each Content-ID
.
--batch_inventory
Content-Type: application/http
Content-ID: <response-get:online:en:US:123456>
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer
{}
--batch_inventory
Content-Type: application/http
Content-ID: <response-post:online:en:US:123456>
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: X-Origin
Vary: Referer
{
"name": "accounts/123/products/online:en:US:123456/regionalInventories/123456",
"region": "123456",
"price": {
"amountMicros": "100000000",
"currencyCode": "USD"
}
}
--batch_inventory--
'