> ## Documentation Index
> Fetch the complete documentation index at: https://docs.keebai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PUT /v1/ecommerce/products/:id/stock/branch-links

> Make branches sell a product from the stock of another branch, for branches that are one physical store.

Links branches to a **source branch** for one product: the branches in `branch_ids` sell it from the source's stock. Use it when two or more branches in Keebai are one physical store, such as a barbershop and a salon under the same roof.

Links are per product, so the same company can have independent pairs: a source with its linked branches in one store, and another source with its own in a second store.

## Endpoint

```
PUT https://api.keebai.com/v1/ecommerce/products/{id}/stock/branch-links
```

## Required scope

`ecommerce:stock:write`

> [!NOTE]
>
> **The scope comes from your role.** Minting a token with `ecommerce:stock:write` requires the permission to adjust and transfer stock in your Keebai role, unless you are the company owner or a superuser. See [scopes](https://docs.keebai.com/dev/scopes.md#scopes-are-independent-from-role-permissions).

## Headers

| Header | Required | Value |
| - | - | - |
| `Authorization` | Yes | `Bearer kbai_pk_<token>` |
| `Content-Type` | Yes | `application/json` |

## Path parameters

| Field | Type | Required | Description |
| - | - | - | - |
| `id` | `string` | Yes | `ObjectId` of the product. |

## Body

| Field | Type | Required | Description |
| - | - | - | - |
| `source_branch_id` | `string` | Yes | Branch whose stock the others sell from. |
| `branch_ids` | `string[]` | Yes | Branches that sell from the source. Replaces the branches linked to that source; an empty list unlinks them all. Up to 50. |

### What linking does

* **The linked branch's stock moves to the source.** Whatever the branch held of this product goes to the source branch, with a `transfer` entry in [the stock movements](https://docs.keebai.com/dev/endpoints/ecommerce-stock-movements.md). The total does not change.
* **Sales from a linked branch take from the source.** The sale's movement keeps the branch that sold, and the units come off the source's stock. Connected stores are updated from the source branch.
* **A linked branch has no stock of its own.** It takes no [transfers](https://docs.keebai.com/dev/endpoints/ecommerce-stock-transfer.md) or [adjustments](https://docs.keebai.com/dev/endpoints/ecommerce-stock-adjust.md); make them on the source.
* **No chains.** A linked branch cannot be a source, and a source cannot be linked to another branch.

## Example request

```bash curl theme={"system"}
curl -X PUT https://api.keebai.com/v1/ecommerce/products/65d3e4f5a6b7c8d9e0f1a2b3/stock/branch-links \
  -H "Authorization: Bearer kbai_pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "source_branch_id": "br_01",
    "branch_ids": ["br_02"]
  }'
```

```js JavaScript theme={"system"}
const response = await fetch(
  `https://api.keebai.com/v1/ecommerce/products/${productId}/stock/branch-links`,
  {
    method: "PUT",
    headers: {
      Authorization: `Bearer ${process.env.KEEBAI_API_TOKEN}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ source_branch_id: "br_01", branch_ids: ["br_02"] }),
  },
);
const stock = await response.json();
```

```python Python theme={"system"}
import os, requests

resp = requests.put(
    f"https://api.keebai.com/v1/ecommerce/products/{product_id}/stock/branch-links",
    headers={"Authorization": f"Bearer {os.environ['KEEBAI_API_TOKEN']}"},
    json={"source_branch_id": "br_01", "branch_ids": ["br_02"]},
    timeout=10,
)
resp.raise_for_status()
stock = resp.json()
```

## Response

### 200 OK

The product's stock after linking, in the same shape as [`GET /v1/ecommerce/products/{id}/stock`](https://docs.keebai.com/dev/endpoints/ecommerce-product-stock), with the links in `links`.

```json theme={"system"}
{
  "product_id": "65d3e4f5a6b7c8d9e0f1a2b3",
  "variants": [
    {
      "variant_id": null,
      "name": "Matte Pomade 100 g",
      "total": 7,
      "allocated": 7,
      "warehouse_available": 0,
      "branches": [{ "branch_id": "br_01", "quantity": 7 }]
    }
  ],
  "links": [{ "branch_id": "br_02", "source_branch_id": "br_01" }]
}
```

### 400 Bad Request

The body does not match the schema, or one of these:

| `code` | Meaning |
| - | - |
| `STOCK_BRANCH_LINK_SELF` | `branch_ids` includes the source branch. |
| `STOCK_BRANCH_LINK_SOURCE_LINKED` | The source branch already sells from another branch. |
| `STOCK_BRANCH_LINK_TARGET_IS_SOURCE` | One of `branch_ids` is the source of other branches. |

### 401 Unauthorized

Missing, invalid, revoked, or expired token.

### 403 Forbidden

The token does not have the `ecommerce:stock:write` scope.

### 404 Not Found

No product with that id, or it belongs to a different company.

### 502 Bad Gateway

`ECOMMERCE_UPSTREAM_ERROR` — the ecommerce service could not answer.

## Operational notes

* **Sending the same set again changes nothing.** The call replaces the branches of that source, so a retry is safe.
* **To unlink one branch**, use [`DELETE .../stock/branch-links/{branch_id}`](https://docs.keebai.com/dev/endpoints/ecommerce-stock-branch-unlink), or send the source again without it.


## OpenAPI

````yaml PUT /v1/ecommerce/products/{id}/stock/branch-links
openapi: 3.0.0
info:
  title: Keebai Public API
  description: Superficie pública REST autenticada con Personal Access Tokens (PAT).
  version: 1.0.0
  contact: {}
servers:
  - url: https://api.keebai.com
    description: Production
security: []
tags: []
paths:
  /v1/ecommerce/products/{id}/stock/branch-links:
    put:
      tags:
        - ecommerce
      summary: Sincronizar sucursales de un producto
      description: >-
        Hace que las sucursales de `branch_ids` vendan este producto con el
        stock de `source_branch_id`, para sucursales que son un mismo local.
        Reemplaza las sucursales que tenía ese origen; con `branch_ids` vacío
        las desvincula todas. Lo que tenía asignado cada sucursal pasa al origen
        y el stock total no cambia. Una sucursal vinculada no puede ser origen
        de otra ni recibir traspasos o ajustes. Responde el stock actualizado
        con sus vínculos en `links`.
      operationId: PublicEcommerceOperationsController_linkStockBranches
      parameters:
        - name: id
          required: true
          in: path
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PublicStockBranchLinksDto'
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PublicProductStockDto'
      security:
        - PAT: []
components:
  schemas:
    PublicStockBranchLinksDto:
      type: object
      properties:
        source_branch_id:
          type: string
          description: Sucursal de origen cuyo stock se comparte
        branch_ids:
          description: >-
            Sucursales que venden con el stock del origen. Reemplaza las que
            tenía ese origen; vacío las desvincula todas
          type: array
          items:
            type: string
      required:
        - source_branch_id
        - branch_ids
    PublicProductStockDto:
      type: object
      properties:
        product_id:
          type: string
        variants:
          type: array
          items:
            $ref: '#/components/schemas/PublicStockVariantDto'
        links:
          description: Sucursales que venden este producto con el stock de otra sucursal
          type: array
          items:
            $ref: '#/components/schemas/PublicStockBranchLinkDto'
      required:
        - product_id
        - variants
        - links
    PublicStockVariantDto:
      type: object
      properties:
        variant_id:
          type: string
          nullable: true
        name:
          type: string
        total:
          type: number
          description: Stock total
        allocated:
          type: number
          description: Unidades asignadas a sucursales
        warehouse_available:
          type: number
          description: Unidades libres en bodega
        branches:
          type: array
          items:
            $ref: '#/components/schemas/PublicStockBranchDto'
      required:
        - variant_id
        - name
        - total
        - allocated
        - warehouse_available
        - branches
    PublicStockBranchLinkDto:
      type: object
      properties:
        branch_id:
          type: string
          description: Sucursal que vende con el stock de otra
        source_branch_id:
          type: string
          description: Sucursal de origen cuyo stock usa
      required:
        - branch_id
        - source_branch_id
    PublicStockBranchDto:
      type: object
      properties:
        branch_id:
          type: string
        quantity:
          type: number
      required:
        - branch_id
        - quantity
  securitySchemes:
    PAT:
      scheme: bearer
      bearerFormat: kbai_pk_<hex>
      type: http
      description: >-
        Personal Access Token con prefijo `kbai_pk_`. Generar desde el portal
        con permiso `developer.manage_tokens`.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.