> ## 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.

# Smart Routing

> Dynamic redirect logic — route scans by time, location, device, or traffic weight.

Smart Routing is the orchestration engine behind every NearNode scan. Instead of a static redirect, each node evaluates a **priority-ordered rule stack** at the edge and routes the user to the first matching destination.

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

<Info>
  **Ops Tip:** You can manage routing rules visually in the Dashboard under each node's "Rules" tab — no API calls required.
</Info>

## Rule Types

<Tabs>
  <Tab title="Time-Based" icon="clock">
    Route based on time of day and day of week. Deploy time-sensitive destinations without touching your infrastructure.

    ```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. Run controlled experiments on physical assets.

    ```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 must sum to 100. Traffic distribution is non-deterministic per request.
    </Note>
  </Tab>

  <Tab title="Geo" icon="earth-americas">
    Route based on the scanner's country, detected via Vercel's edge headers at sub-millisecond latency.

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

    **Use case:** Show German content to users scanning in the DACH region, English everywhere else.
  </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" }
    }
    ```

    **Use case:** Send Android/iOS users to the App Store and desktop users to the web app.
  </Tab>
</Tabs>

## Priority & Evaluation

Rules are evaluated in **ascending priority order** (lower number = higher priority). The first matching rule wins — remaining rules are skipped.

```
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)
```

<Info>
  **Performance:** Rule evaluation adds less than 150ms to the edge response. Total TTFB with routing rules stays under 350ms globally.
</Info>

## 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" }
  }'
```

## Payload Types

Each routing rule can target any supported payload type:

| Type        | Description                                         |
| ----------- | --------------------------------------------------- |
| `redirect`  | HTTP 307 redirect to a URL                          |
| `vcard`     | Generate and serve a vCard download                 |
| `wifi`      | WiFi provisioning page (SSID, password, encryption) |
| `action`    | Direct-to-action: WhatsApp, Calendly, email, phone  |
| `microsite` | Hosted microsite with custom content                |
