# Rules

Rules are defined within a JSON payload as an array of rule objects under the `rules` key. For each rule you must provide at least name, one or more conditions that have to be satisfied for the rule to match, and one or more actions that will be applied if the conditions are met:

<pre class="language-json"><code class="lang-json">{
<strong>  "rules": [
</strong>    {
<strong>      "name": "First rule",
</strong>      ...
<strong>      "conditions": [
</strong>        { ... },
        { ... }
      ],
<strong>      "actions": [
</strong>        { ... },
        { ... },
        { ... }
      ]
    },
    {
<strong>      "name": "Second rule",
</strong>      ...
<strong>      "conditions": [
</strong>        { ... },
        { ... },
        { ... }
      ],
<strong>      "actions": [
</strong>        { ... },
        { ... }
      ]
    },
    { ... }
  ]
}
</code></pre>

Other optional rule's parameters (such as a unique identifier and the logic that you want to use to evaluate multiple rules) can be also specified:

<table><thead><tr><th width="218">Key</th><th width="140">Type<select><option value="cllRZOG270Ox" label="Integer" color="blue"></option><option value="Yciueb95hg7a" label="Boolean" color="blue"></option><option value="WW3yIoBGM4uH" label="String" color="blue"></option><option value="hfhZmN57GY5L" label="Array" color="blue"></option><option value="IU9cves6IuT9" label="Object" color="blue"></option></select></th><th width="120" data-type="checkbox">Required</th><th width="225">Description</th><th>Example</th></tr></thead><tbody><tr><td><strong><code>id</code></strong></td><td><span data-option="WW3yIoBGM4uH">String</span></td><td>false</td><td>The rule's identifier (we recommend using <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">UUIDs</a>, if not specified will be automatically generated and added by the <a href="/pages/76teVgpOQmx0z10k1QTC#core-api-integration">Core API integration</a>).</td><td><code>"abcd-1234-xyzk-5678"</code></td></tr><tr><td><strong><code>name</code></strong></td><td><span data-option="WW3yIoBGM4uH">String</span></td><td>true</td><td>The rule's name (we recommend adding all essential information to help easily identify it among others).</td><td><code>"10% discount on tagged orders"</code></td></tr><tr><td><strong><code>priority</code></strong></td><td><span data-option="cllRZOG270Ox">Integer</span></td><td>false</td><td>The order in which the rules are evaluated (the lower the number, the higher the priority).</td><td><code>2</code></td></tr><tr><td><strong><code>conditions_logic</code></strong></td><td><span data-option="WW3yIoBGM4uH">String</span></td><td>false</td><td>The logic according to which the conditions are evaluated to determine the overall match. Can be one of <code>and</code> or <code>or</code>, default is <code>and</code>.</td><td><code>or</code></td></tr><tr><td><strong><code>conditions</code></strong></td><td><span data-option="hfhZmN57GY5L">Array</span></td><td>true</td><td>The conditions that must be satisfied for the rule to match.</td><td>Learn more <a href="/pages/Pm9784Fg2yQzMG9qkUCo">here</a>.</td></tr><tr><td><strong><code>actions</code></strong></td><td><span data-option="hfhZmN57GY5L">Array</span></td><td>true</td><td>The actions that will be applied if the conditions are met.</td><td>Learn more <a href="/pages/GJppZuqeEmFNv7OXixe6">here</a>.</td></tr></tbody></table>

{% hint style="info" %}
In case of multiple rules,  all and only the actions belonging to the matching rules will be applied.  If no `priority` is defined, the rules are applied in the order that they are listed in the `rules` array (starting from priority `0`).
{% endhint %}

{% hint style="warning" %}
For performance reasons, the maximum number of rules within a rules payload is **10**.
{% endhint %}

## Example

Let's consider a combination of rules that implements a quite complex promotional logic. Based on the order payload checked against them the rules application may result in different outcomes. Some of them are analyzed here below.

{% tabs %}
{% tab title="Rules" %}

1. The first rule contains two conditions and one action to apply a 2500 cents discount per unit to all the products in the order that costs more than 9900 cents if the order contains such kind of products and the total order's amount is at least 50000 cents.&#x20;
2. The second rule contains one condition and two actions to apply an additional 15% discount on all the products in the order and offer free shipping if the email address of the customer who placed the order belongs to a specified domain.

<pre class="language-json"><code class="lang-json"><strong>{
</strong>  "rules": [
    {
      "name": "Get 2500 cents off item cost based on items price or order total amount",
<strong>      "conditions": [
</strong>        {
          "field": "order.line_items.unit_amount_cents",
          "matcher": "gt",
          "value": 9900,
          "group": "discountable-items"
        },
        {
          "field": "order.total_amount_cents",
          "matcher": "gteq",
          "value": 50000
        }
      ],
<strong>      "actions": [
</strong>        {
          "type": "fixed_amount",
          "value": 2500,
          "selector": "order.line_items.sku",
          "groups": [ "discountable-items" ]
        }
      ]
    },
    {
      "name": "Get 15% off item cost plus free shipping for company customers",
<strong>      "conditions": [
</strong>        {
          "field": "order.customer_email",
          "matcher": "matches",
          "value": ".*@mybrand.com"
        }
      ],
<strong>      "actions": [
</strong>        {
          "type": "percentage",
          "selector": "order.line_items.sku",
          "value": 0.15
        },
        {
          "type": "percentage",
          "selector": "order.line_items.shipment",
          "value": 1
        }
      ]
    }
  ]
}
</code></pre>

{% endtab %}
{% endtabs %}

### All rules match

{% tabs %}
{% tab title="Order" %}
This order contains at least one SKU that costs more than 9900 cents, the order total amount is greater than or equal to 50000 cents, and the associated customer email matches the specified domain:

<pre class="language-json"><code class="lang-json">{
  "order": {
    "id": "oXkhYLlzgE",
<strong>    "customer_email": "john@mybrand.com",
</strong><strong>    "total_amount_cents": 66000,
</strong>    "line_items": [
      {
        "id": "dKdhYLlzgE",
        "quantity": 1,
<strong>        "unit_amount_cents": 15000,
</strong>        "sku": { "id": "dKfhgdlzgE" }
      },
      {
        "id": "eKfhYFkztQ",
        "quantity": 2,
        "unit_amount_cents": 5000,
        "sku": { "id": "sDfhYFkcfR" }
      },
      {
        "id": "kKffYAkzdW",
        "quantity": 2,
<strong>        "unit_amount_cents": 20000,
</strong>        "sku": { "id": "sWfhYDccwQ" }
      },
      {
        "id": "adfSYwAzar",
        "quantity": 1,
        "unit_amount_cents": 1000,
        "shipment": { "id": "dwFhYDGtwE" }
      }
    ]
  }
}
</code></pre>

{% endtab %}

{% tab title="Outcomes" %}
Both rules match and all the three actions will be applied, meaning that:

* The identified discountable items will be discounted by 2500 cents per unit based on the first rule. An additional 15% discount due to the second rule will be also applied to those items.
* All the other line items in the order associated with an SKU (if any) will be discounted by 15%.
* All the shipment costs will be zeroed by applying a 100% percentage discount.

<pre class="language-json"><code class="lang-json">[
  {
    "id": "g2584766-ca17-4666-90b6-866a115bacgd",
    "name": "Get 2500 cents off item cost based on items price or order total amount",
<strong>    "priority": 0,
</strong><strong>    "match": true,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.line_items.unit_amount_cents",
        "matcher": "gt",
        "value": 9900,
        "group": "discountable-items",
<strong>        "match": true,
</strong>        "matches": [
          {
            "order": "oXkhYLlzgE",
<strong>            "line_item": "dKdhYLlzgE",
</strong>            "group": "discountable-items"
          },
          {
            "order": "oXkhYLlzgE",
<strong>            "line_item": "kKffYAkzdW",
</strong>            "group": "discountable-items"
          }
        ],
        "scope": "any"
      },
      {
        "field": "order.total_amount_cents",
        "matcher": "gteq",
        "value": 50000,
        "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40",
<strong>        "match": true,
</strong>        "matches": [ 
          { 
<strong>            "order": "oXkhYLlzgE",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40"
          } 
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "dKdhYLlzgE",
</strong>            "group": "discountable-items",
            "quantity": 1,
<strong>            "value": 2500,
</strong><strong>            "action_type": "fixed_amount"
</strong>          },
          {
            "resource_type": "line_items",
<strong>            "id": "kKffYAkzdW",
</strong>            "group": "discountable-items",
            "quantity": 2,
<strong>            "value": 2500,
</strong><strong>            "action_type": "fixed_amount"
</strong>          }
        ]
      }
    ]
  },
  {
    "id": "a1234567-ce16-3234-90k2-566a465bachy",
    "name": "Get 15% off item cost plus free shipping for company customers",
<strong>    "priority": 1,
</strong><strong>    "match": true,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@mybrand.com",
        "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40
<strong>        "match": true,
</strong>        "matches": [
          { 
<strong>            "order": "oXkhYLlzgE",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40"
          }
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "dKdhYLlzgE",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40",
            "quantity": 1,
<strong>            "value": 0.15,
</strong><strong>            "action_type": "percentage"
</strong>          },
          {
            "resource_type": "line_items",
<strong>            "id": "eKfhYFkztQ",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40",
            "quantity": 2,
<strong>            "value": 0.15,
</strong><strong>            "action_type": "percentage"
</strong>          },
          {
            "resource_type": "line_items",
<strong>            "id": "kKffYAkzdW",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40",
            "quantity": 2,
<strong>            "value": 0.15,
</strong><strong>            "action_type": "percentage"
</strong>          }
        ]
      },
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "adfSYwAzar",
</strong>            "group": "1cd66bb2-56d9-44ac-805d-33d29d56fe40",
            "quantity": 1,
<strong>            "value": 1.0,
</strong><strong>            "action_type": "percentage"
</strong>          }
        ]
      }
    ]
  }
]
</code></pre>

{% hint style="info" %}
As you can see, since no priority was defined at the rules level, the `priority` attribute in the results follows the default behavior and matches the order in which the rules are listed in the original `rules` payload.
{% endhint %}
{% endtab %}
{% endtabs %}

### Only the first rule matches

{% tabs %}
{% tab title="Order" %}
This order contains at least one SKU that costs more than 9900 cents, the order total amount is greater than or equal to 50000 cents, but the associated customer email doesn't match the specified domain:

<pre class="language-json"><code class="lang-json">{
  "order": {
    "id": "oXkhYLlzgE",
<strong>    "customer_email": "jane@yourbrand.com",
</strong><strong>    "total_amount_cents": 66000,
</strong>    "line_items": [
      {
        "id": "dKdhYLlzgE",
        "quantity": 1,
<strong>        "unit_amount_cents": 15000,
</strong>        "sku": { "id": "dKfhgdlzgE" }
      },
      {
        "id": "eKfhYFkztQ",
        "quantity": 2,
        "unit_amount_cents": 5000,
        "sku": { "id": "sDfhYFkcfR" }
      },
      {
        "id": "kKffYAkzdW",
        "quantity": 2,
<strong>        "unit_amount_cents": 20000,
</strong>        "sku": { "id": "sWfhYDccwQ" }
      },
      {
        "id": "adfSYwAzar",
        "quantity": 1,
        "unit_amount_cents": 1000,
        "shipment": { "id": "dwFhYDGtwE" }
      }
    ]
  }
}
</code></pre>

{% endtab %}

{% tab title="Outcomes" %}
Only the first rule matches, meaning that:

* The identified discountable items will be discounted by 2500 cents per unit.
* No other additional discount will be applied.

<pre class="language-json"><code class="lang-json">[
  {
    "id": "g2584766-ca17-4666-90b6-866a115bacgd",
    "name": "Get 2500 cents off item cost based on items price or order total amount",
<strong>    "priority": 0,
</strong><strong>    "match": true,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.line_items.unit_amount_cents",
        "matcher": "gt",
        "value": 9900,
        "group": "discountable-items",
<strong>        "match": true,
</strong>        "matches": [
          {
            "order": "oXkhYLlzgE",
<strong>            "line_item": "dKdhYLlzgE"
</strong>            "group": "discountable-items"
          },
          {
            "order": "oXkhYLlzgE",
<strong>            "line_item": "kKffYAkzdW"
</strong>            "group": "discountable-items"
          }
        ],
        "scope": "any"
      },
      {
        "field": "order.total_amount_cents",
        "matcher": "gteq",
        "value": 50000,
        "group": "0fe4c17c-fbf2-4418-9a28-0cd02e55916a",
<strong>        "match": true,
</strong>        "matches": [ 
          { 
<strong>            "order": "oXkhYLlzgE",
</strong>            "group": "0fe4c17c-fbf2-4418-9a28-0cd02e55916a" 
          } 
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "dKdhYLlzgE",
</strong>            "group": "discountable-items",
            "quantity": 1,
<strong>            "value": 2500,
</strong><strong>            "action_type": "fixed_amount"
</strong>          },
          {
            "resource_type": "line_items",
<strong>            "id": "kKffYAkzdW",
</strong>            "group": "discountable-items",
            "quantity": 2,
<strong>            "value": 2500,
</strong><strong>            "action_type": "fixed_amount"
</strong>          }
        ]
      }
    ]
  },
  {
    "id": "a1234567-ce16-3234-90k2-566a465bachy",
    "name": "Get 15% off item cost plus free shipping for company customers",
<strong>    "priority": 1,
</strong><strong>    "match": false,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@mybrand.com",
        "group": "0fe4c17c-fbf2-4418-9a28-0cd02e55916a",
<strong>        "match": false,
</strong><strong>        "matches": [],
</strong>        "scope": "any"
      }
    ],
<strong>    "actions": []
</strong>  }
]
</code></pre>

{% hint style="info" %}
As you can see, the second rule's `actions` array is empty, which implies that the rule triggers no action since the related condition's `matches` array is empty (meaning that the condition is not satisfied).
{% endhint %}
{% endtab %}
{% endtabs %}

### Only the second rule matches

{% tabs %}
{% tab title="Order" %}
The customer email associated to this order matches the specified domain, the order contains at least one SKU that costs more than 9900 cents but the order total amount is lower than 50000 cents:

<pre class="language-json"><code class="lang-json">{
  "order": {
    "id": "oXkhYLlzgE",
<strong>    "customer_email": "john@mybrand.com",
</strong><strong>    "total_amount_cents": 26000,
</strong>    "line_items": [
      {
        "id": "dKdhYLlzgE",
        "quantity": 1,
<strong>        "unit_amount_cents": 15000,
</strong>        "sku": { "id": "dKfhgdlzgE" }
      },
      {
        "id": "eKfhYFkztQ",
        "quantity": 2,
        "unit_amount_cents": 5000,
        "sku": { "id": "sDfhYFkcfR" }
      },
      {
        "id": "adfSYwAzar",
        "quantity": 1,
        "unit_amount_cents": 1000,
        "shipment": { "id": "dwFhYDGtwE" }
      }
    ]
  }
}
</code></pre>

{% endtab %}

{% tab title="Outcomes" %}
Only the second rule matches, meaning that:

* All the line items in the order associated with an SKU will be discounted by 15%.
* All the shipment costs will be zeroed by applying a 100% percentage discount.

<pre class="language-json"><code class="lang-json">[
  {
    "id": "g2584766-ca17-4666-90b6-866a115bacgd",
    "name": "Get 2500 cents off item cost based on items price or order total amount",
<strong>    "priority": 0,
</strong><strong>    "match": false,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.line_items.unit_amount_cents",
        "matcher": "gt",
        "value": 9900,
        "group": "discountable-items",
<strong>        "match": true,
</strong>        "matches": [
          {
            "order": "oXkhYLlzgE",
<strong>            "line_item": "dKdhYLlzgE",
</strong>            "group": "discountable-items"
          }
        ],
        "scope": "any"
      },
      {
        "field": "order.total_amount_cents",
        "matcher": "gteq",
        "value": 50000,
        "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece",
<strong>        "match": false,
</strong><strong>        "matches": [],
</strong>        "scope": "any"
      }
    ],
<strong>    "actions": []
</strong>  },
  {
    "id": "a1234567-ce16-3234-90k2-566a465bachy",
    "name": "Get 15% off item cost plus free shipping for company customers",
<strong>    "priority": 1,
</strong><strong>    "match": true,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@mybrand.com",
        "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece",
<strong>        "match": true,
</strong>        "matches": [
          { 
<strong>            "order": "oXkhYLlzgE",
</strong>            "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece"
          }
        ],
        "scope": "any"
      }
    ],
    "actions": [
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "dKdhYLlzgE",
</strong>            "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece",
            "quantity": 1,
<strong>            "value": 0.15,
</strong><strong>            "action_type": "percentage"
</strong>          },
          {
            "resource_type": "line_items",
<strong>            "id": "eKfhYFkztQ",
</strong>            "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece",
            "quantity": 2,
<strong>            "value": 0.15,
</strong><strong>            "action_type": "percentage"
</strong>          }
        ]
      },
      {
        "resources": [
          {
            "resource_type": "line_items",
<strong>            "id": "adfSYwAzar",
</strong>            "group": "f8e14d34-08ee-4209-98b0-20b1755a3ece",
            "quantity": 1,
<strong>            "value": 1.0,
</strong><strong>            "action_type": "percentage"
</strong>          }
        ]
      }
    ]
  }
]
</code></pre>

{% hint style="info" %}
As you can see, the first rule's `actions` array is empty, which implies that the rule triggers no action since one of the related conditions' `matches` array is empty (meaning that the overall combination of conditions is not satisfied).
{% endhint %}
{% endtab %}
{% endtabs %}

### No rules match

{% tabs %}
{% tab title="Order" %}
The customer email associated with this order doesn't match the specified domain, the order total amount is greater than or equal to 50000 cents but the order doesn't contain at least one SKU that costs more than 9900 cents:

<pre class="language-json"><code class="lang-json">{
  "order": {
    "id": "oXkhYLlzgE",
<strong>    "customer_email": "jane@yourbrand.com",
</strong><strong>    "total_amount_cents": 58000,
</strong>    "line_items": [
      {
        "id": "dKdhYLlzgE",
        "quantity": 5,
        "unit_amount_cents": 2000,
        "sku": { "id": "dKfhgdlzgE" }
      },
      {
        "id": "eKfhYFkztQ",
        "quantity": 4,
        "unit_amount_cents": 5000,
        "sku": { "id": "sDfhYFkcfR" }
      },
      {
        "id": "kKffYAkzdW",
        "quantity": 3,
        "unit_amount_cents": 9000,
        "sku": { "id": "sWfhYDccwQ" }
      },
      {
        "id": "adfSYwAzar",
        "quantity": 1,
        "unit_amount_cents": 1000,
        "shipment": { "id": "dwFhYDGtwE" }
      }
    ]
  }
}
</code></pre>

{% endtab %}

{% tab title="Outcomes" %}
None of the two rules matches, so no discount will be applied.

<pre class="language-json"><code class="lang-json">[
  {
    "id": "g2584766-ca17-4666-90b6-866a115bacgd",
    "name": "Get 2500 cents off item cost based on items price or order total amount",
    "priority": 0,
<strong>    "match": false,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.line_items.unit_amount_cents",
        "matcher": "gt",
        "value": 9900,
        "group": "discountable-items",
<strong>        "match": false,
</strong><strong>        "matches": [],
</strong>        "scope": "any"
      },
      {
        "field": "order.total_amount_cents",
        "matcher": "gteq",
        "value": 50000,
        "group": "473aa43c-ae1e-4034-9a21-7cddd95b85ab",
<strong>        "match": true,
</strong>        "matches": [
          { 
<strong>            "order": "oXkhYLlzgE",
</strong>            "group": "473aa43c-ae1e-4034-9a21-7cddd95b85ab"
          }
        ],
        "scope": "any"
      }
    ],
<strong>    "actions": []
</strong>  },
  {
    "id": "a1234567-ce16-3234-90k2-566a465bachy",
    "name": "Get 15% off item cost plus free shipping for company customers",
    "priority": 1,
<strong>    "match": false,
</strong>    "conditions_logic": "and",
    "conditions": [
      {
        "field": "order.customer_email",
        "matcher": "matches",
        "value": ".*@mybrand.com",
        "group": "473aa43c-ae1e-4034-9a21-7cddd95b85ab",
<strong>        "match": false,
</strong><strong>        "matches": [],
</strong>        "scope": "any"
      }
    ],
<strong>    "actions": []
</strong>  }
]
</code></pre>

{% hint style="info" %}
As you can see, both rules' `actions` array is empty, which implies that they trigger no action since the related conditions' `matches` arrays are empty (meaning that the conditions are not satisfied).
{% endhint %}
{% endtab %}
{% endtabs %}

{% hint style="success" %}
Keep reading to learn more about how to set [conditions](/rules-engine/conditions.md) and [actions](/rules-engine/actions.md) for a a rule or feel free to browse the [use cases](/rules-engine/use-cases/promotions.md) section to check more examples of rules based on real ecommerce scenarios.
{% endhint %}

{% content-ref url="/pages/8lGakTDxR9sCgQheVzzS" %}
[Use cases](/rules-engine/use-cases/promotions.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.commercelayer.io/rules-engine/rules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
