# Add stock keys

`POST https://api.anyshop.io/v1/products/{id}/stock_items`

Duplicates within the upload and keys already in stock are left out and counted.

- Part of the [API reference](https://docs.anyshop.io/reference.md), [Stock](https://docs.anyshop.io/reference.md#stock).
- Scope: `stock:write`.
- Idempotent with Idempotency-Key.
- Answers 403 `store_suspended` while the store is suspended.

## Path parameters

- `id` (string, required)

## Body (application/json)

- `keys` (array of strings, required): The keys, one per item: 1 to 10,000, each up to 500 characters. They go into the stock of your API key's mode. 1 to 10,000 items.
- `variant_id` (string): The option the keys are for: required when the product keeps stock per option, refused when it has one stock for all options.

## Headers

- `Idempotency-Key` (string): Up to 255 characters. A retry with the same key and request gets the first answer, without running twice (24 hours).

## Returns

The **stock_upload** object.

## Errors

| Status | error.type | When |
| --- | --- | --- |
| 400 | `invalid_request` | The request is not valid; `error.param` names the field |
| 401 | `authentication` | No API key, or it is not valid |
| 403 | `permission` | The key does not have the scope this needs, or the store is suspended (store_suspended) |
| 404 | `not_found` | Not found in the key's store and mode |
| 409 | `conflict` | Conflicts with the object's state (type conflict), or an Idempotency-Key reused for another request (type idempotency, code idempotency_key_reused) |
| 429 | `rate_limited` | Too many requests; see Retry-After |

## Example

curl:

```bash
curl -X POST \
  https://api.anyshop.io/v1/products/prod_01m3rr5kmbeb2s18x1kpntjxqb/stock_items \
  -H "Authorization: Bearer $ANYSHOP_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "keys": [
      "TEST1-K2LM-3F9Q",
      "TEST2-P8WN-4H7R",
      "TEST2-P8WN-4H7R"
    ],
    "variant_id": "var_01m3rr5kmd6h0q2t8w4y9zb7ce"
  }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/products/prod_01m3rr5kmbeb2s18x1kpntjxqb/stock_items", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "keys": [
      "TEST1-K2LM-3F9Q",
      "TEST2-P8WN-4H7R",
      "TEST2-P8WN-4H7R"
    ],
    "variant_id": "var_01m3rr5kmd6h0q2t8w4y9zb7ce"
  }),
});
const answer = await response.json();
```

Python:

```python
import os
import requests
import uuid

response = requests.post(
    "https://api.anyshop.io/v1/products/prod_01m3rr5kmbeb2s18x1kpntjxqb/stock_items",
    headers={
        "Authorization": f"Bearer {os.environ['ANYSHOP_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
      "keys": [
        "TEST1-K2LM-3F9Q",
        "TEST2-P8WN-4H7R",
        "TEST2-P8WN-4H7R"
      ],
      "variant_id": "var_01m3rr5kmd6h0q2t8w4y9zb7ce"
    },
)
answer = response.json()
```

Answer (200):

```json
{
  "object": "stock_upload",
  "product": "prod_01m3rr5kmbeb2s18x1kpntjxqb",
  "livemode": true,
  "submitted": 3,
  "added": 2,
  "repeated_in_upload": 1,
  "already_in_stock": 0,
  "variant_id": null
}
```

Answer (400):

```json
{
  "error": {
    "type": "invalid_request",
    "code": "invalid_body",
    "message": "The request is not valid; `error.param` names the field",
    "param": null
  }
}
```

Answer (409):

```json
{
  "error": {
    "type": "conflict",
    "code": "conflict",
    "message": "Conflicts with the object's state (type conflict), or an Idempotency-Key reused for another request (type idempotency, code idempotency_key_reused)",
    "param": null
  }
}
```

The IDs and keys in these examples are made up. Every schema is in the OpenAPI document, https://api.anyshop.io/v1/openapi.json.
