Using variables with API tests

Creating and using variables in the API Test Editor

You can use variables in API tests to assert on dynamic values, compare values from one response to another, generate random values, do data-driven testing, and handle other scenarios that require working with dynamic data.

The following types of variables can be used in API tests:

Creating variables

There are two primary ways to create a variable within an API test:

  • Use the Validation and variable assignment panel to create test-generated variables
  • Using the Variables section in the left-hand panel to create placeholder values for data-driven variables and environment variables.

Test-generated variables

Test-generated variables are created from the response of an API step:

  1. Click on the Validation and variable assignment section on the right side of the screen. A panel will open up.
  2. Select the Variables tab.
  3. Click on the Add button. A variable assignment card will appear.
  4. Enter a description for your variable.
  5. Select a variable target. If you select Header, enter the header key. For a JSON body, type the JSON path to the target response property.
  6. Give the variable a name in the "Assign to variable" input field.

Creating a test-generated variable

When you send the request again, you will see the variable appear in the Variables section of the left-side panel. Variables can be used in consecutive test steps or shared with other tests within the same test plan.

Data-driven variables

If you want to use data-driven variables in your API test, use the Variables section on the left side.

Click the Add row button and enter a name and default value.


Creating data-driven variables to use for assertions

As long as the variable name matches (case-sensitive), the default value in the test will be overridden by data-driven variables, which can come from a DataTable scenario or a previous test run within the same plan (shared variables).


API tests can be associated with DataTables. To edit an API test with a DataTable scenario, follow the instructions outlined here.

Environment variables

If you need to run an API test across multiple environments, you can use environment variables to pass environment-specific variables to an API test. Here is what you need to do:

  1. In the API Test Editor, add the variable(s) to the Variables section on the left side.
  2. Give the variable(s) a default value that will be overridden based on the environment.
  3. Ensure that the variable names in the test match the environment variables.

When you run the API test in the mabl cloud, choose the desired environment and mabl will use the variable values coming from the environment.

Using variables

You can use variables in the API request or response validation with mabl variable syntax. In the following example, the variable existing_user_id is added to the URL using mabl variable syntax ({{@existing_user_id}}) in order to obtain information on a user:


Variables may also be used in the Validations and variable assignment section. For example, if you wanted to verify that a response value matches a value coming in from a DataTable scenario, you could use mabl variable syntax to represent the DataTable value in the assertion:


Using variables to assert an expected value

Random variables

While API tests can use mabl variable syntax, they do not support the other valid expressions: macros, faker values, and math expressions. However, you can use Postman dynamic variables in API tests thanks to the mabl integration with Postman.


Using Postman dynamic variables in a request