> For the complete documentation index, see [llms.txt](https://lmile.gitbook.io/lmile-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lmile.gitbook.io/lmile-docs/routing-api/cancel-update.md).

# Cancel / Update — 取消・更新

確定済みの受注ルーティング（[Routing](/lmile-docs/routing-api/orders.md)）に対する **取消（cancel）** と **更新（update）**。引き当て先の各在庫ロケーションへ、それぞれ専用の **action 別 dispatch 設定**（[Dispatch Config](/lmile-docs/configuration/dispatch-config.md) の `action: "cancel" / "update"`）で WMS へ通知します。

> **Base URL** `https://api.lmile.io` · **認証** `x-ingest-secret` ヘッダー（[詳細](/lmile-docs/getting-started/authentication.md)） · `shop` はリクエストボディに含めます（省略するとキーのアカウント）

> dispatch のアクション種別: **create**（出荷指示＝通常の `/orders`・既定）/ **update**（注文更新）/ **cancel**（注文取消）。location ごとに action 別の宛先・テンプレートを登録できます。設定の無い `(location, action)` は送信を skip します。

## エンドポイント

| Method | Path                  | 用途                                       |
| ------ | --------------------- | ---------------------------------------- |
| `POST` | `/api/routing/update` | 注文を最新内容で再ルーティング → `update` を dispatch    |
| `POST` | `/api/routing/cancel` | 注文を `cancelled` にし → `cancel` を dispatch |

***

## `POST` /api/routing/update

カート変更（SKU 追加・数量変更・宛先/ tag 変更）を**全量の最新内容**で受け直し、同一出荷元内で再ルーティングしてから各在庫ロケーションへ `update` を dispatch します。ボディは [`/api/routing/orders`](/lmile-docs/routing-api/orders.md) と同一契約（`tag` / `shipTo` / `fulfillmentSourceLocationId` / 任意 `inventoryLocationId` / `lines`）。

### リクエスト

```http
POST https://api.lmile.io/api/routing/update
x-ingest-secret: {INGEST_SECRET}
Content-Type: application/json
```

```json
{
  "shop": "demo-store.myshopify.com",
  "sourceSystem": "shopify",
  "externalOrderRef": "1001",
  "fulfillmentSourceLocationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "tag": "express",
  "shipTo": { "name": "山田 太郎", "prefecture": "東京都", "address1": "道玄坂1-2-3" },
  "lines": [ { "barcode": "1234567890123", "quantity": 3 } ]
}
```

* 既存の routing order が前提（無ければ `404`）。`routingOrderId` を併記すればそれで特定、無ければ `sourceSystem + externalOrderRef`。
* `cancelled` 済の注文は更新不可（`409`）。
* 明細単位の `properties`（[Routing](/lmile-docs/routing-api/orders.md) の `lines[].properties`）も全量の最新内容で受け直し、再ルーティング後の decision / unit / 出荷指示に反映します（同一 `barcode` 複数明細のグルーピングも `/orders` と同じ挙動）。
* 出荷指示の冪等キーは元注文の `idempotencyKey` を維持（create→update で同一行を更新）。
* レスポンスは `/orders` と同形（`status` / `shippingInstruction` / `dispatches[]`）。`dispatches` は `update` 設定で送信。

## `POST` /api/routing/cancel

注文を `cancelled` にし、引き当て済の各在庫ロケーションへ `cancel` を dispatch します。あわせて内部の出荷状態（出荷元ヘッダ `lm_fulfillment_shipments` / 単位明細 `lm_fulfillment_units` / 出荷指示 `lm_shipping_instructions`）も `cancelled` に同期します。

### リクエスト

```json
{
  "shop": "demo-store.myshopify.com",
  "routingOrderId": "0f1e2d3c-4b5a-6789-0abc-def123456789"
}
```

| パラメータ                               | 型             |  必須 | 説明                                                |
| ----------------------------------- | ------------- | :-: | ------------------------------------------------- |
| `shop`                              | string        |     | 店舗ドメイン（省略するとキーのアカウント）                             |
| `routingOrderId`                    | string (uuid) |  △  | 対象注文。無ければ `sourceSystem` + `externalOrderRef` で特定 |
| `sourceSystem` / `externalOrderRef` | string        |  △  | `routingOrderId` 未指定時に必須                          |

* **冪等**: 既に `cancelled` なら再 dispatch / 再 cascade せず `idempotent: true` を返す。
* 取消は **SoT 側で常に確定**（`status=cancelled`）。WMS への `cancel` 通知は best-effort（失敗は `dispatches[]` に記録、注文取消自体は確定）。
* **内部 SoT へ cascade**: `lm_fulfillment_shipments` / `lm_fulfillment_units` を `status='cancelled'`（+ `cancelled_at`）に、`lm_shipping_instructions` を `delivery_status='cancelled'` に更新します。**`shipped`（出荷完了）は終端として保護**し上書きしません（追跡番号・ラベル PDF は監査用に温存）。在庫数量（`inventory_levels`）は「状態のみ」方針のため変更しません。
* 取消時の cascade 件数はレスポンスの `cancelledShipments` / `cancelledUnits` / `shippingInstructionCancelled` に返します。

### レスポンス

`update` / `cancel` 共通で `ok` / `routingOrderId` / `status` / `shippingInstruction` / `dispatches[]` を返します。`cancel` はさらに cascade 結果（`cancelledShipments` / `cancelledUnits` / `shippingInstructionCancelled`）を返します。

```json
{ "ok": true, "routingOrderId": "...", "status": "cancelled",
  "shippingInstruction": { "...": "..." },
  "dispatches": [ { "locationId": "...", "status": "sent", "httpStatus": 200, "attempts": 1 } ],
  "cancelledShipments": 2, "cancelledUnits": 3, "shippingInstructionCancelled": true }
```

***

## エラー

エンドポイント固有:

| Status | 条件                                           | レスポンス例                                                                                              |
| :----: | -------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|  `400` | 注文特定情報なし / (update)バリデーション失敗                 | `{ "ok": false, "error": "..." }`                                                                   |
|  `404` | 注文が存在しない                                     | `{ "ok": false, "error_kind": "not_found", "error": "routing order not found" }`                    |
|  `409` | 取消済の注文を update                               | `{ "ok": false, "error_kind": "order_cancelled", "error": "cancelled order cannot be updated" }`    |
|  `422` | (update) 出荷元未解決 / メンバー無し / 指定 location 非メンバー | `fulfillment_source_unresolved` / `fulfillment_source_no_members` / `location_not_member_of_source` |

共通エラー（`401` / `400` / `403` / `405` / `500` / `503`）は [Error Codes](/lmile-docs/reference/error-codes.md) を参照。

## データモデル

* `sot.lm_routing_orders.status` に `cancelled` を追加。
* `sot.lm_location_dispatch_config.action`（`create` / `update` / `cancel`、UNIQUE `(shop, location_id, tag, action)`）。
* `sot.lm_location_dispatch_log.action` で送信ごとの action を記録。
* `sot.lm_fulfillment_shipments.status` / `sot.lm_fulfillment_units.status` に `cancelled` を追加（+ `cancelled_at` 列）。集計 view `lm_v_routing_order_fulfillment`（`cancelled_count`）/ `lm_v_routing_order_unit_fulfillment`（`cancelled_unit_count`）に `cancelled` 分類を追加。
* `sot.lm_shipping_instructions.delivery_status` に `cancelled` を追加。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://lmile.gitbook.io/lmile-docs/routing-api/cancel-update.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
