Variables

Last modified on July 26, 2024

Use variables to extract values from JSON and XML responses and insert these values in subsequent requests. This way you can pass session tokens, item IDs, and similar data between the requests.

Variables can be used in the request URL, headers, body, and in bearer tokens. The {{var_name}} syntax is used to insert a variable into a request. See Using variables in requests below for details.

View existing variables

When you view your API monitor’s configuration, the Step Summary on the left displays the variables defined in each step. The Variables tab in the step configuration is where the variables can be created, modified and deleted.

API monitor variables

Click the image to enlarge it.

Create variables

Variables can be created from JSON and XML responses.

  1. Open your API monitor settings.

  2. Select the monitor step from which you want to extract a variable.

  3. Click a value in the response, then click Add Variable.

    Adding variables

    Click the image to enlarge it.

    If this is an XML response, AlertSite will display the values of the selected element and all its attributes. Choose the values you want to extract and click Add.

  4. Variable names are generated automatically based on the response field names. You can change the names in the Name column if you wish.

    Note: Variable names must be unique within a monitor.
  5. Add more variables if required.

  6. Save the monitor.

Variables table

You can also add variables manually to the table on the Variables tab:

Column Description

Name

The variable name, up to 32 characters. Can contain only letters A..Z, a..z, numbers 0..9 and underscores _, and cannot start with a number. Variable names must be unique within a monitor.

In subsequent requests, you can refer to this variable using {{var_name}}. See Using variables in requests below.

Property

An expression that specifies the JSON or XML property whose value will be stored in the variable. If the specified property is not found in the response, the variable value will be empty.

JSON responses

Specify the path to the desired JSON property using the dot notation, for example:

store.book[0].title

XML responses

Specify an XPath expression that points to the desired XML element or attribute whose value you want to save, for example:

//store/book[1]/title   - element value

//user[2]/@id           - attribute value

AlertSite supports XPath 2.0.

Value Read-only. The current value of the specified JSON or XML property.

Use variables in requests

You can use variables in the request URL, headers, body, and in bearer tokens. The fields where you can use variables have the VAR button. To insert a variable, put the cursor where you want the variable to appear, then click VAR and select a variable from the list.

Inserting variables

Click the image to enlarge it.

The inserted variables use this syntax:

{{var_name}}

Variables can be concatenated with static text to produce the desired value:

prefix{{var_name}}suffix

Add quotes around the variable name if necessary – "{{var_name}}" (for example, if the value must be sent as a string in JSON).

When running the test, AlertSite will replace the variables with the extracted values. If no value was extracted from a request (for example, if the specified JSON or XML field was not found), the variable value will be empty.

Notes:

  • Each step can only use variables from the previous steps.

  • Variables are supported only in the request data and not in other monitor parameters.

Variables in URLs

A request URL has the following format:

http(s)://host/path/subpath?param=value&param2=value2

You can use variables to replace some parts of a URL, such as host, path/subpath, and values of query parameters.

Variables cannot be used:

  • As a complete URL.

  • In names of query parameters (param and param2 in the example above).

Examples

In URLs:

https://{{region}}.example.com/v1/reports/{{report_type}}?month={{year}}-{{month}}

Given the following variable values:

  • {{region}} = us-west

  • {{report_type}} = usage

  • {{year}} = 2018

  • {{month}} = 11

the URL expands to:

https://us-west.example.com/v1/reports/usage?month=2018-11

In JSON:

{
  "session_id": "{{token}}",
  "message": "test"
}

In XML:

<user id="{{id}}">
  <name>{{firstname}} Jones</name>
</user>

In form data:

field1={{var1}}&{{var2}}={{var3}}

In plain text:

Hello, {{name}}!

In request headers:

Authorization: Bearer {{token}}

See Also

Creating an API Endpoint Monitor

Highlight search results