When designing automated test workflows in Apidog, you’ll use various types of test steps—such as API requests, database queries, For loop, ForEach Loop, Scripts, and more.

Often, the data produced in one step needs to be used in a later step. This creates a logical flow where data drives the process, and steps are linked together.
For example, you might:
- Retrieve a user token in an earlier step, or run a database query to get a specific record
- Then use those results in an endpoint request in the next step.
This raises an important question:
How can you correctly reference the data from earlier (upstream) steps in the steps that follow (downstream)?
Two Mechanisms for Data Passing in Apidog
In Apidog's automated test, there are two main methods for passing data between different test steps:
1. Retrieve Pre-step Data via "Dynamic Values"
You can directly reference the output of a previous (upstream) step in a later (downstream) step by using the "Dynamic Value" feature. This allows you to fetch data on the fly without storing it.

2. Store Variables and Reuse Them
You can extract important data from an upstream step and store it as a variable. These variables can then be reused in any step that follows.

Mastering these two data passing methods is key to building efficient, data-driven automated workflows in Apidog.
This article explains how to handle data in different types of test steps from two angles: how to extract data and how to use it. By understanding both, you'll be able to build more flexible and better-connected test workflows.
Extracting and Using Endpoint Response Data
Understanding Endpoint Response Data
In automated tests, API requests usually return structured JSON responses, which often serve as the input for later test steps.

Unlike other step types, endpoint responses tend to be more complex — frequently including nested objects and arrays.
How to Extract Data from Endpoint Response
There are two main ways to extract and pass endpoint response data between test steps:
Method One: Dynamic Value Reference
Use "Dynamic Values" to directly reference data from previous steps:
- In any input field of a downstream step, click the magic wand icon.
- Choose "Retrieve Pre-step Data"
- Apidog will automatically insert the correct expression — no need to write it manually.

Method Two: Extract Data as Variables
You can also use the "Extract Variables" feature in endpoint post processors to extract specific fields from the response as variables.
For example, to extract the id
of the products in a response:
- Set the variable name as
products_id
- Use the JSONPath:
$.products[0].id

In later steps, simply reference it using {{products_id}}
.
How to Use Data Extracted from Endpoint Response
Once you've extracted data from an API response—either using "Dynamic Values" or "Extracted Variables"—you can use that data in downstream steps in several ways:
1. Use the data in endpoint requests
- Via Dynamic Value: Just like before, click the magic wand icon in the input field, choose "Retrieve Pre-step Data", and Apidog will insert the correct expression automatically.
- Via Extracted Variable: If you previously saved a value as a variable (e.g.,
products_id
), simply use{{products_id}}
as a parameter in the API request.

2. Use the data in database operations
You can use the API response as input in a database query. Both methods work:
- Dynamic Value Method
SELECT * FROM products WHERE id = '{{$.1.response.body.products[0].id}}'
Use dynamic values to reference the API response data directly in the SQL query:

- Extracted Variable Method: If you’ve extracted the product ID as a variable named
products_id
, use:
SELECT * FROM products WHERE id = '{{products_id}}'
3. Use the data in For Loop
To repeat steps based on the length of an array from an API response:
- Use dynamic values to get the array length, for example:
{{$.1.response.body.products.length}}
This sets the number of loop iterations.

4. Use the data in ForEach Loops
If you want to loop through each item in an array returned by an endpoint:
- Dynamic Value Method: Directly extract the entire array, for example
{{$.1.response.body.products}}

- Extract Variable Method: Assuming the entire array has been extracted as the
products
variable, then directly insert{{products}}
in the loop.

5. Use the data in scripts
To use data from previous steps inside a script, use the pm.variables.get()
method. Here's how:
- Dynamic Value Method: Read the data directly from a previous step:
const products = pm.variables.get("$.1.response.body.products")

- Extract Variable Method: If you saved the array or value as a variable, use the following script to get the data:
Get the value of a temporary variable:
const products = pm.variables.get("products")
Get the value of an environment variable:
const products = pm.environment.get("products")
Get the value of a global variable:
const products = pm.globals.get("products")
{{products}}
directly. Instead, you must retrieve variable values using the appropriate methods mentioned above.Extracting and Using Database Query Results
Understanding Database Data
When a database query step is executed, it returns structured data in the form of an array of objects. Even if only one record is returned, it will still be wrapped in an array. For example:

Unlike endpoint responses, data from database steps cannot be accessed directly using dynamic variables. You must first extract the values into variables.
How to Extract Data from the Database
After running the SQL query in a database step, Apidog will automatically parse the response into structured data, like this:
[
{
"id": "1000",
"title": "Title 1",
"description": "Description for Title 1"
}
]
You can then use JSONPath to extract specific fields and save them as variables.
For example:
- Variable name:
products_id
- JSONPath:
$[0].id
(to extract the ID of the first item). If you want to extract the entire result set, use:$
. - Downstream steps reference variables through
{{ }}
, for example:{{products_id}}

How to Use Data Extracted from a Database
1. Use the data in endpoint requests
If your database query returns an ID and you've saved it as a variable (products_id
), you can directly use it in a downstream endpoint request:

2. Use the data in ForEach Loops
If your query returns a list of records, and you want to process each one individually, you can use the entire array as the source for a ForEach
loop:

The premise is that you save the entire array when extracting variables in database operations, for example:
- Variable name:
products
- JSONPath:
$

3. Use the data in scripts
To use extracted database variables in a script, use the same method as with other variables.
Get the value of a temporary variable:
const products = pm.variables.get("products")
Get the value of an environment variable:
const products = pm.environment.get("products")
Get the value of a global variable:
const products = pm.globals.get("products")
Extracting and Using Data from For Loops
How For Loops
Works?
A For loop
is used to repeat a specific set of actions multiple times. It runs based on a defined number of iterations.
You can either set a fixed number of times to loop, or use a dynamic value like the length of an array .length
returned from a previous step. For example: {{$.1.response.body.products.length}}
:

Note: Unlike other steps that output data, a For loop
itself doesn't directly return data. It only provides an index value (starting from 0) to show how many times the loop has been executed.
How to Extract Data from For Loops
During the execution process, you can access the current loop index using dynamic variable syntax: {{$.9.index}}

- The number
9
refers to the ID of theFor loops
step. - Each step has a unique ID.
- The index starts at 0 for the first loop, 1 for the second, and so on.
How to Use Data Extracted from For Loops
1. Use the data in scripts
If you want to use loop-related info in your test script, you can use pm.variables.get()
to get the current index:
// Get the current loop index
const index = pm.variables.get("$.7.index");

2. Use the data with other data sources
A common use case for a For loop
is to process data from earlier steps—like looping through an array and handling each item:
// Get the array from a previous step
const products = pm.variables.get("$.1.response.body.products");
// Get the current loop index
const index = pm.variables.get("$.7.index");
// Access the current item in the array using the index
console.log(products[index]);
This lets you perform batch operations on each item in the array during each loop iteration.
Extracting and Using Data from ForEach Loop
How the ForEach Loop
Works?
The ForEach loop is specifically designed for array data. It automatically iterates through each element in the array and performs the same set of operations.
The key difference between a ForEach loop
and a For loop
is that ForEach loop
automatically extracts the complete data of the current array element in each iteration, making it directly accessible in its child steps.
Accessing Data in a ForEach Loop
Within a ForEach loop
, the system automatically creates two special variables:
- Current loop elements: Contains the full data of the array element currently being processed. For example:
{{$.4.element}}
. If the array contains objects, you can directly access their properties, such as:{{$.4.element.id}}
,{{$.4.element.title}}
, etc.

- Current loop index: Represents the current iteration count (starting from 0), e.g.,
{{$.4.index}}
.

Note: The number 4
refers to the ID of the ForEach Loop
step. In your actual workflow, replace this with the correct step ID from your process.
Common Use Cases for ForEach Loop
Data
1. Use the data in endpoint requests
ForEach loops are perfect for handling batch data operations. For example, if you have an array of items, you can automatically send an endpoint request for each item in the array. During each loop, the same request template is reused, but filled with different data, such as {{$.4.element.id}}
.

2. Use the data in database operations
You can use data from the current loop item to run database queries or insert multiple rows.
Example: Querying the database using the current item's field
SELECT * FROM products WHERE id = '{{$.4.element.id}}'
The expression {{$.4.element.id}}
can be accessed using the dynamic value feature.

Inserting multiple fields from the current item into a table:
INSERT INTO products (id, title) VALUES ('{{$.4.element.id}}', '{{$.4.element.title}}')
3. Use the data in scripts
If you need to further process data from the loop in a custom script, you can use the method pm.variables.get()
to retrieve the values:
// Get the current element
const item = pm.variables.get("$.4.element");
// Get the current index
const index = pm.variables.get("$.4.index");
Extracting and Using Data from Scripts
How Script Data Works
Unlike other steps in a workflow, scripts do not automatically generate output that can be used directly in the following steps. If you want to pass data from a script to later steps, you must manually store it in a variable.
How to Extract Data from a Script
You can save values into different types of variables like this:
// Get JSON response data
// 1. From the current endpoint response
// const currentData = pm.response.json();
// 2. Or get it from a previous step using a dynamic value
const preData = pm.variables.get("$.1.response.body");
// Save to environment variables
pm.environment.set('products', preData.products);
pm.environment.set('products_id', preData.products[0].id);
// Save to global variables
pm.globals.set('products', preData.products);
// Save to temporary variables (valid only during this run)
pm.variables.set('products', preData.products);
Once the variables are extracted and set, you can reference them in later steps using the syntax {{variableName}}
.
How to Use Data Extracted from Scripts
1. Use the data in endpoint requests
Variables set in a script can be directly used as parameters in downstream endpoint requests. For example, if you save products_id
in the script, you can reference it in a later request like this: {{products_id}}
.

2. Use the data in database operations
Script variables can also be used to build dynamic SQL statements. For example:
SELECT * FROM products WHERE id = '{{products_id}}'
3. Use the data in For Loop
You can use a script to generate the loop count or other intermediate values, store them as "temporary variables", and pass them to the For Loop
step:
pm.variables.set("loopCount", 5);
Then, in the loop settings, use {{loopCount}}
as the number of iterations.
4. Use the data in ForEach Loops
You can store an entire array in a variable and use it as the data source for a ForEach Loop
.
Example:
// Optionally retrieve array from a previous step using dynamic values
// const preData = pm.variables.get("$.1.response.body.products")
const preData = [{id: 1}, {id: 2}, {id: 3}];
// Save to environment variable
pm.environment.set('products', preData);
Then in the ForEach Loop
, set the data source to {{products}}
.
Conclusion
To build an efficient automated test workflow, it’s essential to understand how to extract and use data from different types of steps:
- Endpoint and database steps generate business-related data.
- Loop steps control the flow of logic.
- Script steps are used to process and transform data.
By combining dynamic value referencing with variable extraction, you can design a flexible and powerful data-driven testing process.
Best Practices for Passing Data:
- Use dynamic value referencing for simple data that’s only used once or occasionally.
- Use variable extraction for data that needs to be reused across multiple steps or handled in scripts.
Choosing the right method based on data complexity and frequency of use will make your workflow more maintainable and efficient.