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

# Routing Rules

> Dynamic logic that controls what happens when a node is scanned.

Routing rules let you change a node's behavior **without modifying the node itself**. Each node can have multiple rules evaluated top-to-bottom by priority. The first matching rule wins.

If no rule matches, the node's base payload is used as a fallback.

## Rule Types

<Tabs>
  <Tab title="Time-Based" icon="clock">
    Route based on time of day and day of week. Perfect for office hours vs. after-hours destinations.

    ```json theme={null}
    {
      "rule_type": "time_based",
      "conditions": {
        "timezone": "Europe/Zurich",
        "windows": [
          {
            "days": [1, 2, 3, 4, 5],
            "start_time": "08:00",
            "end_time": "18:00"
          }
        ]
      },
      "function_type": "redirect",
      "payload": { "url": "https://acme.com/open" }
    }
    ```

    **Use case:** A restaurant QR code routes to the lunch menu from 11:00–14:00 and the dinner menu from 18:00–22:00.
  </Tab>

  <Tab title="A/B Split" icon="flask">
    Split traffic by percentage between variants. Useful for testing different landing pages.

    ```json theme={null}
    {
      "rule_type": "ab_split",
      "conditions": {
        "variant": "A",
        "weight": 50
      },
      "function_type": "redirect",
      "payload": { "url": "https://acme.com/page-a" }
    }
    ```

    <Note>
      Create one rule per variant. Weights across all variants for a node should sum to 100.
    </Note>
  </Tab>

  <Tab title="Geo" icon="earth-americas">
    Route based on the scanner's country (detected via Vercel's edge headers).

    ```json theme={null}
    {
      "rule_type": "geo",
      "conditions": {
        "countries": ["CH", "DE", "AT"],
        "match_type": "include"
      },
      "function_type": "redirect",
      "payload": { "url": "https://acme.com/dach" }
    }
    ```
  </Tab>

  <Tab title="Device" icon="mobile">
    Route based on device type — mobile, desktop, or tablet.

    ```json theme={null}
    {
      "rule_type": "device",
      "conditions": {
        "devices": ["mobile"],
        "match_type": "include"
      },
      "function_type": "redirect",
      "payload": { "url": "https://acme.com/mobile-app" }
    }
    ```
  </Tab>
</Tabs>

## Priority & Evaluation

Rules are evaluated in **ascending priority order** (lower number = higher priority).

```
Priority 0: Time-based → Office hours redirect
Priority 1: Geo → DACH region redirect
Priority 2: Device → Mobile app redirect
Priority 3: Default → Base payload (fallback)
```

The first rule that matches is used. Remaining rules are skipped.

## Creating Rules via API

```bash theme={null}
curl -X POST https://nearnode.io/api/v1/nodes/k9m2xp4q/rules \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "label": "Office Hours",
    "priority": 0,
    "rule_type": "time_based",
    "conditions": {
      "timezone": "Europe/Zurich",
      "windows": [{ "days": [1,2,3,4,5], "start_time": "08:00", "end_time": "18:00" }]
    },
    "function_type": "redirect",
    "payload": { "url": "https://acme.com/welcome" }
  }'
```
