Postman Using Variables for Dynamic Testing in Postman Estimated reading: 6 minutes 232 views Using Variables for Dynamic Testing in Postman is a game-changer when it comes to building clean, flexible, and scalable API tests. As a QA tester, you’ve probably run into situations where values like IDs, tokens, or base URLs change constantly. Instead of updating each request manually, we can store these values as variables and reuse them across requests.In this guide, we’ll walk through the different types of variables Postman supports, how to define and assign them, and how to dynamically extract values from API responses. We’ll use a real example from a Billing API so you can see these practices in action. By the end, you’ll be equipped to write API tests that adapt automatically making your workflow more efficient and reliable.Why Use Variables for Dynamic Testing?When working with APIs, you often need to reference values that are dynamic or generated at runtime like a newly created billing record ID or an access token. Using variables helps you: Avoid hardcoding values across requests Easily switch between different environments Chain requests by passing data from one response to the next Write tests that scale and stay maintainable over timeTypes of Variables in Postman Global Variables: Accessible from anywhere in your Postman workspace. Use these for universal values like fallback tokens or default settings. Environment Variables: These are tied to a specific environment. You might have separate environments for: Local,Development,Staging andProduction Collection Variables: Scoped to a specific collection. This is useful when you want to keep variables like billing_id or base_url tied to one set of requests. Local/Temporary: These exist only within the execution of a single request or script. They’re not stored or reused later but are helpful for temporary calculations.Creating an Environment to Store VariablesBefore you can start reusing variables like base_url, person_id, or billing_id, it’s a good idea to create an environment in Postman. Environments act like isolated workspaces where you can store and manage key-value pairs making it easy to switch between different setups like Development, QA, or Production. Creating a New Environment In Postman, click the gear icon in the top-right corner of the app and choose Manage Environments . Click Add to create a new environment. Give it a meaningful name, such as Financial API - QA Set the key and initial value for these: base_url,person_id & billing_id Save the environment or collection. Variable Initial Valuebase_urlhttps://mqa-banking-api.onrender.comperson_id(leave blank – will be set dynamically)billing_id(leave blank – will be set dynamically) Adding New Variables To EnviromentUsing Environment Variables in RequestsOnce your environment is saved and selected from the dropdown in the top-right corner of Postman, you can reference these variables in any request using double curly braces:Plaintext{{base_url}}/api/billing/{{billing_id}} Using Environment Variables in Postman RequestAs you run tests and scripts, variables like person_id and billing_id can be dynamically set using the Scripts tab and automatically injected into future requests.Capturing Data Dynamically with ScriptsInstead of manually copying values like IDs between requests, Postman lets you capture data directly from a response and store it as a variable using the Scripts tab. This makes your workflow faster, more reliable, and completely automated.Creating A New Person Record{ "firstName": "Lilly", "lastName": "John", "streetAddress": "123 South Hill", "city": "West End", "state": "AL", "zip": "12640", "email": "lilly_john@masteringqa.com", "jobTitle": "Marketer" }To store this id as a variable named person_id, add the following code in the Scripts tab of that request:JavaScriptlet jsonData = pm.response.json(); pm.environment.set("person_id", jsonData.id);Now, when you create a billing record in the next request, you can dynamically insert that person_id into the request url like: POST {{base_url}}/api/billing/{{person_id}} BashPOST {{base_url}}/api/billing/{{person_id}}Create A New Billing Record{ "streetAddress": "123 South Hill", "city": "West End", "state": "AL", "zip": "12640", "phoneNumber": "(264) 555-1234", "cardNumber": "4111 1111 1111 1111", "expDate": "12/26", "cvv": "123", "subscriptionName": "Premium", "amount": 49.99 }Similarly, after creating a billing record, you can capture its id as billing_id using this script:JavaScriptlet jsonData = pm.response.json(); pm.environment.set("billing_id", jsonData.id);This allows you to reuse {{billing_id}} in subsequent requests like:BashGET {{base_url}}/api/billing/{{billing_id}}Built-in Dynamic VariablesPostman also supports auto-generated variables you can use in request bodies to simulate new or random data without writing scripts. Example: JSON{ "personId": "$guid", "email": "user_$randomInt@example.com", "amount": 49.99 }Popular built-ins include: $guid – generates a globally unique ID $randomInt – returns a random number $timestamp – current time in seconds $randomEmail – useful for mock signupsUse these when you need fresh test data on each run, especially in POST requests.Real-World Workflow: Dynamic Billing API TestingLet’s walk through a real API test sequence. We’ll use dynamic variables to simulate the full workflow no hardcoded values. 1 Create a New Person 2 Create a Billing Record 3 Fetch the Billing Record Endpoint: POST {{base_url}}/api/personsRequest Body (with built-in dynamic variables):{ "firstName": "Dennis", "lastName": "Mennis", "streetAddress": "4567 Element Dr", "city": "Chestnut", "state": "FL", "zip": "33405", "email": "dennis_{{ '$randomInt' }}@masteringqa.com", "jobTitle": "Engineer" }Tests tab script: let jsonData = pm.response.json(); pm.environment.set("person_id", jsonData.id); This captures the generated id from the response and saves it as person_id. Endpoint: POST {{base_url}}/api/billingRequest Body: { "streetAddress": "1420 Elm Street", "city": "Austin", "state": "TX", "zip": "73301", "phoneNumber": "(512) 555-1234", "cardNumber": "4111 1111 1111 1111", "expDate": "12/26", "cvv": "123", "subscriptionName": "Premium", "amount": 49.99 } Tests tab script (store the billing ID): let jsonData = pm.response.json(); pm.environment.set("billing_id", jsonData.id); Now we’ve stored the billing record ID dynamically and can reference it in future tests. Endpoint: GET {{base_url}}/api/billing/{{billing_id}}This request will fetch the exact billing record you just created using the dynamically captured ID. You can now validate fields like amount, subscriptionName, and personId. Why This Workflow MattersBy using variables dynamically: You eliminate manual copy-paste between requests. You test full workflows in realistic, chained sequences. Your tests remain reusable and data-driven. You’re preparing for seamless integration with tools like the Postman Collection Runner or CI/CD pipelines.ConclusionUsing Variables for Dynamic Testing in Postman helps you move beyond hardcoded requests by capturing real response data and reusing it across your workflow. Whether you’re testing billing, users, or tokens, variables make your tests flexible and maintainable.You’ve now created a person, stored their ID, generated a billing record, and retrieved it dynamically all within Postman.Next, let’s explore Automating Tests with Postman Runner to take our Postman testing to the next level!Tagged:Postman Postman - Previous Writing Basic Tests in Postman Next - Postman Automating Tests with Postman Runner