Lesson 85 min

How to Use Switch Node in n8n

Learn how to use the Switch node in n8n to route your workflow execution to different paths based on conditions and rules.

How to Use Switch Node in n8n Tutorial

In this tutorial, you'll learn how to use the Switch node in n8n to create conditional routing in your workflows. The Switch node allows you to direct your workflow execution down different paths based on specific conditions, making it essential for building intelligent automation that responds dynamically to your data.

What is the Switch Node?

The Switch node acts as a traffic controller in your workflows. It evaluates incoming data against multiple conditions (called "routing rules") and sends the data down different output paths based on which condition is met. Think of it like a switchboard operator directing calls to the right department.

Workflow Overview

A typical workflow using the Switch node includes:

  • Trigger Node - Starts the workflow (webhook, schedule, manual trigger, etc.)
  • Data Processing Nodes - Optional nodes that prepare or transform data before routing
  • Switch Node - Evaluates conditions and routes data to appropriate outputs
  • Output Branches - Different paths for each condition, each leading to specific actions

Switch Node Configuration

The Switch node offers two main routing modes:

Mode 1: Rules (Conditions)

In this mode, you define multiple rules with conditions. The data is routed to outputs based on which rule matches first.

Configuration Options:

  • Mode: Rules
  • Rules: Define multiple conditions
  • Output: Specify output index for each rule
  • Fallback Output: Where to route data if no rules match

Mode 2: Expression

In this mode, you write a single expression that returns an output number to determine the routing path.

Example Use Cases

Use Case 1: Customer Query Routing

Route customer inquiries to different teams based on query type.

Rules Configuration:

Rule 1: {{ $json.query_type }} === "refund" → Output 0 (Refunds Team)
Rule 2: {{ $json.query_type }} === "complaint" → Output 1 (Support Team)
Rule 3: {{ $json.query_type }} === "order_tracking" → Output 2 (Logistics Team)
Fallback: Output 3 (General Inquiries)

Use Case 2: Order Value-Based Processing

Route orders to different fulfillment workflows based on order value.

Rules Configuration:

Rule 1: {{ $json.order_total }} > 1000 → Output 0 (Premium Processing)
Rule 2: {{ $json.order_total }} > 100 → Output 1 (Standard Processing)
Rule 3: {{ $json.order_total }} <= 100 → Output 2 (Basic Processing)

Use Case 3: Lead Scoring and Assignment

Direct leads to different sales representatives based on lead score.

Rules Configuration:

Rule 1: {{ $json.lead_score }} >= 80 → Output 0 (Senior Sales Rep)
Rule 2: {{ $json.lead_score }} >= 50 → Output 1 (Mid-Level Sales Rep)
Rule 3: {{ $json.lead_score }} < 50 → Output 2 (Junior Sales Rep)

Working with Multiple Conditions

You can combine multiple conditions in a single rule using logical operators:

AND Condition Example:

{{ $json.country === "US" && $json.order_total > 500 }}

OR Condition Example:

{{ $json.priority === "urgent" || $json.status === "critical" }}

Complex Condition Example:

{{ ($json.customer_type === "premium" && $json.order_total > 100) || $json.vip === true }}

Best Practices

  1. Order Matters: Rules are evaluated from top to bottom. Place more specific conditions before general ones.
  2. Use Fallback Output: Always configure a fallback output to handle unexpected data.
  3. Keep It Simple: If you have too many rules, consider breaking your workflow into smaller, more manageable pieces.
  4. Test Thoroughly: Use the "Execute Node" feature to test each routing path with sample data.
  5. Descriptive Names: Label your output paths clearly so you know where each route leads.

Common Patterns

Pattern 1: Priority-Based Routing

High Priority → Immediate Action
Medium Priority → Queue for Processing
Low Priority → Batch Processing

Pattern 2: Status-Based Workflow

New → Initial Processing
Pending → Review Process
Approved → Execution
Rejected → Notification

Pattern 3: Geographic Routing

North America → US Server
Europe → EU Server
Asia → Asia Server
Other → Global Server

Key Benefits

  • Conditional Logic: Build intelligent workflows that adapt to different scenarios.
  • Multiple Outputs: Route data to multiple different paths from a single node.
  • Clean Architecture: Keep workflows organized by separating different logic paths.
  • Error Handling: Use fallback outputs to gracefully handle unexpected data.
  • Scalability: Easily extend workflows by adding new routing rules.
  • Flexibility: Combine with other nodes for powerful automation scenarios.

Tips and Tricks

  • Use with IF Node: For simple true/false conditions, consider using the IF node instead.
  • Combine with Merge Node: Merge outputs back together after processing if needed.
  • Expression Mode: Use expression mode for dynamic routing based on calculations.
  • Testing: Always test with edge cases to ensure all conditions work as expected.

Troubleshooting

Data Not Routing as Expected?

  • Check that your expressions are correctly formatted
  • Verify data types (string vs number comparisons)
  • Ensure conditions are in the right order

No Data in Outputs?

  • Check if data is reaching the Switch node
  • Verify that at least one condition matches
  • Check the fallback output configuration

Conclusion

The Switch node is a powerful tool for creating dynamic, intelligent workflows in n8n. By mastering conditional routing, you can build sophisticated automation that handles complex business logic with ease.