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

# Create Rule

> Create a new routing rule for a link

## Request Body

<ParamField body="linkId" type="string" required>
  The link to attach this rule to
</ParamField>

<ParamField body="name" type="string" required>
  Display name for the rule
</ParamField>

<ParamField body="destinationUrl" type="string" required>
  URL to redirect when rule matches
</ParamField>

<ParamField body="conditions" type="array" required>
  Array of condition objects
</ParamField>

<ParamField body="conditionLogic" type="string" default="AND">
  How to combine conditions: `AND` or `OR`
</ParamField>

<ParamField body="priority" type="number">
  Evaluation order (lower = higher priority)
</ParamField>

<ParamField body="enabled" type="boolean" default="true">
  Whether the rule is active
</ParamField>

<ParamField body="description" type="string">
  Optional description
</ParamField>

## Condition Object

Each condition has the following structure:

| Field      | Type         | Description                 |
| ---------- | ------------ | --------------------------- |
| `field`    | string       | Targeting field to evaluate |
| `operator` | string       | Comparison operator         |
| `value`    | string/array | Value(s) to compare against |

### Available Fields

#### Location

`country`, `region`, `city`, `timezone`, `isp`, `asn`, `latitude`, `longitude`

#### Device

`deviceType`, `os`, `osVersion`, `browser`, `browserVersion`, `deviceBrand`

#### Network

`vpn`, `proxy`, `tor`, `datacenter`, `bot`, `isCrawler`

#### Traffic

`referrer`, `referrerDomain`, `utmSource`, `utmMedium`, `utmCampaign`, `utmTerm`, `utmContent`

#### Time

`hour`, `dayOfWeek`

#### Visitor

`isNewVisitor`, `isReturning`

### Operators

| Operator       | Description            | Example                         |
| -------------- | ---------------------- | ------------------------------- |
| `equals`       | Exact match            | `country equals "US"`           |
| `not_equals`   | Does not match         | `country not_equals "CN"`       |
| `contains`     | String contains        | `referrer contains "google"`    |
| `not_contains` | String doesn't contain | `referrer not_contains "spam"`  |
| `in`           | Value in array         | `country in ["US", "CA", "UK"]` |
| `not_in`       | Value not in array     | `country not_in ["CN", "RU"]`   |
| `greater_than` | Numeric comparison     | `hour greater_than 17`          |
| `less_than`    | Numeric comparison     | `hour less_than 9`              |
| `is_true`      | Boolean true           | `vpn is_true`                   |
| `is_false`     | Boolean false          | `bot is_false`                  |

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.linquid.io/api/rules \
    -H "X-API-Key: lw_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "linkId": "link_abc123",
      "name": "Mobile iOS Users in US",
      "destinationUrl": "https://apps.apple.com/app/myapp",
      "conditions": [
        {
          "field": "deviceType",
          "operator": "equals",
          "value": "mobile"
        },
        {
          "field": "os",
          "operator": "equals",
          "value": "iOS"
        },
        {
          "field": "country",
          "operator": "equals",
          "value": "US"
        }
      ],
      "conditionLogic": "AND",
      "priority": 1
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.linquid.io/api/rules', {
    method: 'POST',
    headers: {
      'X-API-Key': 'lw_your_api_key_here',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      linkId: 'link_abc123',
      name: 'Mobile iOS Users in US',
      destinationUrl: 'https://apps.apple.com/app/myapp',
      conditions: [
        { field: 'deviceType', operator: 'equals', value: 'mobile' },
        { field: 'os', operator: 'equals', value: 'iOS' },
        { field: 'country', operator: 'equals', value: 'US' },
      ],
      conditionLogic: 'AND',
      priority: 1,
    }),
  });
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.linquid.io/api/rules',
      headers={
          'X-API-Key': 'lw_your_api_key_here',
          'Content-Type': 'application/json',
      },
      json={
          'linkId': 'link_abc123',
          'name': 'Mobile iOS Users in US',
          'destinationUrl': 'https://apps.apple.com/app/myapp',
          'conditions': [
              {'field': 'deviceType', 'operator': 'equals', 'value': 'mobile'},
              {'field': 'os', 'operator': 'equals', 'value': 'iOS'},
              {'field': 'country', 'operator': 'equals', 'value': 'US'},
          ],
          'conditionLogic': 'AND',
          'priority': 1,
      }
  )
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "success": true,
    "data": {
      "id": "rule_def456",
      "linkId": "link_abc123",
      "name": "Mobile iOS Users in US",
      "destinationUrl": "https://apps.apple.com/app/myapp",
      "conditions": [
        { "field": "deviceType", "operator": "equals", "value": "mobile" },
        { "field": "os", "operator": "equals", "value": "iOS" },
        { "field": "country", "operator": "equals", "value": "US" }
      ],
      "conditionLogic": "AND",
      "priority": 1,
      "enabled": true,
      "createdAt": "2024-01-15T10:30:00.000Z"
    }
  }
  ```
</ResponseExample>
