# Switch

Route messages to different branches based on message properties, sequence information, or a JSONata expression.

## How it works

Switch rules are evaluated in order from top to bottom.

* value rules test a specific message, flow, global, or environment value
* sequence rules work with message sequences such as those produced by `Split`
* JSONata rules evaluate an expression against the whole message and match when the expression returns a true value
* `Otherwise` is the fallback rule when no earlier rule matches

In the current editor, new Switch nodes start with an `Otherwise` rule and the editor keeps it last. For most Contextual agent flows, enable the option to stop after the first match so one message follows one primary branch and `Otherwise` acts as the final fallback output.

Sending the same message down multiple matching branches is an advanced pattern. It can create race conditions if more than one branch can reach terminal behavior such as [Event End](/documentation-and-resources/components-and-data/flows/node-reference/event/event-end.md), [Event Error](/documentation-and-resources/components-and-data/flows/node-reference/event/event-error.md), or an HTTP response path.

## Important rule behavior

* `is true`, `is false`, and `is null` use strict comparisons and do not coerce values between types
* `is empty` and `is not empty` work on strings, arrays, Buffers, and objects; they do not match `boolean`, `null`, or `undefined` values
* sequence rules apply to message sequences and can be used with data produced by `Split`

## Message sequences

By default, Switch does not change `msg.parts` when it receives a message that belongs to a sequence.

The **recreate message sequences** option can be enabled to create new sequences for each matching rule. In that mode, the node buffers the full incoming sequence before sending the new sequences onward.

## Common uses

* branch on HTTP status or response content
* continue pagination only when a token or next-page value is present
* separate success and error paths after a Function or Request node
* route event types to different processing branches


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.contextual.io/documentation-and-resources/components-and-data/flows/node-reference/function/switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
