> This page is for SWML.

> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes.

# switch

> Branch on a variable's value, with optional text transforms.

Branch on a variable's value, with optional text transforms applied before matching. Useful for
keyword-driven inbound message handling.

After the selected branch runs, execution continues with the next step after `switch`, unless the
branch moves execution elsewhere (for example with `goto`, `return`, or `transfer`), a step fails,
or the document reaches its 100-step limit.

## **Properties**

**`switch`** `object` — required

An object that accepts the following properties.

---

**`switch.variable`** `string` — required

Variable path to match. Specified without the `%{}` wrapper (e.g. `message.body`). The resolved
value is converted to a string, then transformed and compared exactly with the `case` keys. A
missing variable path resolves to an empty string.

---

**`switch.transform`** `string`

Transform to apply to the value before matching. One of `lowercase`, `uppercase`, `trim`,
`lowercase_trim`, or `uppercase_trim`.

---

**`switch.case`** `object` — required

Map of values to arrays of [SWML methods](/docs/swml/reference/messaging) to execute. The key is the value to
compare against `variable` (after applying `transform`); the value is the array of methods to run on
match. Keys are literal and are not transformed, and matching is exact. An empty-string key matches
a missing variable; otherwise `default` is used.

---

**`switch.default`** `object[]`

Array of [SWML methods](/docs/swml/reference/messaging) to execute if no `case` matches. If
omitted and no case matches, execution stops with an error.

---

## **Examples**

### Keyword-based reply

The `request` after the `switch` runs after whichever branch was taken.

#### YAML

```yaml
version: 1.0.0
sections:
  main:
    - switch:
        variable: message.body
        transform: lowercase_trim
        case:
          help:
            - reply:
                body: "Reply STOP to unsubscribe, or visit https://example.com/help."
          stop:
            - reply:
                body: "You've been unsubscribed."
          start:
            - reply:
                body: "Welcome back!"
        default:
          - reply:
              body: "Sorry, I didn't understand that. Reply HELP for assistance."
    - request:
        url: "https://api.example.com/keyword-log"
        body:
          from: "%{message.from}"
          keyword: "%{message.body}"
```

#### JSON

```json
{
  "version": "1.0.0",
  "sections": {
    "main": [
      {
        "switch": {
          "variable": "message.body",
          "transform": "lowercase_trim",
          "case": {
            "help": [
              {
                "reply": {
                  "body": "Reply STOP to unsubscribe, or visit https://example.com/help."
                }
              }
            ],
            "stop": [
              {
                "reply": {
                  "body": "You've been unsubscribed."
                }
              }
            ],
            "start": [
              {
                "reply": {
                  "body": "Welcome back!"
                }
              }
            ]
          },
          "default": [
            {
              "reply": {
                "body": "Sorry, I didn't understand that. Reply HELP for assistance."
              }
            }
          ]
        }
      },
      {
        "request": {
          "url": "https://api.example.com/keyword-log",
          "body": {
            "from": "%{message.from}",
            "keyword": "%{message.body}"
          }
        }
      }
    ]
  }
}
```

### Branch on a saved request response

#### YAML

```yaml
version: 1.0.0
sections:
  main:
    - request:
        url: "https://api.example.com/customer"
        body:
          phone: "%{message.from}"
        save_variables: true
    - switch:
        variable: request_response.tier
        case:
          gold:
            - reply:
                body: "Welcome back, valued customer!"
          silver:
            - reply:
                body: "Thanks for being a member."
        default:
          - reply:
              body: "Thanks for your message."
```

#### JSON

```json
{
  "version": "1.0.0",
  "sections": {
    "main": [
      {
        "request": {
          "url": "https://api.example.com/customer",
          "body": {
            "phone": "%{message.from}"
          },
          "save_variables": true
        }
      },
      {
        "switch": {
          "variable": "request_response.tier",
          "case": {
            "gold": [
              {
                "reply": {
                  "body": "Welcome back, valued customer!"
                }
              }
            ],
            "silver": [
              {
                "reply": {
                  "body": "Thanks for being a member."
                }
              }
            ]
          },
          "default": [
            {
              "reply": {
                "body": "Thanks for your message."
              }
            }
          ]
        }
      }
    ]
  }
}
```