The Set Vars action is used to define or update global variables in a Traffic Policy’s runtime context. Subsequent actions or expressions can reference these variables for conditional logic through expressions or dynamic interpolation.

Configuration Reference

This is the Traffic Policy configuration reference for this action.

Supported Phases

on_http_request, on_http_response, on_tcp_connect

Type

set-vars

Configuration Fields

vars
array of map[string]any
Required
List of maps that have a key of string and a value of any valid CEL type
Each map must be of exactly size 1, and represents one variable where the key is the name of the variable:
vars:
  - variable_a: value
  - variable_b: value

Behavior

The set-vars action allows you to define your own global variables to be used in future actions or expressions, like this:
on_http_request:
  - actions:
      - type: set-vars
        config:
          vars:
            - variable_a: Hello,
            - variable_b: World!
  - actions:
      - type: custom-response
        config:
          status_code: 200
          headers:
            content-type: text/plain
          body: ${vars.variable_a} ${vars.variable_b}
After being defined through the set-vars action, you can access them through CEL Interpolation or anywhere else that CEL is supported, like expressions.
You can use any valid CEL type as a variable value, including strings, booleans, doubles, null, maps, and lists.

CEL Interpolation

Variables also support CEL interpolation (${<expression>}) in values, enabling you to create dynamic values and types:
on_http_request:
  - actions:
      - type: set-vars
        config:
          vars:
            - message: Your IP is ${conn.client_ip}!
CEL-interpolated strings that are exactly of the form "${vars.<name>}" will resolve to the evaluated type. Strings that contain CEL-interpolated substrings within them such as sample ${conn.tls.client.pem} here will convert all CEL-interpolated substrings to strings

Scoping

Variables you define in the set-vars action are scoped to the Traffic Policy and are not accessible outside of it. Variables defined in each of the three phases (on_http_request, on_http_response, on_tcp_connect) are scoped to that phase, and cannot be accessed in other phases.

Referencing other variables and macros

CEL-interpolated strings may reference all other variables and macros available to traffic policies. This also includes variables previously defined in the same or previous set-vars actions. This allows for powerful dynamic behavior and customization of your Traffic Policy.

Examples

Basic Example

The following Traffic Policy configuration is an example configuration of the set-vars action.

Example Traffic Policy Document

on_http_request:
  - actions:
      - type: set-vars
        config:
          vars:
            - sample_string: bar
            - sample_double: 1.5
            - sample_bool: true
            - sample_null: null
            - sample_list:
                - 1
                - 2
                - 3
            - sample_map:
                key: value
            - sample_nested_map:
                key:
                  - value1
                  - value2
  - expressions:
      - vars.sample_bool == true
    actions:
      - type: custom-response
        config:
          status_code: 200
          headers:
            content-type: text/plain
          body: ${vars.sample_string} ${vars.sample_double} ${vars.sample_list[0]} ${vars.sample_map.key} ${vars.sample_nested_map.key[1]}

CEL Interpolation Example

The following Traffic Policy configuration is an example of using CEL-interpolation with the set-vars action.

Example Traffic Policy Document

on_http_request:
  - actions:
      - type: set-vars
        config:
          vars:
            - sample_string: 'x-foo header: ${request.headers[''x-foo'']}'
            - sample_uint: ${uint(1.5) + 10u}
            - sample_int: ${int(50.5)}
            - sample_map:
                pem: ${conn.tls.client.pem}

Setting Vars based on Previous Vars Example

The following Traffic Policy configuration is an example of setting vars based on previous vars with the set-vars action. Variables are determined in the order in which they are listed in the configuration list.

Example Traffic Policy Document

on_http_request:
  - actions:
      - type: set-vars
        config:
          vars:
            - accepted_models:
                - openai/o1
                - openai/4o
                - openai/4o-mini
            - models: ${actions.ngrok.http_request.res.body}
            - models: ${vars.models.filter(model, model.id in vars.accepted_models)}
            - models: ${vars.models.filter(model, model.latency < 100)}
            - models: ${vars.models.sort(x, y, x.latency < y.latency)}