</>

Technology

Postman

Difficulty

Intermediate

Interview Question

How do you chain requests in Postman using variables?

Answer

Chaining Requests in Postman

API chaining in Postman works by extracting values from a response and storing them in variables, then using those variables in subsequent requests.

Example: Login → Use Token → Create User → Delete User

Request 1: Login

POST {{base_url}}/auth/login

Tests tab:

JavaScript
// Extract token from login response and save it
pm.test("Login successful", function () {
    pm.response.to.have.status(200);
});

const jsonData = pm.response.json();
pm.environment.set("auth_token", jsonData.token);
pm.environment.set("user_id", jsonData.user.id);

Request 2: Get User Details (uses saved token)

GET {{base_url}}/users/{{user_id}}

Authorization tab:

  • Type: Bearer Token
  • Token: {{auth_token}}

Tests tab:

JavaScript
pm.test("User retrieved successfully", function () {
    pm.response.to.have.status(200);
});

const jsonData = pm.response.json();
pm.environment.set("user_email", jsonData.email);

Request 3: Update User

PUT {{base_url}}/users/{{user_id}}

Body (raw JSON):

JSON
{
    "email": "{{user_email}}",
    "name": "Updated Name"
}

Chaining with Pre-request Scripts

If you need to execute a request before the current one:

JavaScript
// In Pre-request Script — fetch token before main request
pm.sendRequest({
    url: pm.environment.get("base_url") + "/auth/login",
    method: "POST",
    header: { "Content-Type": "application/json" },
    body: {
        mode: "raw",
        raw: JSON.stringify({
            username: pm.environment.get("username"),
            password: pm.environment.get("password")
        })
    }
}, function(err, response) {
    const token = response.json().token;
    pm.environment.set("auth_token", token);
});

Tips for Effective Chaining

  • Always validate each step with pm.test() before extracting values
  • Use environment variables for values needed across requests
  • Use collection variables for values shared across collection runs
  • Order matters — use Collection Runner to run in sequence

Follow AutomateQA

Related Topics