# Update stock levels for multiple SKUs

URL: https://docs.sparklayer.io/developers/api/stock/update-stock-levels
Operation: `POST /api/v1/batch-update-stock` (operationId `updateBatchStock`)
OpenAPI spec: https://docs.sparklayer.io/openapi/stock.yaml

Creates or updates the stock level for each SKU and stock location pair in the array. Identify the location with either `stock_location_id` or `stock_location_external_id`, not both. The stored values for each pair you send are overwritten; pairs you leave out are unchanged.

Returns `400` if a `stock_location_external_id` does not exist, or if the same SKU and location appear more than once in the request.

`POST /api/v1/batch-update-stock`

**Base URLs:** `https://app.sparklayer.io` (Live), `https://test.app.sparklayer.io` (Test)

**Authentication:** send `Authorization: Bearer <access_token>` and `Site-Id: <site id>` with every request. Get the token from [Get an access token](https://docs.sparklayer.io/developers/api/core/get-an-access-token.md); see [Authentication](https://docs.sparklayer.io/developers/authentication.md).

### Example request

```bash
curl -X POST "https://app.sparklayer.io/api/v1/batch-update-stock" \
  -H "Authorization: Bearer $SPARKLAYER_TOKEN" \
  -H "Site-Id: $SPARKLAYER_SITE_ID" \
  -H "Content-Type: application/json" \
  -d '[
  {
    "stock_location_id": "123e4567-e89b-12d3-a456-426614174000",
    "sku": "SKU-1",
    "stock_level": 9,
    "restock_date": "2020-01-01",
    "min_stock_level": -9
  }
]'
```

Set `SPARKLAYER_TOKEN` to an access token and `SPARKLAYER_SITE_ID` to your Site ID. For the test environment, use `https://test.app.sparklayer.io`.

### Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Site-Id` | string | Yes | Your SparkLayer Site ID, from Settings > API in the SparkLayer Dashboard. Example: `jones-climbing` |

### Request body (`application/json`) (required)

An array. Each item is one of these:

**BatchUpdateStockLevelByLocationId**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `[].stock_location_id` | string (uuid) | Yes | Stock Location ID (internal UUID) |
| `[].sku` | string | Yes | Product Variant SKU. Length: max 128. |
| `[].stock_level` | integer (int32) | Yes | Quantity of stock held at the location. Default: `0`. |
| `[].restock_date` | string (date) \| null | | Restock Date |
| `[].min_stock_level` | integer (int32) | | Allow pre-ordering / overselling up to this qty. For example, if set to -9 and stock level was 0, 9 units could be sold. Default: `0`. |

**BatchUpdateStockLevelByExternalLocationId**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `[].stock_location_external_id` | string | Yes | External ID (must be unique for the site). Length: max 255. |
| `[].sku` | string | Yes | Product Variant SKU. Length: max 128. |
| `[].stock_level` | integer (int32) | Yes | Quantity of stock held at the location. Default: `0`. |
| `[].restock_date` | string (date) \| null | | Restock Date |
| `[].min_stock_level` | integer (int32) | | Allow pre-ordering / overselling up to this qty. For example, if set to -9 and stock level was 0, 9 units could be sold. Default: `0`. |

```json
[
  {
    "stock_location_id": "123e4567-e89b-12d3-a456-426614174000",
    "sku": "SKU-1",
    "stock_level": 9,
    "restock_date": "2020-01-01",
    "min_stock_level": -9
  }
]
```

### Responses

#### 204: The stock levels were saved. No body.

#### Other statuses: An error. The body describes the problem: see [Errors](https://docs.sparklayer.io/developers/errors.md).

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `detail` | string | | Human-readable summary of the error |
| `status` | integer | | HTTP Status code returned from API |
| `title` | string | | Machine-readable error code |
| `type` | string \| null | | |
| `errors` | object[] \| null | | A list of errors related to the call |
| `errors[].code` | string | | Machine-readable error code |
| `errors[].property` | string | | The property the error relates to |
| `errors[].message` | string | | Human readable summary of the error |

```json
{
  "detail": "Data Validation Failed",
  "status": 400,
  "title": "invalid-api-request-contents",
  "type": "https://docs.sparklayer.io/api/errors#invalid-api-request-contents",
  "errors": [
    {
      "code": "resource-not-found",
      "property": "stock_location_id",
      "message": "stock_location_id not found"
    }
  ]
}
```
