You can use [batch requests](https://cloud.google.com/compute/docs/api/how-tos/batch)
with the Merchant API to send multiple HTTP requests in a single API call.

If you prefer to perform batching with [client
libraries](https://developers.google.com/merchant/api/client-libraries), see [Refactor code for concurrent
requests](https://developers.google.com/merchant/api/guides/compatibility/refactor-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 Merchant API is `batch/{sub-api}/v1`. You
can find the `batchPath` for other APIs in their [discovery
documents](https://developers.google.com/discovery/v1/reference/apis).

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.

Sending multiple requests in parallel prevents you from waiting for the slowest
sub-request, which improves server response times and reduces latency.

> [!TIP]
> **Tip:** For optimal performance and minimal latency, don't send more than 100 nested requests in a single batch request. For more details, see [Limits](https://developers.google.com/merchant/api/guides/send-multiple-requests#limits). For the API-specific quotas, see [Quotas](https://developers.google.com/merchant/api/guides/quotas-limits/quotas).

> [!NOTE]
> **Note:** Batching requests doesn't reduce your usage count. If a batch has N individual requests, it counts as N requests against your quota.

## 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:

1. Use `https://merchantapi.googleapis.com/batch/{sub-api}/v1` as the base URL.
2. Specify a boundary to separate each nested request, for example: `-H
   'Content-Type: multipart/mixed,boundary=batch_inventory' \`
3. Separate each nested request with the boundary, for example `--batch_inventory`.
4. Include `Content-Type: application/http` at the beginning of each nested request.
5. Use `Content-ID` to label each nested request with your own ID. For example: `Content-ID: <get~en~US~123456>`.
6. 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.
7. Don't include the base URL in each individual nested request.
8. End the main request with a final boundary, for example `--batch_inventory--`.

    curl https://merchantapi.googleapis.com/batch/inventories/v1 \
    -H 'Authorization: Bearer {ACCESS_TOKEN}' \
    -H 'Content-Type: multipart/mixed,boundary=batch_inventory' \
    --data '
    --batch_inventory
    Content-Type: application/http
    Content-ID: <get~en~US~123456>

    GET /inventories/v1/accounts/123/products/en~US~123456/regionalInventories

    --batch_inventory
    Content-Type: application/http
    Content-ID: <post~en~US~123456>

    POST /inventories/v1/accounts/123/products/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~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~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/en~US~123456/regionalInventories/123456",
      "region": "123456",
      "price": {
        "amountMicros": "100000000",
        "currencyCode": "USD"
      }
    }
    --batch_inventory--

## Limits

Batch requests have the following limits:

- **2,000** nested requests per batch request.

> [!TIP]
> **Tip:** For optimal performance and minimal latency, don't send more than 100 nested requests in a single batch request.

If a batch request exceeds any of these limits, the API returns a
`400 Bad Request` error and rejects the entire request.