# Products

> Keep the brand's catalog in step with yours, matched by your own product id, one at a time or in bulk.

Source: https://developers.loyx.se/guides/products/

LoyX keeps a catalog per brand. Products matter for programs that reward specific products, and for the brand's reports. **Your own product id is the key**: send it as `external_product_id` and LoyX matches on it everywhere.

You do not have to load the catalog before you send sales. Unknown products on a sale are created for you.

## Products on a sale

Put your product id on every sale line as `items[].external_product_id`, with the `product_name`:

```json
{
  "external_product_id": "SKU-1001",
  "product_name": "Oat latte",
  "quantity": { "value": 1, "unit": "ea" },
  "unit_price": { "value": 4500, "currency": "SEK" }
}
```

- A known id links the line to that product.
- An unknown id **creates a minimal product**: the line's name, price, unit and identifiers, your id, no store assortment. It counts for loyalty, shows in the staff app, and stays hidden from the consumer app until the brand completes it.
- Keep `external_product_id` stable for the life of a product, and never reuse it for a different one.

Lines with `loyalty_eligible: false` never earn. Use it for gift cards, deposits and services.

## Create or update one product

```
POST /v1/products
```

```json
{
  "external_product_id": "SKU-1001",
  "name": "Oat latte",
  "price": { "value": 4500, "currency": "SEK" },
  "identifiers": [
    { "type": "sku", "value": "SKU-1001" },
    { "type": "ean13", "value": "7300000000017" }
  ],
  "stores": [{ "store_id": "8a9b0c1d-0000-4000-8000-000000000005" }],
  "is_loyalty_program_eligible": true
}
```

This is an **upsert** on `external_product_id`:

| Situation | Result |
|---|---|
| No product has this id | `201`, created. |
| A product already has this id | `200`, updated. Only the fields you send change; `price` is always applied; `stores` replaces the assortment. A removed product with this id is restored. |
| No `external_product_id` sent | `201`, always a new product. |

Retrying is safe: the same id always lands on the same product. You may also send an `Idempotency-Key`: the same key with the same body within 24 hours returns the original response, status included (a `201` stays `201`); the same key with a different body is `409 idempotency_key_reused`; a resend while the first is still running is `409 idempotency_request_in_progress`. Keys for this route and for `/bulk` are separate.

- `price` is the base price in [minor units](/guides/money/). Each store entry may override it.
- Identifier types are `gtin`, `ean13`, `ean8`, `sku` and `pos`. Each `(type, value)` pair is unique per brand.

## Bulk upsert

Send up to **100** products per call:

```
POST /v1/products/bulk
{ "products": [ { "external_product_id": "SKU-1001", "name": "Oat latte", ... }, ... ] }
```

`external_product_id` is required on every item. Items are processed in order and independently: one failure does not undo the others, and a failed item is never half written. The call always answers `207 Multi-Status` with a result per item:

```json
{
  "results": [
    { "index": 0, "external_product_id": "SKU-1001", "status": "updated", "product": { "...": "..." }, "error": null },
    { "index": 1, "external_product_id": "SKU-1002", "status": "failed", "product": null,
      "error": { "code": "store_not_found", "message": "...", "param": "products.1.stores.0.store_id" } }
  ],
  "created": 0,
  "updated": 1,
  "failed": 1
}
```

- `status` is `created`, `updated` or `failed`. Check every item: `207` does not mean all succeeded.
- An id repeated in one request fails from its second occurrence with `duplicate_in_request`.
- Retry the whole request or only the failed items: both are safe.
- Send an `Idempotency-Key` to make a retry return the original result instead of running again. The same key with the same body within 24 hours replays; the same key with a different body is `409 idempotency_key_reused`; a resend while the first is still running is `409 idempotency_request_in_progress`. Keys are scoped to your API key.
- 0 or more than 100 items is a `422`.

## Other calls

| Call | Use |
|---|---|
| `GET /v1/products` | List the catalog (`limit`, `offset`). |
| `GET /v1/products/{product_id}` | Read one product. Responses include `external_product_id`. |
| `PATCH /v1/products/{product_id}` | Change some fields, including `external_product_id`. Setting an id another product holds is `409 external_product_id_conflict`. |
| `DELETE /v1/products/{product_id}` | Remove a product. It is kept for history. |
| `GET /v1/products/{product_id}/stores` | Which stores carry it, at what price. |
| `GET /v1/stores/{store_id}/products` | What one store carries, with cursor paging. |

Reading needs `products:read`; creating, changing and removing need `products:write`. The full fields are in the [API reference](/api/).
