---
title: "Shipping rules"
description: "Automate carrier and service selection with IF/THEN rules evaluated on every shipment."
---

Shipping rules automate decisions at shipment creation: choosing a carrier, pinning a service, auto-insuring, switching the label format, and more. They are evaluated in priority order, before quoting.

## Endpoints

| Method | Path | Description |
| --- | --- | --- |
| `GET` | `/v1/shipping-rules` | List rules (ordered by priority) |
| `POST` | `/v1/shipping-rules` | Create a rule |
| `GET` | `/v1/shipping-rules/:id` | Get a rule |
| `PUT` | `/v1/shipping-rules/:id` | Update a rule |
| `DELETE` | `/v1/shipping-rules/:id` | Delete (soft delete) |
| `PATCH` | `/v1/shipping-rules/reorder` | Bulk-update priorities |
| `POST` | `/v1/shipping-rules/preview` | Dry run: evaluate without persisting |

### Body parameters (create and update)

| Prop | Type | Default | Description |
| - | - | - | - |
| `priority` | `number` | - | Evaluation order (1–9999). Unique per organization. |
| `conditions` | `object` | - | all/any tree of { field, op, value } conditions. See the table below. |
| `actions` | `object[]` | - | Actions applied when the rule fires. See the actions table. |
| `name?` | `string` | - | A descriptive name for the rule. |
| `isActive?` | `boolean` | `true` | Inactive rules are not evaluated. |

```bash
curl -X POST https://api.sendit.mx/v1/shipping-rules \
  -H "X-API-Key: sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "DHL for heavy shipments to Jalisco",
    "priority": 10,
    "conditions": {
      "all": [
        { "field": "parcel.weight", "op": ">=", "value": 5 },
        { "field": "to.state", "op": "==", "value": "JAL" }
      ]
    },
    "actions": [{ "type": "select_carrier", "carrierCode": "DHL" }]
  }'
```

```json
{
  "success": true,
  "data": {
    "id": "clxrule1",
    "name": "DHL for heavy shipments to Jalisco",
    "priority": 10,
    "isActive": true,
    "conditions": { "all": [ { "field": "parcel.weight", "op": ">=", "value": 5 }, { "field": "to.state", "op": "==", "value": "JAL" } ] },
    "actions": [{ "type": "select_carrier", "carrierCode": "DHL" }],
    "createdAt": "2026-07-18T10:00:00.000Z"
  }
}
```

The sections below detail the `conditions` and `actions` syntax.

## Write conditions

Conditions nest with `all` (AND) and `any` (OR):

```json
{
  "all": [
    { "field": "parcel.weight", "op": ">=", "value": 5 },
    { "any": [
      { "field": "to.state", "op": "in", "value": ["CDMX", "JAL", "NLE"] },
      { "field": "to.isResidential", "op": "==", "value": false }
    ]}
  ]
}
```

### Available fields

| Field | Type | Description |
| --- | --- | --- |
| `parcel.weight` | number | Weight in kg |
| `parcel.length` / `width` / `height` | number | Dimensions in cm |
| `parcel.packagingType` | string | Packaging type |
| `to.country` | string | Destination country (ISO) |
| `to.state` | string | Destination state |
| `to.postalCode` | string | Destination postal code |
| `to.isResidential` | boolean | Residential delivery |
| `order.totalPrice` | number | Order total (MXN) |
| `order.channel` | string | `SHOPIFY`, `WOOCOMMERCE`, ... |
| `shipment.declaredValue` | number | Declared value |
| `shipment.isInternational` | boolean | Cross-border shipment |

### Operators

`==`, `!=`, `>`, `>=`, `<`, `<=`, `in`, `not_in`, `contains`, `starts_with`

## Define actions

| Action | Fields | Effect |
| --- | --- | --- |
| `select_carrier` | `carrierCode` | Prefer this carrier (no match → full list back) |
| `select_service` | `carrierCode`, `serviceCode` | Prefer a specific service (same tolerance) |
| `exclude_carrier` | `carrierCode` | Exclude a carrier (hard filter — can yield zero rates) |
| `add_insurance` | `declaredValue` | Auto-insure |
| `set_label_format` | `format` | Override the label format: `PDF`, `ZPL`, or `PNG` |
| `add_signature_required` | — | Require a delivery signature |
| `tag` | `tags: string[]` | Tag the shipment |

## Priority and cascading

Rules are evaluated in ascending `priority` order. **Every** matching rule fires, and later rules can override earlier ones:

```text
Priority 1: select_carrier DHL
Priority 2: select_service DHL EXPRESS   ← refines what priority 1 set
```

## Test before you activate

`preview` evaluates a hypothetical shipment without writing anything:

```bash
curl -X POST https://api.sendit.mx/v1/shipping-rules/preview \
  -H "X-API-Key: sk_test_..." \
  -H "Content-Type: application/json" \
  -d '{
    "shipment": {
      "parcel": { "weight": 8, "length": 40, "width": 30, "height": 20 },
      "to": { "country": "MX", "state": "JAL", "postalCode": "44100" },
      "shipment": { "declaredValue": 5000, "isInternational": false }
    }
  }'
```

```json
{
  "success": true,
  "data": {
    "finalActions": {
      "carrierCode": "DHL",
      "serviceCode": "EXPRESS"
    },
    "evaluations": [
      { "ruleId": "clxrule1", "fired": true, "actionsApplied": [{ "type": "select_carrier", "carrierCode": "DHL" }] },
      { "ruleId": "clxrule2", "fired": true, "actionsApplied": [{ "type": "select_service", "carrierCode": "DHL", "serviceCode": "EXPRESS" }] }
    ]
  }
}
```

`evaluations[]` shows the exact cascade: which rule fired, which conditions it evaluated, and which actions it applied. The same information is recorded on every real shipment for auditing.

## Limits and utilities

- Up to **100 non-deleted rules** per organization — disabled ones count too.
- Priority is unique per organization (1–9999).
- To skip rules on a shipment, create it with `"skipShippingRules": true`. That is a lifetime decision for that shipment, not a per-request switch.
- If a rule fails to evaluate (`SHIPPING_RULE_EVALUATION_FAILED`) or to persist (`SHIPPING_RULE_PERSISTENCE_FAILED`, in `error.details.code`), the creation rolls back before rating.
- `metadata.shippingRules` is reserved by the platform: sending it is rejected.
- A condition on absent context is never satisfied — not even with `!=` or `not_in`.
- `add_insurance` only fills what you omitted: an explicit `requestInsurance`/`declaredValue` (including `false`/`0`) always wins, and it does not guarantee coverage. Don't send `requestInsurance: false` by default — omit it.

## Rules and orders

Rules also evaluate when you bind a shipment to an order:

- `orderId` on `POST /v1/shipments` evaluates rules at creation (API keys need `orders:write` alongside `shipments:write`).
- `POST /v1/orders/:id/shipments` binds a `DRAFT` shipment to the order and **re-evaluates the rules**: it advances the quote revision, clears any selected `rateId`, and re-rates. A shipment with a purchased label or a purchase in flight is never re-evaluated — it only binds.
- Binding to the same order again is a no-op; moving to another order is rejected.
- If the order changed meanwhile, `412 PRECONDITION_FAILED` — reload and retry.
- Rule conditions only see amounts in MXN; an order in another currency does not satisfy them.
- A return shipment takes its own `orderId` — it does not inherit the original's.
