How to Pass Data between Test Steps in Apidog?

Discover how to pass and reuse data across endpoint, database, loop, and script steps in Apidog. This guide explains how to use dynamic values and variables to build flexible, data-driven automated test workflows that are easier to maintain and scale.

Oliver Kingsley

Oliver Kingsley

6 August 2025

How to Pass Data between Test Steps in Apidog?

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.

test step types

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:

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.

Retrieve Pre-step Data via "Dynamic Values" in Apidog Automated test

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:

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:

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

Use the extracted data in API Requests

2. Use the data in database operations

You can use the API response as input in a database query. Both methods work:

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:

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:

{{$.1.response.body.products.length}}

This sets the number of loop iterations.

Use the data in For Loop

4. Use the data in ForEach Loops

If you want to loop through each item in an array returned by an endpoint:

Use the data in ForEach Loops via dynamic value method

5. Use the data in scripts

To use data from previous steps inside a script, use the pm.variables.get() method. Here's how:

const products = pm.variables.get("$.1.response.body.products")
Use the data in Scripts via dynamic value method

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")
💡
In scripts, you can't use interpolation syntax like {{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:

database query results in Apidog

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:

use JSONPath to extract specific fields and save them as variables

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:

use the data extracted from database in endpoint requests

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:

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}}

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");
Use the extracted for loops data in scripts

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:

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}}.

Use the extracted data in API Requests

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:

By combining dynamic value referencing with variable extraction, you can design a flexible and powerful data-driven testing process.

Best Practices for Passing Data:

Choosing the right method based on data complexity and frequency of use will make your workflow more maintainable and efficient.

Explore more

How to Share Database Connection Settings with Team Members in Apidog

How to Share Database Connection Settings with Team Members in Apidog

Apidog now supports secure, cloud-based database connection sharing using Vault and environment variables. This guide walks you through setup, testing, and collaborative use—boosting team efficiency while keeping sensitive data protected.

6 August 2025

How to Run GPT-OSS for Free Using Ollama?

How to Run GPT-OSS for Free Using Ollama?

Learn how to run GPT-OSS (OpenAI’s open-weight models) for free using Ollama. This technical guide covers installation, model setup, API integration, and debugging with Apidog. Optimize your local AI workflow with privacy and cost savings.

5 August 2025

How to Use JSON Format to Write Shockingly Accurate Prompts?

How to Use JSON Format to Write Shockingly Accurate Prompts?

Discover how to use JSON format to write shockingly accurate AI prompts with this technical guide. Learn step-by-step methods, best practices, and examples to enhance outputs using tools like Apidog. Perfect for developers and AI enthusiasts!

5 August 2025

Practice API Design-first in Apidog

Discover an easier way to build and use APIs