Route Data with the Switch Node

Difficulty
4/10
Tags
EssentialsSwitchGmail TriggerOpenAIGmailConditional RoutingJSON
Requirements
[Gmail OAuth credentials](https://docs.n8n.io/integrations/builtin/credentials/google/oauth-single-service/)[OpenAI API key](https://docs.n8n.io/integrations/builtin/credentials/openai/)

Most of the time, an IF node is enough. You check a condition, things go left or right, done.

But sometimes you hit a point in your workflow where you need more than two directions. I run into this constantly with AI classifiers. Say you have incoming emails and an AI that categorizes each one as support, sales, complaint, or info. That's four categories. An IF node gives you two outputs. You'd need to chain three IF nodes together, and that gets ugly fast.

The Switch node handles this. One node, as many outputs as you need, each with its own rule. And the part I like most: you can name each output, so the arrows in your workflow actually say "Support" or "Sales" instead of "Output 0" and "Output 1". When you have five or six branches coming out of one node, those labels are the only thing keeping the workflow readable.

In this exercise, you'll build exactly that: a Gmail workflow where an AI classifies incoming emails and a Switch node routes each one to a named output. The full pattern, from trigger to classification to routing, in one workflow.

What you'll practice:

  • Using the Switch node in Rules mode
  • Creating multiple routing rules
  • Naming outputs for readability
  • Handling unmatched items with a fallback output
  • Connecting an AI classifier to a Switch node

Your Task

  1. 1Copy the exercise below
  2. 2Paste into your n8n editor (Ctrl+V)
  3. 3Solve it — use hints if you get stuck
  4. 4Check the solution when done

Build a workflow that classifies incoming emails with AI and routes them to named outputs:

  1. Gmail Trigger watches for new unread emails in your inbox
  2. OpenAI classifies each email as: support, sales, complaint, or info
  3. Switch routes by the AI's classification to named outputs
  4. Edit Fields on each branch labels the email with its category

Your Switch node should have these outputs:

  • "Support" for support emails
  • "Sales" for sales emails
  • "Complaint" for complaint emails
  • "Info" for info emails
  • "Other" as a fallback for anything the AI can't classify

Expected result: When an email arrives, the AI classifies it, the Switch routes it to the matching output, and the Edit Fields node labels it. The arrows on the Switch node should display the output names.

Get the exercise workflow

Create a free account to copy exercises into your n8n editor.

Hints

Setting up the trigger:

  1. Add a Gmail Trigger node. Set it to poll every minute
  2. Under Filters, you can optionally filter for INBOX and unread emails

Setting up the classifier:

  1. Add an OpenAI node (search for "OpenAI", select the one that says "Message a Model")
  2. Set the model to gpt-5-nano (or any model you have access to)
  3. Add a user message with the email content: Subject: {{ $json.subject }} From: {{ $json.from }} Body: {{ $json.body }}
  4. Add a second message with role Assistant containing classification instructions: tell the AI to classify into support, sales, complaint, or info and return only JSON like {"category": "support"}
  5. Enable JSON Output so the AI returns structured data

Setting up the routing:

  1. Add a Switch node after the OpenAI node
  2. Add a rule: set the value to {{ $json.message.content.category }}, operation to equals, value to support. Enable Rename Output and name it "Support"
  3. Click Add Routing Rule and repeat for sales, complaint, and info
  4. Under Options, set Fallback Output to "Extra Output" and rename it to "Other"

Adding actions:

  1. Add an Edit Fields (Set) node after each Switch output. In each, add a field called routed_to with the category name (e.g., "Support"). In a real workflow, you'd replace these with Gmail label nodes, Slack messages, or whatever action fits each category.

Alternative: If you don't have Gmail set up, use a Manual Trigger with pinned test data instead. The workflow pattern is the same.

Explanation

Switch vs IF vs Filter:

n8n has three conditional nodes, and they all use the same condition engine under the hood. The difference is what they do with the results.

IF gives you two outputs: true and false. Every item goes to one or the other, nothing is ever dropped. Use it for simple either/or decisions. "Is this order above $100?" Two paths, done.

Filter also has two outputs (kept and discarded), but the framing is different. You're narrowing down a list, not branching a workflow. "Only keep products rated above 4." If you're using an IF node but never connecting anything to the false output, use a Filter instead. It communicates intent better.

Switch is for everything beyond two paths. When you need three, four, ten different directions, Switch is the tool. Each rule creates a new output.

I often combine them: Filter out noise first, then Switch to route what's left.

How Rules mode works:

Each rule has its own conditions. The Switch evaluates each item against the rules in order. By default, an item goes to the first matching rule and stops. Rule order matters.

You can change this: under Options, there's "Send data to all matching outputs." When enabled, an item is duplicated and sent to every output whose rule matches. Useful when a single item belongs in multiple categories.

Multiple conditions per rule:

Each rule can have more than one condition, combined with AND or OR. Say you want to route only urgent support emails. Add two conditions to the same rule: category equals "support" AND priority equals "urgent". Both must be true for the item to match that rule.

Naming outputs:

Each rule has a "Rename Output" toggle. Enable it, type a name, and that name shows up on the arrow in the workflow canvas. Purely cosmetic, but it makes a big difference when you have many branches. Without labels, you're squinting at "Output 0" trying to remember what it does.

Fallback output:

By default, items that don't match any rule are silently dropped. This is the most common "where did my data go?" bug with the Switch node. If you want to catch unmatched items, set Fallback Output to "Extra Output" under Options. This creates an additional output at the end. You can rename it too.

Expression mode:

There's a second mode where you don't write rules. Instead, you write a single expression that returns a number (the output index). You set how many outputs you want, and the expression decides where each item goes. It's more compact for certain patterns but harder to read. Rules mode covers the vast majority of use cases.

The "exists" check:

One pattern worth knowing: instead of checking if a field equals a specific value, you can check if a field exists at all. For example, in a Telegram bot, incoming messages might have a voice field or a text field, but never both. A Switch rule that checks "does voice.file_id exist?" routes voice messages one way, and a rule checking "does text exist?" routes text another way. This works whenever the shape of your data tells you what type of item you're dealing with, not just its value.

Copy the Solution

Paste into n8n with Ctrl+V to compare with your approach.

How was this lesson?

Get the solution workflow

Create a free account to copy solutions into your n8n editor.

Related Content

Continue your learning journey with these related lessons and guides: