Check and validation

How to use the public endpoint to check and validate a rule against a specific payload

If you need to check and validate a rule against a specific payload, you can leverage the public endpoint:

https://{{your_domain}}.commercelayer.io/api/rules/check

Where {{your_domain}} is the unique subdomain of a Commerce Layer organization for which the Rules Engine is enabled.

To do that you just need to send a POST request using a sales channel or integration access token and passing the rules you want to check along with the payloads you want the rules to be checked against:

Key
Type
Description

payload

Array

An array of objects containing the resource payload(s) you want the rules to be checked against.

rules

Array

An array of objects containing the rules you want to check against the payload.

You'll get in the response all the rules outcomes information, including:

  • The overall matching result of the rules.

  • The list of all the conditions matchers.

  • The list of single resources on which the related actions will be applied.

  • Any errors.

... and more.

Example

Check and outcomes

The following request checks the outcomes of a rule that should apply a 3% discount to the total amount of the orders paid by credit card against two different order payloads:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/api/rules/check' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "payload": [
      {
        "order": {
          "id": "oKkhYLlzgE",
          "payment_method": {
            "id": "gnYtPpKLeG",
            "payment_source_type": "credit_cards"
          }
        }
      },
      {
        "order": {
          "id": "DgkhGJlzgf",
          "payment_method": {
                    "id": "snXtWdKLdC",
                    "payment_source_type": "wire_transfers"
                }
            }
        }
    ],
    "rules": [
      {
        "name": "Discount 3% if paid by credit card",
        "conditions": [
          {
            "field": "order.payment_method.payment_source_type",
            "matcher": "eq",
            "value": "credit_cards",
            "group": "discountable-orders"
          }
        ],
        "actions": [
          {
            "type": "percentage",
            "selector": "order",
            "value": 0.03,
            "groups": [ "discountable-orders" ]
          }
        ]
      }
    ]
  }'

Validation and errors

The following request checks the outcomes of a couple of rules that should apply a 3% discount to the total amount of the orders paid by credit card and a 1000 cents fixed amount discount to the total amount of the orders that have more than two SKUs, but the rule payloads contain some errors:

  • The first rule's field is payment_source (a wrong attribute that doesn't exist in the Core API's payment method resource object) instead of payment_source_type.

  • The format of the percentage discount value in the first rule is incorrect.

  • The type of action in the second rule is fixed (which doesn't exist among the available action types) instead of fixed_amount.

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.io/api/rules/check' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "payload": [
      {
        "order": {
          "id": "oKkhYLlzgE",
          "payment_method": {
            "id": "gnYtPpKLeG",
            "payment_source_type": "credit_cards"
          }
        }
      },
      {
        "order": {
          "id": "DgkhGJlzgf",
          "payment_method": {
            "id": "snXtWdKLdC",
            "payment_source_type": "wire_transfers"
          }
        }
      }
    ],
    "rules": [
      {
        "name": "Discount 3% if paid by credit card",
        "conditions": [
          {
            "field": "order.payment_method.payment_source",
            "matcher": "eq",
            "value": "credit_cards",
            "group": "paid-by-cc"
          }
        ],
        "actions": [
          {
            "type": "percentage",
            "selector": "order",
            "value": "3%",
            "groups": [ "paid-by-cc" ]
          }
        ]
      },
      {
        "name": "Discount 1000 cents if order has > 2 items",
        "conditions": [
          {
            "field": "order.skus_count",
            "matcher": "gt",
            "value": 2,
            "group": "min-num-items"
          }
        ],
        "actions": [
          {
            "type": "fixed",
            "selector": "order",
            "value": 1000,
            "groups": [ "min-num-items" ]
          }
        ]
      }
    ]
  }'

Last updated